日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
除了Markdown編輯器,你還需要會用程序來處理它

前言

創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計制作、做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)施甸,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108

隨著 wordpress 和靜態(tài)網(wǎng)站的流行,markdown 被用的越來越多。我們已經(jīng)介紹過很多 Markdown 編輯器,但是有時候你也需要用程序來處理 Markdown 文本。

markdown 是一個面向?qū)懽鞯恼Z法引擎,markdown 的最終目的都是解析成 html 用于網(wǎng)頁瀏覽,所以它兼容 html 語法,即你可以在 markdown 文檔中使用原生的 html 標(biāo)簽。

markdown 解析器

開發(fā)靜態(tài)網(wǎng)站生成器的時候都會采用一種叫 front matter 的格式進行網(wǎng)站內(nèi)容寫在類似下面的格式

 
 
  1. ---
  2. title: 玩轉(zhuǎn)markdown,你需要用到這幾個工具
  3. date: 2016-08-14 16:44:54
  4. image: /img/pencils-762555_640.jpg
  5. ---
  6. ## 前言
  7. 隨著wordpress和靜態(tài)網(wǎng)站的流行,markdown被用的越來越多。...

當(dāng)進行網(wǎng)站生成的時候需要進行 markdown 解析,然后渲染成 html 頁面,那用什么工具進行解析呢?

marked

marked 是最早用 node.js 開發(fā)的 markdown 解析器,同時提供 CLI 命令調(diào)用和 node.js API 調(diào)用。

CLI 調(diào)用代碼示例

 
 
  1. $ marked -o hello.html
  2. hello world
  3. ^D
  4. $ cat hello.html
  5. hello world

API調(diào)用示例

 
 
  1. var marked = require('marked');
  2. console.log(marked('I am using __markdown__.'));
  3. // Outputs: 

    I am using markdown.

這些都是一些通用的功能,但是 marked 還支持代碼高亮,通過使用 highlight.js。

使用 highlight.js 進行代碼高亮相信大家都用到過,可能大家不知道是 highlight.js 還支持 API 方式調(diào)用,下面的代碼會配置 marked 使用 highlight.js 進行代碼高亮:

 
 
  1. marked.setOptions({
  2.   highlight: function (code, lang) {
  3.     var res;
  4.     if (lang) {
  5.       res = hljs.highlight(lang, code, true).value;
  6.     } else {
  7.       res = hljs.highlightAuto(code).value;
  8.     }
  9.     return res;
  10.   }
  11. });

生成的代碼已經(jīng)包含代碼高亮標(biāo)簽,最后只需要引入 highlight.js 的主題就能顯示了,highlight.js 所有的顏色主題都在這里

markdown-js

markdown-js 也是一款使用 node.js 開發(fā)的 markdown 解析器,基本用法和 marked 差不多,但是文檔里面好像沒有提到像 marked 一樣進行代碼高亮生成的接口,有興趣的同學(xué)自己找找吧。

markdown 生成器

to-markdown

什么是 markdown 生成器,就是根據(jù) html 標(biāo)簽生成 markdown 文件。

github 上面 markdown 生成器 star 數(shù)最高的是 to-markdown。

簡單的代碼示例

 
 
  1. var toMarkdown = require('to-markdown');
  2. toMarkdown('

    Hello world!

    ');

to-markdown 最近進行了更新,增加了對 gfm 的兼容,gfm 就是 git flavored markdown 的意思, 是 github 對 markdown 語法進行的擴展。

使用 gfm 的示例

 
 
  1. toMarkdown('Hello world!', { gfm: true });

那這個 to-markdown 有什么用呢?

舉個簡單的例子,假如我想開發(fā)一個簡單的 RSS 閱讀器,但是我又不想跳轉(zhuǎn)到目標(biāo)網(wǎng)站去閱讀,因為不同的網(wǎng)站風(fēng)格不一,導(dǎo)致不一致的閱讀體驗。

怎么辦呢?那就把網(wǎng)站內(nèi)容抓取下來,然后用 to-markdown 生成 markdown 文件,然后使用自己的模板樣式進行統(tǒng)一渲染。

當(dāng)然去除廣告只是一個 side effect。

heckyesmarkdown

除了 to-markdown 之外還有一個比較好用的 API: heckyesmarkdown,這個項目使用了 php-readability,提高文章的可讀性。

可惜 heckyesmarkdown 沒有開源出來,這個項目有點古老,估計那個時候 github 還沒流行起來。

heckyesmarkdow 對中文的支持不是非常友好,如果想抓取中文站還是使用 to-markdown 比較靠譜一點。

front matter

markdown 寫文章確實很方便,簡單容易上手,但是 markdown 不能保存元數(shù)據(jù),例如作者,日期,類型這樣的結(jié)構(gòu)化的數(shù)據(jù),如果都生成 html 標(biāo)簽的話提取的時候又稍微麻煩了點, 還得借助 cheerio 才能完成。

所以,為了能方便的保存文章的元數(shù)據(jù),幾乎所有的靜態(tài)網(wǎng)站生成器都使用 front matter 格式來保存文章。

front matter 文件通常分為頭部和正文部分,頭部一般使用 yaml、toml 和 json 三種格式,front matter 解析工具需要識別這三種格式的文件頭。正文部分就是普通的 markdown 內(nèi)容。

front-matter

front-matter 也是用 node.js 開發(fā)的,相比 markdown 解析器來說,fornt-matter 解析器要簡單很多。

示例文件 example.md

 
 
  1. ---
  2. title: Just hack'n
  3. description: Nothing to see here
  4. ---
  5. This is some text about some stuff that happened sometime ago

解析代碼

 
 
  1. var fs = require('fs')
  2.   , fm = require('front-matter')
  3. fs.readFile('./example.md', 'utf8', function(err, data){
  4.   if (err) throw err
  5.   var content = fm(data)
  6.   console.log(content)
  7. })

解析結(jié)果

 
 
  1. {
  2.     attributes: {
  3.         title: 'Just hack\'n',
  4.         description: 'Nothing to see here'
  5.     },
  6.     body: '\nThis is some text about some stuff that happened sometime ago',
  7.     frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here'
  8. }

front matter 雖然格式看起來不太統(tǒng)一,卻是對 markdown 強有力的補充。


文章標(biāo)題:除了Markdown編輯器,你還需要會用程序來處理它
本文網(wǎng)址:http://www.dlmjj.cn/article/djehepd.html