Ronalds Vilciņš

Hugo shortcode for raw HTML

Sometimes a Markdown file needs one small block of HTML. Enabling raw HTML for the whole site works, but a shortcode makes that exception explicit in the content.

Create layouts/shortcodes/rawhtml.html:

<!-- raw html -->
{{.Inner}}

Then wrap the HTML in your Markdown file with the shortcode:

{{ < rawhtml > }}
  <p>Hello World!</p>
{{ < /rawhtml >}}

Keep the exception small

This shortcode outputs its inner content directly. Never use it for untrusted or user-generated HTML unless that content has been properly sanitized, because it can introduce cross-site scripting vulnerabilities.

Raw HTML also bypasses some of the useful constraints Markdown gives you. Keep the markup semantic, preserve accessibility, and prefer a purpose-built shortcode when the same component appears more than once. I would use this generic shortcode for occasional trusted markup, not as the default way to build content.

Enjoyed reading this?

If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.

← Back to all articles