How to comment out content in Hugo
Asked Answered
I

4

26

How do I comment out content in Hugo?

If I have notes, unfinished thoughts, I'd like to leave them in the .md file but not have them appear in the html.

<!-- tags don't seem to work -- it doesn't even become a html comment, it remains visible text on the page.

Inhabitant answered 20/7, 2017 at 20:39 Comment(3)
<!-- the text -->Bolden
ah, needs the closing --> tag thanksInhabitant
No problem man....glad to help :)Bolden
F
13

See this example for a no-op shortcode that can be used to add comments in content files:

https://github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/todo.html

Friulian answered 22/7, 2017 at 8:45 Comment(3)
Go comments didn't work for me as they got rendered anyway. But creating a custom shortcode like this worked excellent.Coumas
The link is broken. This is a good example of why SO tends to hold link-only responses in low regard. If anyone knows the answer, please add more details.Praiseworthy
Yes, the link is still broken.Kamseen
R
22

Hugo uses Go templates under the hood, so the comment syntax is {{/* a comment */}}. Anything inside the comment will not be rendered to the resulting HTML files.

Official Hugo comments documentation

Rey answered 23/1, 2019 at 12:22 Comment(4)
This still renders the text between {{/* and */}} in the HTML, at least in the test case I had with multi-line comment text.Eldreeda
@gbmhunter: Sounds strange. I use it all the time without issues. Also for multi-line comments.Flitter
The op is asking about within markdown files it seems, I had the same issueAndria
@ClarenceLiu: That's actually a good point. :) Had not read it that way.Flitter
S
16

Official doc says, HTML comment does not rendered to final HTML pages.

HTML comments are by default stripped, but their content is still evaluated. That means that although the HTML comment will never render any content to the final HTML pages, code contained within the comment may fail the build process.

https://gohugo.io/templates/introduction/#html-comments-containing-go-templates

So you can use HTML comment like this

<!-- your comment text -->

Confirmed on v0.55.0

Sandarac answered 30/7, 2019 at 7:26 Comment(1)
This does work only in templates but not in the content. The linked reference also points into the templating section. (hugo v0.104.3)Hierarchize
F
13

See this example for a no-op shortcode that can be used to add comments in content files:

https://github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/todo.html

Friulian answered 22/7, 2017 at 8:45 Comment(3)
Go comments didn't work for me as they got rendered anyway. But creating a custom shortcode like this worked excellent.Coumas
The link is broken. This is a good example of why SO tends to hold link-only responses in low regard. If anyone knows the answer, please add more details.Praiseworthy
Yes, the link is still broken.Kamseen
M
5

As the link for the no-op shortcode in bep's answer seems to be broken, I will post my solution of a comment shortcode here for anyone who gets to this in the future:

You can simply add a shortcode, lets name it, for example, "comment.html". We want to use a paired shortcode (i.e., one with a starting and closing shortcode) and put our comment inside. To achieve this, the shortcode must evaluate the .Inner variable. If .Inner is not evaluated, Hugo failes to build. Since we do not want the .Inner text in our HTML output, we can for example store it in a variable or evaluate it in a condition.

Therefore the shortcode file "comment.html" could contain something like:

{{ if .Inner }}{{ end }}

and in your markdown, you can use it as follows:

{{< comment >}}
this is a comment
{{< /comment >}}

The resulting HTML will not contain the comment in its source code.

Mallissa answered 12/12, 2023 at 13:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.