4

I am trying to get my metalsmith setup working, nothing fancy.

In my build.js I have:

…
handlebars.registerHelper('doctype', function() {
  return new handlebars.SafeString('<!DOCTYPE html>');
});
…

in my partial template header.hbt I have:

{{doctype}}
<html>
…

And the result html begins with:

<p>&lt;!DOCTYPE html&gt;</p>

– which is obviously not what I need.

Any suggestions?

PS. Using a plain doctype definition in my layout file had the same effect. Using handlebars ^4.0.5.

Edit 1: After trying around a bit, i can supply another symptom: The result of a SafeString() call seems to be wrapped in a <p> tag if it is placed before the <html> block. This is not specific to the doctype declaration, »normal« html elements seem to be affected as well.

Edit 2: Now i have distilled 2x2 cases for the functioning of safeString():

a) a conventional element (e.g. <div>) inside the <html> block gets rendered, as expected, without changes.

b) a conventional element before the <html> block gets rendered ok, but is placed inside a <p> tag.

c) a doctype element inside the <html> block gets rendered as pure text, like this: &lt;!DOCTYPE html&gt;

d) a doctype element before the <html> block will be rendered in the same manner but wrapped in a <p> element.

  • 2
    guess what SafeString was made for – GottZ Apr 07 '16 at 12:06
  • https://github.com/assemble/handlebars-helpers/blob/master/docs/helpers/html/helper-doctype.md – GottZ Apr 07 '16 at 12:13
  • Sorry, didn't see your second comment! – Dimitar Roszenov Ruszev Apr 07 '16 at 12:28
  • well to explain the issue here: handlebars.SafeString will return a string that you can use -safely- inside html by threating it as text rather html – GottZ Apr 07 '16 at 12:51
  • The suggested library looks promising, thanks! However, it doesn't seem trivial integrating it into my setup. Moreover, i would like to understand what is wrong with my code, since i have copy/pasted it from a documentation site. – Dimitar Roszenov Ruszev Apr 07 '16 at 13:36
  • @GottZ thanks for the insight! Yes, you are right, in order for html to render correctly it must be interpreted / copied as string. But in this case it escapes it, which is the role of the `escapeExpression()` function. I am still not sure, if it is a `handlebars` or `metalsmith` specific problem. Also, i couldn't get `handlebar-helpers` to work with my setup. – Dimitar Roszenov Ruszev Apr 07 '16 at 19:56

1 Answers1

2

Seems to be a metalsmith-related matter, not inherent to handlebars. A given (erroneous!) order of plugin invocations causes the issue: calling use(layouts(…)) before calling use(markdown(…)) is the cause. (so probably the markdown plugin does the wrapping.) Sorry for the trouble.