SyntaxHighlighter被错误地解析

时间:2016-01-15 18:00:29

标签: syntax-highlighting syntaxhighlighter

我的SyntaxHighlighter被错误地解析,将带有<pre class="brush: lang">...</pre>的代码块插入到Markdown中,例如:

案例1:删除</body>

`</body>` parse as ``

案例2:配对错误

<pre class="brush: java">
private class MaxPropComparator implements Comparator<message> {
    ...
}
</pre>

被解析为(添加</message>):

<pre class="brush: java">
private class MaxPropComparator implements Comparator<message> {
    ...
}
</message></pre>

案例3:嵌入式代码块

<pre class="brush: xml; smart-tabs: false">
<pre class="brush: js; highlight: [2, 4, 6]">
...
</pre>
</pre>

被解析为:

<pre class="brush: xml; smart-tabs: false">
</pre><pre class="brush: js; highlight: [2, 4, 6]">
...
</pre>

因此显示为:

enter image description here

案例4:等等

# messages: event-type org-node (loc-x,loc-y) (anchor-x,anchor-y) r=<core> a=<availability> ttl size [node-snd ( loc-x loc-y ) [node-rcv (     loc-x loc-y)]]

被解析为(添加</availability></core>):

# messages: event-type org-node (loc-x,loc-y) (anchor-x,anchor-y) r=<core> a=<availability> ttl size [node-snd ( loc-x loc-y ) [node-rcv (     loc-x loc-y)]]
</availability></core>

1 个答案:

答案 0 :(得分:0)

原因:

  

问题:此方法的主要问题是所有左尖括号都必须进行HTML转义,例如,所有<必须替换为&lt;这将确保正确呈现。

我的解决方案:SyntaxHighlighter转移到Google Code Prettify,并使之前的代码与Google Code Prettify兼容。这是我的工作:

<script>
jQuery(document).ready(function(){
    jQuery('pre').each(function(){
        var el = jQuery(this).find('code');
        var code_block = el.html(); // remove the pairwise tag of <code></code>
        var lang = el.attr('class');

        if (el.length > 0) { // <pre>...</pre> with <code>...</code> inside
            if (lang) {
                jQuery(this).addClass('prettyprint linenums lang-' + lang).html(code_block);
            } else {
                jQuery(this).addClass('prettyprint linenums').html(code_block);
            }
        } else { // <pre>...</pre> without <code>...</code> inside
            jQuery(this).removeClass().addClass('prettyprint linenums'); // take over SyntaxHighlighter
        }
    });
});
</script>
相关问题