讓 Markdown-On-Save-Improved 支援 Github 的 Fenced code blocks
修改 markdown-on-save.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected function process( $content, $id ) { $this->maybe_load_markdown(); // Links with "quoted titles" bork without stripping slashes. // Stripslashes would lose desired backslashes. So, regex. $content = preg_replace( '/\\\"/', '"', $content ); // Added by Starck Lin - parsing Github Fenced code blocks $content = preg_replace("/\n```([A-z :]+)([^```]+?)\n```/", '<pre class="lang:$1">$2</pre>', $content ); $content = preg_replace("/\n```([^```]+?)\n```/", '<pre>$1</pre>;', $content ); // convert to Markdown $content = $this->parser->transform( $content ); // reference the post_id to make footnote ids unique $content = preg_replace( '/fn(ref)?:/', "fn$1-$id:", $content ); return $content; } |
這是搭配 Crayon WP-Plugin, 如果遇到原始碼有 </pre> 的問題, 可以轉成
<
(<) 和>
(>), 再把 crayon 的 decode 設為 true.另外有個值得注意的點, 換行符號我是用雙引號來轉換 \n