这两天想着把博客的文章内容改为使用markdown编写,就可以找nodejs下的markdown解析器,比较推荐的是下面两个
github: https://github.com/chjj/marked
使用示例:
javascriptconst markdown = require('marked');
const convertedHTML = markdown('#this is h1');
github: https://github.com/evilstreak/markdown-js
使用示例:
javascriptconst markdown = require('markdown').markdown;
const convertedHTML = markdown.toHTML('#this is h1');
上次两种都可以达到解析markdown文本的目的,但是支持的语法都偏少。同时最重要的,由于我的博客采用vuejs编写,想要为代码着色就产生了很大的困难。尝试了SyntaxHighlighter和google-code-prettify都不能令人满意。google-code-prettify是最终成功了的,但是是直接引入的js。对于追求完美的我,还是差那么一点意思。 无奈在github上找终于找到了我们的主角markdown-it
github: https://github.com/markdown-it/markdown-it
使用示例:
javascriptconst markdown= require('markdown-it')();
const convertedHTML = markdown.render('#this is h1');
最关键的可以和highlight.js结合使用
javascriptconst hljs = require('highlight.js');
const md = require('markdown-it')({
highlight(str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return `<pre class="hljs"><code>${hljs.highlight(lang, str, true).value}</code></pre>`;
} catch (__) {
// nothing to do
}
}
return ''; // use external default escaping
},
});
同时支持插件,文档上没有细说,这个还需要深入了解下。
本文作者:谭三皮
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!