Hi guys,
I recently came upon a tricky scenario where I need to mark a block as plain-text and prevent hugo from interpretting it.
Eg, say we have defined a shortcode “code” for highlighting some code block. The following is the definition
Since you’re using the {{< >}} syntax, Hugo will pass everything in the body through unmodified, as raw HTML.
So, the example you posted will (correctly) generate:
.Inner is a template.HTML (a type), which basically means that Go’s template library won’t worry about escaping it. The printf "%s" .Inner transforms .Inner into a regular string, so the template library does escape it. (For the inverse, see safeHTML in the Hugo docs, under Templates > Functions)
You may want to explore using fenced code blocks, which can indicate the language used. See “Fenced Code Blocks” on the Blackfriday page. They produce <pre><code class="language-html"> ... </code></pre>
If you want to talk about shortcodes, there’s a(n apparently undocumented) way to escape the shortcode processing: {{</* code */>}} will be passed through as {{< code >}} and will not be processed. See the source of the Hugo template function docs for example usage.
It is undocumented, yes. Documenters welcome! I added it to be able to document the shortcodes and the syntax is very similar to Go’s text template comments.