labnotes

Styling tables with Hugo

Markdown sometimes isn’t enough

tags
bootstrap
hugo

Markdown is a nice format to write it, but sometimes you need to add HTML classes directly to the output to make it look how you want. Here’s a way to do that using hugo shortcodes.

Create a table shortcode

Put this in /layouts/shortcodes/table.html:

1
2
3
4
5
{{ $htmlTable := .Inner | markdownify }} {{ $class := .Get 0 }} {{ $old := "
<table>
  " }} {{ $new := printf "<table class=\"%s\">" $class }} {{ $htmlTable :=
  replace $htmlTable $old $new }} {{ $htmlTable | safeHTML }}
</table>

Create a table

Like this:

1
2
3
4
| Header 1 | Header 2 | Header 3 |
| -------- | -------- | -------- |
| Item 1   | Item 2   | Item 3   |
| Item 1a  | Item 2a  | Item 3a  |

Which should render like so:

Header 1Header 2Header 3
Item 1Item 2Item 3
Item 1aItem 2aItem 3a

Wrap it in shortcodes

{{<table “table table-striped table-bordered”>}}
Item 1 Item 2 Item 3
Item 1a Item 2a Item 3a
{{</table>}}

Which should render:

Header 1Header 2Header 3
Item 1Item 2Item 3
Item 1aItem 2aItem 3a

References

  1. https://zwbetz.com/style-a-markdown-table-with-bootstrap-classes-in-hugo/

Previously

howto

Emacs Tramp tricks

Replacing terminals with emacs

tags
emacs
tramp
docker

Next

labnotes

Book Image Shortcode for Hugo

Lets link to bookshop

tags
hugo