1

我写了这个插件:

module Jekyll
    module Tags
        class Prism < Liquid::Block
            def initialize(tag_name, text, tokens)
                @arg = text.strip
                super
            end

            def render(context)
                output = super(context)
                "<pre><code class=\"language-#{@arg}\">#{output}</code></pre>"
            end
        end
    end
end 

Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)

这就是我使用它的方式:

{% prism cpp %}
#include <iostream>

// Hello World
int main()
{
    cout << "hello world" << endl;
    int a = 10;
}
{% endprism %}

现在,问题是,我主要在我的网站上使用 C++ 代码。当我现在使用 Jekyll 生成此降价时,之后的所有文本{% endprism %}仍将在<pre>标记内,因为<iostream>如果我转义它,Kramdown 会感到困惑,(\<iostream\>),然后我的插件按预期工作,但我的 Javascript 荧光笔变得混乱。

如果不启用 Jekyll 的荧光笔,我该如何解决这种情况?

4

2 回答 2

0

我认为您尝试使用GitHub Flavored Markdown中使用的围栏代码块。

为什么不使用开箱即用的Jekyll代码高亮cpp功能呢?此处提供了用于突出显示的基本 css

尝试 :

{% highlight cpp %}
#include <iostream>

// Hello World
int main()
{
    cout << "hello world" << endl;
    int a = 10;
}
{% endhighlight %}
于 2015-05-10T14:47:06.093 回答
0

CGI 模块中有一个函数,它允许转义 HTML,就像 PHP 中的 htmlspecialchars 一样。我将液体插件更改为这个并且它可以工作:

require 'cgi'

module Jekyll
    module Tags     
        class Prism < Liquid::Block
            def initialize(tag_name, text, tokens)
                @arg = text.strip
                super
            end

            def render(context)
                output = super(context)
                output = CGI.escapeHTML(output);
                "<pre><code class=\"language-#{@arg}\">#{output}</code></pre>"
            end
        end
    end
end 

Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)

它将所有 <> 转义为 html 特殊字符。因此,Kramdown 不会感到困惑,prism.js 仍然能够正确突出显示代码。

高亮截图

于 2015-05-10T18:45:09.367 回答