HTML comment handling regression between 0.55.6 and 0.54.0

I was looking for ways to retain html comments in the generated output and found it is a limitation in golang’s html/template library. However, I found that using a {{ ` (backtick) before many lines of html in my baseof.html file, followed by ` (backtick) | safeHTML }} before the {{ block “main” . }} {{ end }} code in the file would retain comments in the first section using both version 0.54.1 and 0.55.6. Repeating this technique after the the main block in the remainder of the template works in version 0.54.1 but not in 0.55.6 where comments are stripped.

This technique is a small addition to what is trusted template content and it would be very desirable if it could be continued.

Another curiosity is that the content inserted in the main block retains its comments without prior or following use of the backtick technique in both versions, so that partial must be considered trusted by Hugo. Perhaps the code used to insert the partial will give a clue on what changed.

Hi @crusty. I’m not familiar with the way you currently write HTML comments so I won’t comment on that.

But if you don’t find a solution to your question, you can always write comments like:

{{ print "<!-- comment -->" | safeHTML }}

I am aware of that option, but a purchased template has comments on nearly every other line and I want to preserve the content as-is. It makes file comparison after update much easier.

I see. Would you show us an example of how the comments are currently written?

This is an extreme example:

\<!DOCTYPE html>
\<html lang="en">
\<head>
	\<!-- set the encoding of your site -->
	\<meta charset="utf-8">
	\<!-- set the viewport width and initial-scale on mobile devices -->
	\<meta name="viewport" content="width=device-width, initial-scale=1.0">
	\<!-- set the HandheldFriendly -->
	\<meta name="HandheldFriendly" content="True">
	\<!-- set the description -->
	\<meta name="description" content="STUDYLMS HTML Template">
	\<!-- set the Keyword -->
	\<meta name="keywords" content="">
	\<meta name="author" content="STUDYLMS HTML Template">
	\<!-- set the page title -->
	\<title>STUDYLMS HTML Template</title>
	\<!-- include google roboto font cdn link -->
	\<link href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7COpen+Sans:300,300i,400,400i,600,600i,700,700i" rel="stylesheet">
	\<!-- include the site bootstrap stylesheet -->
	\<link rel="stylesheet" href="css/bootstrap.css">
	\<!-- include the site stylesheet -->
	\<link rel="stylesheet" href="css/plugins.css">
	\<!-- include the site stylesheet -->
	\<link rel="stylesheet" href="css/colors.css">
	\<!-- include the site stylesheet -->
	\<link rel="stylesheet" href="style.css">
	\<!-- include the site responsive stylesheet -->
	\<link rel="stylesheet" href="css/responsive.css">
\</head>

The rest of the template gets more sensible, but the point remains that just a couple of inserts solve the issue.

@crusty I suspect this to be a regression in Go; could you create a GitHub issue with a small example?

As a side tip: If you fully trust your HTML templates, you can redefine the HTML output format and set isPlainText = true which will use the much leaner text template parser.

2 Likes

TIL this ^

Setting config.yaml to include:

outputFormats :
  html :
    isPlainText : true

lets me avoid adding anything to the template except for the partials, so that is a better solution.

Of course, it begs the question: what features in the template would I not trust, requiring to be escaped? The template started as a pure html file and will have script inclusions at the bottom beyond the four there already.

That is a good question. The general answer would be “templates you get from others who you do not trust”.

Well, there are just two of us working on this application, so I think I can trust the developers.

I am satisfied with using the isPlainText : true technique and consider this issue solved. I leave the regression fixing to better programmers than me.

Thanks,
Chris

I wrote too quickly. Version 0.55.6 goes through the motions but does not write the generated files to the output directory. Reverting to version 0.54.0 (not .1 as I thought before) results in generated files as expected. No changes in any configuration or content.

Another day’s work and I have success with version 0.55.6 but the technique is very different. I needed to define new entries in config.yaml:

outputFormats :
  NOESC :
    isPlainText : true
    mediaType: text/html

outputs : 
  page :
   - HTML
   - NOESC

There is nothing special about NOESC (implying no html escaping needed). I am not sure if it needs to be in all capitals since lower case is used in the template file name.

Then, in the front matter of .html content pages intended to retain HTML comments add the following:

outputs : NOESC

Finally, a default template file must be named differently:

~/layouts/_default/list.noesc.html

If you are using a section layout then the same filename can be used in the relevant section sub-directory.

I hope this helps other readers in the future.

Chris