Skip to content

Instantly share code, notes, and snippets.

@clvs7-gh
Last active June 22, 2021 09:30
Show Gist options
  • Save clvs7-gh/77b1f50d51d4e238c8482fc31296d4c4 to your computer and use it in GitHub Desktop.
Save clvs7-gh/77b1f50d51d4e238c8482fc31296d4c4 to your computer and use it in GitHub Desktop.
Table shortcode for hugo
{{ $attrs := "" }}
{{ range $k, $v := .Params }}
{{ $attrs = printf "%s %s=\"%v\"" $attrs $k $v }}
{{ end }}
{{ $newTable := printf "<table %s>" $attrs }}
{{ (replace (.Inner | markdownify) "<table>" $newTable) | safeHTML }}

A table shortcode for hugo. Until 0.60.0, Mmark was good option, convenient way in order to add any attributes to table. But from 0.60.0, mmark is marked as deprecated. On the other hand, new standard markdown parser, goldmark does not support that's way. So, I created a table shortcode for hugo. This shortcode supports both of markdown table and attributes.

Example usage:

{{< table id="sample" class="bordered" data-sample=10 >}}
|A|B|
|------|------|
|A|B|
{{</ table >}}

above code will be:

<table id="sample" class="bordered" data-sample="10">
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
</tbody>
</table>


This snippet is licensed under CC0 (publicdomain, waived all copyrights).

@jmfergeau
Copy link

Doesn't seem to work anymore.

parse failed: template: shortcodes/table.html:1: unterminated quoted string

@clvs7-gh
Copy link
Author

Doesn't seem to work anymore.

parse failed: template: shortcodes/table.html:1: unterminated quoted string

I've tested this now on latest hugo (0.84.0) with plain researcher theme ( https://github.com/ojroques/hugo-researcher ), and it's work.
Did you put snippet correctly? Please re-check your code.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment