JavaScript+Node.js寫一款markdown解析器
1. 準(zhǔn)備工作
首先編寫getHtml
函數(shù),傳入markdown
文本字符串,這里使用fs讀取markdown文件內(nèi)容,返回值是轉(zhuǎn)換過后的字符串。
const fs = require('fs'); const source = fs.readFileSync('./test.md', 'utf-8'); const getHtml = (source) => { ? ? // 處理標(biāo)題 ? ? return source; } const result = getHtml(source); console.log(result);
主要設(shè)計(jì)正則表達(dá)式和String.prototype.replace
方法,replace
接收的第一個(gè)參數(shù)可以是正則,第二個(gè)參數(shù)如果是函數(shù)那么返回值就是所替換的內(nèi)容。
2. 處理圖片&超鏈接
圖片和超鏈接的語法很像,,[超鏈接](url),使用正則匹配同時(shí)需要排除`。props會獲取正則中的$,$1,$2。也就是匹配的字符整體,第一個(gè)括號內(nèi)容,第二個(gè)括號內(nèi)容。比如這里props[0]
就是匹配到的完整內(nèi)容,第四個(gè)參數(shù)props[3]是[]中的alt,第五個(gè)參數(shù)props[4]是鏈接地址。
const imageora = (source) => { ? ? return source.replace(/(`?)(!?)\[(.*)\]\((.+)\)/gi, (...props) => { ? ? ? ? switch (props[0].trim()[0]) { ? ? ? ? ? ? case '!': return `<a href="${props[4]}" rel="external nofollow" alt="${props[3]}">${props[3]}</a>`; ? ? ? ? ? ? case '[': return `<img src="${props[4]}" alt="${props[3]}"/>`; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }); } const getHtml = (source) => { ? ? source = imageora(source); ? ? return source; }
3. 處理blockquote
這里使用\x20匹配空格。如果匹配到內(nèi)容,將文本props[3]放在blockquote
標(biāo)簽返回就行了。
const block = (source) => { ? ? return source.replace(/(.*)(`?)\>\x20+(.+)/gi, (...props) => { ? ? ? ? switch (props[0].trim()[0]) { ? ? ? ? ? ? case '>': return `<blockquote>${props[3]}</blockquote>`; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }); }
4. 處理標(biāo)題
匹配必須以#開頭,并且#的數(shù)量不能超過6,因?yàn)閔6是最大的了,沒有h7,最后props[2]是#后跟隨的文本。
const formatTitle = (source) => { ? ? return source.replace(/(.*#+)\x20?(.*)/g, (...props) => { ? ? ? ? switch (props[0][0]) { ? ? ? ? ? ? case '#': if (props[1].length <= 6) { ? ? ? ? ? ? ? ? return `<h${props[1].length}>${props[2].trim()}</h${props[1].length}>`; ? ? ? ? ? ? }; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }) }
5. 處理字體
寫的開始復(fù)雜了
const formatFont = (source) => { ? ? // 處理 ~ 包裹的文本 ? ? source = source.replace(/([`\\]*\~{2})(.*?)\~{2}/g, (...props) => { ? ? ? ? switch (props[0].trim()[0]) { ? ? ? ? ? ? case '~': return `<del>${props[2]}</del>`;; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }); ? ? // 處理 * - 表示的換行 ? ? source = source.replace(/([`\\]*)[* -]{3,}\n/g, (...props) => { ? ? ? ? switch (props[0].trim()[0]) { ? ? ? ? ? ? case '*': ; ? ? ? ? ? ? case '-': return `<hr />`; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }) ? ? // 處理***表示的加粗或者傾斜。 ? ? source = source.replace(/([`\\]*\*{1,3})(.*?)(\*{1,3})/g, (...props) => { ? ? ? ? switch (props[0].trim()[0]) { ? ? ? ? ? ? case '*': if (props[1] === props[3]) { ? ? ? ? ? ? ? ? if (props[1].length === 1) { ? ? ? ? ? ? ? ? ? ? return `<em>${props[2]}</em>`;; ? ? ? ? ? ? ? ? } else if (props[1].length === 2) { ? ? ? ? ? ? ? ? ? ? return `<strong>${props[2]}</strong>`;; ? ? ? ? ? ? ? ? } else if (props[1].length === 3) { ? ? ? ? ? ? ? ? ? ? return `<strong><em>${props[2]}</em></strong>`;; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }); ? ? return source; }
6. 處理代碼塊
使用正則匹配使用`包裹的代碼塊,props[1]是開頭`的數(shù)量,props[5]是結(jié)尾`的數(shù)量,必須相等才生效。
const pre = (source) => { ? ? source = source.replace(/([\\`]+)(\w+(\n))?([^!`]*?)(`+)/g, (...props) => { ? ? ? ? switch (props[0].trim()[0]) { ? ? ? ? ? ? case '`': if (props[1] === props[5]) { ? ? ? ? ? ? ? ? return `<pre>${props[3] || ''}${props[4]}</pre>`; ? ? ? ? ? ? }; ? ? ? ? ? ? default: return props[0]; ? ? ? ? } ? ? }); ? ? return source; }
7. 處理列表
這里只是處理了ul無序列表,寫的同樣很麻煩。主要我的思路是真復(fù)雜。而且bug
肯定也不少。先匹配-+*加上空格,然后根據(jù)這一行前面的空格熟替換為ul。這樣每一行都保證被ulli包裹。
第二步判斷相鄰ul之間相差的個(gè)數(shù),如果相等則表示應(yīng)該是同一個(gè)ul的li,替換掉</ul><ul>為空,如果后一個(gè)ul大于前一個(gè)ul,則表示后面有退格,新生成一個(gè)<ul>包裹退格后的li,如果是最后一個(gè)ul則補(bǔ)齊前面所有的</ul>。
const list = (source) => { ? ? source = source.replace(/.*?[\x20\t]*([\-\+\*]{1})\x20(.*)/g, (...props) => { ? ? ? ? if (/^[\t\x20\-\+\*]/.test(props[0])) { ? ? ? ? ? ? return props[0].replace(/([\t\x20]*)[\-\+\*]\x20(.*)/g, (...props) => { ? ? ? ? ? ? ? ? const len = props[1].length || ''; ? ? ? ? ? ? ? ? return `<ul${len}><li>${props[2]}</li></ul${len}>`; ? ? ? ? ? ? }) ? ? ? ? } else { ? ? ? ? ? ? return props[0]; ? ? ? ? } ? ? }); ? ? const set = new Set(); ? ? source = source.replace(/<\/ul(\d*)>(\n<ul(\d*)>)?/g, (...props) => { ? ? ? ? set.add(props[1]); ? ? ? ? if (props[1] == props[3]) { ? ? ? ? ? ? return ''; ? ? ? ? } else if (props[1] < props[3]) { ? ? ? ? ? ? return '<ul>'; ? ? ? ? } else { ? ? ? ? ? ? const arr = [...set]; ? ? ? ? ? ? const end = arr.indexOf(props[1]); ? ? ? ? ? ? let start = arr.indexOf(props[3]); ? ? ? ? ? ? if (start > 0) { ? ? ? ? ? ? ? ? return '</ul>'.repeat(end - start); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? return '</ul>'.repeat(end + 1); ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? }); ? ? return source.replace(/<(\/?)ul(\d*)>/g, '<$1ul>'); }
8. 處理表格
const table = (source) => { ? ? source = source.replace(/\|.*\|\n\|\s*-+\s*\|.*\|\n/g, (...props) => { ? ? ? ? let str = '<table><tr>'; ? ? ? ? const data = props[0].split(/\n/)[0].split('|'); ? ? ? ? for (let i = 1; i < data.length - 1; i++) { ? ? ? ? ? ? str += `<th>${data[i].trim()}</th>` ? ? ? ? } ? ? ? ? str += '<tr></table>'; ? ? ? ? return str; ? ? }); ? ? return formatTd(source); } const formatTd = (source) => { ? ? source = source.replace(/<\/table>\|.*\|\n/g, (...props) => { ? ? ? ? let str = '<tr>'; ? ? ? ? const data = props[0].split('|'); ? ? ? ? for (let i = 1; i < data.length - 1; i++) { ? ? ? ? ? ? str += `<td>${data[i].trim()}</td>` ? ? ? ? } ? ? ? ? str += '<tr></table>'; ? ? ? ? return str; ? ? }); ? ? if (source.includes('</table>|')) { ? ? ? ? return formatTd(source); ? ? } ? ? return source; }
9. 調(diào)用方法
const getHtml = (source) => { ? ? source = imageora(source); ? ? source = block(source); ? ? source = formatTitle(source); ? ? source = formatFont(source); ? ? source = pre(source); ? ? source = list(source); ? ? source = table(source); ? ? return source; } const result = getHtml(source); console.log(result);
到此這篇關(guān)于JavaScript+Node.js寫一款markdown解析器的文章就介紹到這了,更多相關(guān)寫一款markdown解析器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Vue中postcss-pxtorem的使用詳解
在Web開發(fā)領(lǐng)域,響應(yīng)式設(shè)計(jì)已經(jīng)成為一個(gè)不可或缺的趨勢,PostCSS插件——postcss-pxtorem的出現(xiàn)為我們提供了一種更加智能和高效的解決方案,本文將深入探討postcss-pxtorem的使用,包括其背后的原理、配置選項(xiàng)、實(shí)際應(yīng)用中的注意事項(xiàng)等方面,需要的朋友可以參考下2023-12-12JavaScript實(shí)現(xiàn)網(wǎng)頁版的五子棋游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁版的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05ElementPlus?Tag標(biāo)簽用法小結(jié)
這篇文章主要介紹了ElementPlus?Tag標(biāo)簽用法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09javascript原型鏈學(xué)習(xí)記錄之繼承實(shí)現(xiàn)方式分析
這篇文章主要介紹了javascript原型鏈學(xué)習(xí)記錄之繼承實(shí)現(xiàn)方式,結(jié)合實(shí)例形式分析了javascript使用原型鏈實(shí)現(xiàn)繼承的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05JavaScript中offsetWidth的bug及解決方法
這篇文章主要為大家詳細(xì)介紹了JavaScript中offsetWidth的bug及解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05讓 JavaScript 輕松支持函數(shù)重載 (Part 2 - 實(shí)現(xiàn))
在上一篇文章里,我們設(shè)計(jì)了一套能在JavaScript中描述函數(shù)重載的方法,這套方法依賴于一個(gè)叫做Overload的靜態(tài)類,現(xiàn)在我們就來看看如何實(shí)現(xiàn)這個(gè)靜態(tài)類。2009-08-08js通過location.search來獲取頁面?zhèn)鱽淼膮?shù)
js獲取通過window.location.search來獲取頁面?zhèn)鱽淼膮?shù),經(jīng)測試可用,大家可以學(xué)習(xí)下2014-09-09