Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML comments stripped? #263

Closed
jchatard opened this issue Apr 21, 2014 · 16 comments
Closed

HTML comments stripped? #263

jchatard opened this issue Apr 21, 2014 · 16 comments

Comments

@jchatard
Copy link

Are HTML comments stripped by Hugo during rendering?

It's not that I mind so much to remove comments, but for conditional comments, this can be annoying... Don't know if it's a bug, or a feature.

<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

Becomes:

:-)

Thanks,
Jérémy

@ghost
Copy link

ghost commented May 20, 2014

I'm seing the same -- all HTML comments are deleted from the output. And it's clearly a bug because it breaks real-world websites on old IE's. IE Conditional comments are the de-facto way of allowing IE8 to render both HTML5 tags and and responsive CSS.

For the use case of non-output comments, I assume a Go comment in a template tag could be used instead? :

{{/* A suppressed Go comment example */}}

@boushley
Copy link

Unfortunately I'm not sure this is going to be an easy fix.

The Go html/template package purposefully strips comments. You can see a discussion of this here

I think in the meantime you can work around this by using something like::

{{ "<!--[if lt IE 9]>" | safeHtml }}
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
{{ "<![endif]-->" | safeHtml }}

@boushley
Copy link

@spf13 would you welcome a PR that added more escaping options? Something like a safeJs function?

@spf13
Copy link
Contributor

spf13 commented May 21, 2014

Yes. Please also add documentation for using them.

On Wednesday, May 21, 2014, Aaron Boushley notifications@github.com wrote:

@spf13 https://github.com/spf13 would you welcome a PR that added more
escaping options? Something like a safeJs function?


Reply to this email directly or view it on GitHubhttps://github.com//issues/263#issuecomment-43797091
.

Steve Francia
http://stevefrancia.com
http://spf13.com
http://twitter.com/spf13

@ghost
Copy link

ghost commented May 24, 2014

I can confirm that the safeHtml solution by @boushley works. Maybe documenting that workaround is the safest and quickest solution...

@spf13
Copy link
Contributor

spf13 commented Jun 3, 2014

I agree that we should add documentation for this. @jesper-mortensen or @boushley, would either of you like to add it, since you've been successfully using it?

@jononomo
Copy link

What can I do if I don't want comments stripped out?

@dimo414
Copy link
Contributor

dimo414 commented Jan 27, 2016

Has this changed? I'm now seeing comments in my HTML output.

@spf13
Copy link
Contributor

spf13 commented Jan 27, 2016

@dimo414 I'm not aware of any changes made to the Go template library. What version of Hugo are you using? What Go is it built with?

@dimo414
Copy link
Contributor

dimo414 commented Jan 29, 2016

I'm using Hugo 0.15, via the hugo_0.15_windows_amd64.zip Windows binary. Demo:

$ hugo version
Hugo Static Site Generator v0.15 BuildDate: 2015-11-25T14:35:20-05:00

$ cat content/post/demo.md
+++
date = "2014-08-10"
title = "demo"

+++

This is text

<!-- this is a comment -->

$ hugo --theme hyde
0 draft content
0 future content
1 pages created
1 paginator pages created
0 categories created
0 tags created
in 13 ms

$ cat public/post/demo/index.html | grep comment
<!-- this is a comment -->

@mbertschler
Copy link

Okay so I just discovered that the current state of things is that comments are stripped from template files, but not stripped from content files.

@dimo414
Copy link
Contributor

dimo414 commented May 9, 2016

@spf13 is this desired functionality? Could comments be stripped from content files as well?

@mbertschler
Copy link

@dimo414 as far as I can se Hugo uses the blackfriday for markdown rendering. From looking at its documentation it does not provide an option to strip comments, as it probably just treats them like inline HTML.

@ernsheong
Copy link

A note for onlookers, safeHtml has been renamed to safeHTML in Hugo 0.14.

@behindthepage
Copy link

@ernsheong thanks for your last comment. It saved me time and stress. Really appreciated it.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants