最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript+Node.js寫一款markdown解析器

 更新時(shí)間:2022年02月14日 09:54:09   作者:隱冬?  
這篇文章主要介紹了利用JavaScript和Node.js寫一款markdown解析器,首先編寫getHtml函數(shù),傳入markdown文本字符串,下面更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下

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),[超鏈接](url),使用正則匹配同時(shí)需要排除`。props會(huì)獲取正則中的$,$1,$2。也就是匹配的字符整體,第一個(gè)括號(hào)內(nèi)容,第二個(gè)括號(hào)內(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

鸡西市| 凌云县| 常宁市| 甘孜| 政和县| 龙泉市| 姜堰市| 杭州市| 漠河县| 佛教| 彰化县| 丰原市| 施秉县| 双柏县| 巴青县| 株洲县| 鄂伦春自治旗| 托克托县| 潞城市| 息烽县| 梁山县| 道真| 韶关市| 盘山县| 西乌| 阳春市| 新营市| 平阴县| 塘沽区| 吉安市| 汽车| 绥化市| 梁平县| 房产| 青冈县| 旺苍县| 阿瓦提县| 濮阳县| 柯坪县| 邯郸县| 五原县|