SyntaxHighlighter 被错误地解析
SyntaxHighlighter gets parsed incorrectly
我的 SyntaxHighlighter 解析错误,将带有 <pre class="brush: lang">...</pre>
的代码块插入到 Markdown 中,例如:
情况一:删除</body>
`</body>` parse as ``
案例二:配对不正确
<pre class="brush: java">
private class MaxPropComparator implements Comparator<message> {
...
}
</pre>
解析为(添加</message>
):
<pre class="brush: java">
private class MaxPropComparator implements Comparator<message> {
...
}
</message></pre>
案例三:嵌入代码块
<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>
因此显示为:
案例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>
原因:
PROBLEMS: Major issue with this method is that all left angle brackets must be HTML escaped, eg all <
must be replaced with <
This will ensure correct rendering.
我的解决方案: 将 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>
我的 SyntaxHighlighter 解析错误,将带有 <pre class="brush: lang">...</pre>
的代码块插入到 Markdown 中,例如:
情况一:删除</body>
`</body>` parse as ``
案例二:配对不正确
<pre class="brush: java">
private class MaxPropComparator implements Comparator<message> {
...
}
</pre>
解析为(添加</message>
):
<pre class="brush: java">
private class MaxPropComparator implements Comparator<message> {
...
}
</message></pre>
案例三:嵌入代码块
<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>
因此显示为:
案例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>
原因:
PROBLEMS: Major issue with this method is that all left angle brackets must be HTML escaped, eg all
<
must be replaced with<
This will ensure correct rendering.
我的解决方案: 将 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>