Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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).

@maxlefou
Copy link

maxlefou commented Jun 22, 2021

Doesn't seem to work anymore.

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

@clvs7-gh
Copy link
Author

clvs7-gh commented Jun 22, 2021

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