解决Tinection下markdown代码块显示问题

使用Tinection,发现使用markdown写文章插入代码会在代码前后加入 如下

1
2
3
4
5
6
7
<code>
public static void main(String[] args) {
// TODO Auto-generated method stub
long num=Math.round(-2.6);
System.out.println(num);
}
</code>

网上提供了一种方法修改markdown插件里的文件 比如修改jetpack-markdown/lib/markdown/extra.php 中的 $codeblock = "<pre><code>$codeblock\n</code>\</pre>”; $codeblock = "<pre$pre_attr_str><code>$codeblock</code>\</pre>”; 这2处删除 标签即可。还有下面这个

1
2
3
4
5
function _doFencedCodeBlocks_callback($matches) {
$classname =& $matches[2];
$attrs =& $matches[3];
$codeblock = $matches[4];
$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);

找到这个地方,注释掉最后一行,这里是禁止把 < 和 > 这两个符号转化为 html 的尖括号。 这种情况下可以解决在线markdown发表文章产生标签 但是这是一种治标不治本的问题,经过大量代码修改测试,发现修改一个地方就可以解决。。。 在模板函数 (functions.php)找到以下代码

1
2
3
function pre_content_filter( $content ) {
return preg_replace_callback( '<pre.*>(.*)</preisU' , 'convert_pre_entities', $content );
}

修改为

1
2
3
function pre_content_filter( $content ) {
return preg_replace_callback( '<pre.*><code.*>(.*)</code></preisU' , 'convert_pre_entities', $content );
}

即可解决问题


上文更新完后发现严重的字符转义现象,经过仔细查证 在模板函数 (functions.php)找到以下代码

1
2
3
4
function convert_pre_entities( $matches ) {
$code = str_replace( $matches[0], '<div class="precode clearfix"><pre class="prettyprint linenums">'.htmlspecialchars( $matches[1],ENT_COMPAT,'UTF-8' ).'</pre></div', $matches[0] );
return str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $code);
}

修改为

1
2
3
4
function convert_pre_entities( $matches ) {
$code = str_replace( $matches[0], '<div class="precode clearfix"><pre class="prettyprint linenums">'.$matches[1].'</pre></div', $matches[0] );
return str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $code);
}

即可解决代码转义问题