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.
Related articles
- Customizing Hugo’s RSS Feed
Adjust Hugo RSS titles, descriptions, content, and section filtering while keeping the generated feed valid and easy to discover.
- Organizing Hugo Content With Tags and Categories
Organize Hugo content with a small, deliberate set of tags, categories, or custom taxonomies that lead to useful archive pages.
- A More Useful 404 Page in Hugo
Turn Hugo's 404 page into a useful route back to the homepage, search, or content visitors may have wanted.