How can I keep comments in HTML source?

For my use case it is very important for the javascript comments that I have included in my site remain in the HTML source. This is because I am using Hugo to build pages that are then going to have their source examined by people in order to gain insight. (The site I am building is something along the lines of a tuorial site, or a collection of demos and examples.) A site without comments in the source code is a huge disadvantage for me.

So… how can I keep comments in the HTML source?

Thanks!

There is a workaround described in the GitHub issue you also asked this question.

Adding two pence, if you need a one-off comment, it’s doable via a variable in the Front Matter. For example, I’m using that for ASCII art, see this post.

But comments in HTML in bulk is not a common thing people need; you’ll need hacks and workarounds for that. I like that Hugo does the removal of comments automatically.

Thusly:

{{"<!-- some HTML comment //-->" | safeHTML }}

Yes, it is a bit of a clunker.

@royston: But comments in HTML in bulk is not a common thing people need

WTF???

3 Likes

@stiobhart Thanks for the snippet, I didn’t know it’s possible.