3

I got some text in markdown format, I want to return it to my template so it can be displayed as html. For this I have defined a Handlebars helper:

Handlebars.registerHelper('markdown_highlight', function (options) {
  var converter = new Showdown.converter();
  var res = '';
  var html =converter.makeHtml(options.fn(this));
  var high = hljs.highlightAuto(html).value;
  res += high;
  return res;
});

The result comes out formatted but it is displayed directly as html:

pre><code> class Foo(object): def __init__(self, i, j): self.i, self.j = i, j def __str__(self): return “(%d, %d)” % (self.i, self.j) def __setitem__(self, idx, v): if idx == 0: self.i = v elif idx == 1: self.j = v else: raise RuntimeError(“Index out of bounds [0,1]”) </code></pre> <p>Make a subclass of Foo, named Bar, that implements the special methods <strong>eq</strong> and <strong>repr</strong>, such that the following code works: </p> <pre><code>&amp;gt;&amp;gt;&amp;gt; f = Bar(1,2)age 3 &amp;gt;&amp;gt;&amp;gt; g = Bar(2,2) &amp;gt;&amp;gt;&amp;gt; f == g False &amp;gt;&amp;gt;&amp;gt; g == eval(repr(g)) True &amp;gt;&amp;gt;&amp;gt; g[0] = 1 &amp;gt;&amp;gt;&amp;gt; f == g True </code></pre>

What's happening in the helper function is not so important, but someone might be able to help explain how I can make sure the returned html is displayed as html.

Erlend V
  • 518
  • 4
  • 18

3 Answers3

3

Are you are saying that the html is escaped?

If so use the {{{ & }}} instead of {{ & }} in you template. Eg.

{{{markdown_highlight markdown_snippet}}}`

https://stackoverflow.com/a/7173159/236564

Community
  • 1
  • 1
Kyle Finley
  • 11,842
  • 6
  • 43
  • 64
1

Instead of

return res;

Try:

return new Handlebars.SafeString(res);
acemtp
  • 2,971
  • 6
  • 34
  • 43
1
Template['timeline-item'].rendered = ->
d = @find 'code'
if d
    hljs.highlightBlock d

i use these code, this can make hljs work.

and in your template, warp your template context in {{{content}}}.

crapthings
  • 2,485
  • 3
  • 20
  • 33