【问题标题】:Preformatting HTML and displaying it in template预格式化 HTML 并在模板中显示
【发布时间】:2012-11-24 12:09:48
【问题描述】:

我得到了一些 Markdown 格式的文本,我想将它返回到我的模板中,以便它可以显示为 html。为此,我定义了一个 Handlebars 助手:

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;
});

结果是格式化出来的,但是直接显示为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>

辅助函数中发生的事情并不那么重要,但有人可能会帮助解释我如何确保返回的 html 显示为 html。

【问题讨论】:

    标签: syntax-highlighting markdown meteor handlebars.js


    【解决方案1】:

    你是说html被转义了?

    如果是这样,请在模板中使用 {{{}}} 而不是 {{}}。例如。

    {{{markdown_highlight markdown_snippet}}}`
    

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

    【讨论】:

      【解决方案2】:

      代替

      return res;
      

      试试:

      return new Handlebars.SafeString(res);
      

      【讨论】:

        【解决方案3】:
        Template['timeline-item'].rendered = ->
        d = @find 'code'
        if d
            hljs.highlightBlock d
        

        我使用这些代码,这可以使 hljs 工作。

        在您的模板中,在 {{{content}}} 中扭曲您的模板上下文。

        【讨论】:

          猜你喜欢
          • 2016-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-23
          相关资源
          最近更新 更多