Skip to content

Instantly share code, notes, and snippets.

@exkuretrol
Last active January 26, 2023 16:44
Show Gist options
  • Save exkuretrol/b58cb3592b5cc59a1098423b682d7ce5 to your computer and use it in GitHub Desktop.
Save exkuretrol/b58cb3592b5cc59a1098423b682d7ce5 to your computer and use it in GitHub Desktop.
A hugo shortcode table, have 3 optional arguments, id, class and title.
{{ $htmlTable := .Inner | markdownify }}
{{ $label := i18n "table_label" | default "Table" }}
{{ $tag := "<table>" }}
{{ $newTag := "<table" }}
{{ with .Get "id" }}
{{ $newTag = printf "<table id='%s'" . }}
{{ end }}
{{ with .Get "class" }}
{{ $newTag = print $newTag " " . }}
{{ end }}
{{ $newTag = print $newTag " itemscope itemtype='https://schema.org/Table'>" }}
{{ with .Get "title" }}
{{ $caption := printf "<caption itemprop='about'><strong>%s</strong> %s</caption>" $label . }}
{{ $newTag = print $newTag $caption }}
{{ end }}
{{ $htmlTable := replace $htmlTable $tag $newTag }}
{{ $htmlTable | safeHTML }}
{{/* inspire by https://gist.github.com/djibe/7a8ba9516f4495dbd6fdf1d1de7a60fe#file-hugo-shortcode-table-html and improved by kuaz */}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment