除了Markdown編輯器,你還需要會用程序來處理它
前言
隨著 wordpress 和靜態(tài)網(wǎng)站的流行,markdown 被用的越來越多。我們已經(jīng)介紹過很多 Markdown 編輯器,但是有時(shí)候你也需要用程序來處理 Markdown 文本。
markdown 是一個(gè)面向?qū)懽鞯恼Z法引擎,markdown 的最終目的都是解析成 html 用于網(wǎng)頁瀏覽,所以它兼容 html 語法,即你可以在 markdown 文檔中使用原生的 html 標(biāo)簽。
markdown 解析器
開發(fā)靜態(tài)網(wǎng)站生成器的時(shí)候都會采用一種叫 front matter 的格式進(jìn)行網(wǎng)站內(nèi)容寫在類似下面的格式
- ---
- title: 玩轉(zhuǎn)markdown,你需要用到這幾個(gè)工具
- date: 2016-08-14 16:44:54
- image: /img/pencils-762555_640.jpg
- ---
- ## 前言
- 隨著wordpress和靜態(tài)網(wǎng)站的流行,markdown被用的越來越多。...
當(dāng)進(jìn)行網(wǎng)站生成的時(shí)候需要進(jìn)行 markdown 解析,然后渲染成 html 頁面,那用什么工具進(jìn)行解析呢?
marked
marked 是最早用 node.js 開發(fā)的 markdown 解析器,同時(shí)提供 CLI 命令調(diào)用和 node.js API 調(diào)用。
CLI 調(diào)用代碼示例
- $ marked -o hello.html
- hello world
- ^D
- $ cat hello.html
- <p>hello world</p>
API調(diào)用示例
- var marked = require('marked');
- console.log(marked('I am using __markdown__.'));
- // Outputs: <p>I am using <strong>markdown</strong>.</p>
這些都是一些通用的功能,但是 marked 還支持代碼高亮,通過使用 highlight.js。
使用 highlight.js 進(jìn)行代碼高亮相信大家都用到過,可能大家不知道是 highlight.js 還支持 API 方式調(diào)用,下面的代碼會配置 marked 使用 highlight.js 進(jìn)行代碼高亮:
- marked.setOptions({
- highlight: function (code, lang) {
- var res;
- if (lang) {
- res = hljs.highlight(lang, code, true).value;
- } else {
- res = hljs.highlightAuto(code).value;
- }
- return res;
- }
- });
生成的代碼已經(jīng)包含代碼高亮標(biāo)簽,最后只需要引入 highlight.js 的主題就能顯示了,highlight.js 所有的顏色主題都在這里
markdown-js
markdown-js 也是一款使用 node.js 開發(fā)的 markdown 解析器,基本用法和 marked 差不多,但是文檔里面好像沒有提到像 marked 一樣進(jìn)行代碼高亮生成的接口,有興趣的同學(xué)自己找找吧。
markdown 生成器
to-markdown
什么是 markdown 生成器,就是根據(jù) html 標(biāo)簽生成 markdown 文件。
github 上面 markdown 生成器 star 數(shù)最高的是 to-markdown。
簡單的代碼示例
- var toMarkdown = require('to-markdown');
- toMarkdown('<h1>Hello world!</h1>');
to-markdown 最近進(jìn)行了更新,增加了對 gfm 的兼容,gfm 就是 git flavored markdown 的意思, 是 github 對 markdown 語法進(jìn)行的擴(kuò)展。
使用 gfm 的示例
- toMarkdown('<del>Hello world!</del>', { gfm: true });
那這個(gè) to-markdown 有什么用呢?
舉個(gè)簡單的例子,假如我想開發(fā)一個(gè)簡單的 RSS 閱讀器,但是我又不想跳轉(zhuǎn)到目標(biāo)網(wǎng)站去閱讀,因?yàn)椴煌木W(wǎng)站風(fēng)格不一,導(dǎo)致不一致的閱讀體驗(yàn)。
怎么辦呢?那就把網(wǎng)站內(nèi)容抓取下來,然后用 to-markdown 生成 markdown 文件,然后使用自己的模板樣式進(jìn)行統(tǒng)一渲染。
當(dāng)然去除廣告只是一個(gè) side effect。
heckyesmarkdown
除了 to-markdown 之外還有一個(gè)比較好用的 API: heckyesmarkdown,這個(gè)項(xiàng)目使用了 php-readability,提高文章的可讀性。
可惜 heckyesmarkdown 沒有開源出來,這個(gè)項(xiàng)目有點(diǎn)古老,估計(jì)那個(gè)時(shí)候 github 還沒流行起來。
heckyesmarkdow 對中文的支持不是非常友好,如果想抓取中文站還是使用 to-markdown 比較靠譜一點(diǎn)。
front matter
markdown 寫文章確實(shí)很方便,簡單容易上手,但是 markdown 不能保存元數(shù)據(jù),例如作者,日期,類型這樣的結(jié)構(gòu)化的數(shù)據(jù),如果都生成 html 標(biāo)簽的話提取的時(shí)候又稍微麻煩了點(diǎn), 還得借助 cheerio 才能完成。
所以,為了能方便的保存文章的元數(shù)據(jù),幾乎所有的靜態(tài)網(wǎng)站生成器都使用 front matter 格式來保存文章。
front matter 文件通常分為頭部和正文部分,頭部一般使用 yaml、toml 和 json 三種格式,front matter 解析工具需要識別這三種格式的文件頭。正文部分就是普通的 markdown 內(nèi)容。
front-matter 也是用 node.js 開發(fā)的,相比 markdown 解析器來說,fornt-matter 解析器要簡單很多。
示例文件 example.md
- ---
- title: Just hack'n
- description: Nothing to see here
- ---
- This is some text about some stuff that happened sometime ago
解析代碼
- var fs = require('fs')
- , fm = require('front-matter')
- fs.readFile('./example.md', 'utf8', function(err, data){
- if (err) throw err
- var content = fm(data)
- console.log(content)
- })
解析結(jié)果
- {
- attributes: {
- title: 'Just hack\'n',
- description: 'Nothing to see here'
- },
- body: '\nThis is some text about some stuff that happened sometime ago',
- frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here'
- }
front matter 雖然格式看起來不太統(tǒng)一,卻是對 markdown 強(qiáng)有力的補(bǔ)充。