摘要:如何擴展語法想要擴展語法,也就需要深入了解的工作機制。的工作機制創(chuàng)建詞法解析器實例將字符串解析成是官方文檔的叫法,按照我的理解應該是節(jié)點接下來創(chuàng)建解析器實例調用來解析,生成字符串到這里,的工作機制就完了。
請移步到我的Blog,獲得更好的閱讀體驗!本文的鏈接請點這里起因
我的博客系統(tǒng)的文章是直接使用gitbook保存的markdown文檔,后端使用marked.js來解析markdown文檔。
因為gitbook比較優(yōu)秀,markdown進行了語法擴展,拿gitbook寫文章也就更爽了。但是,這樣問題就出現(xiàn)了,marked.js無法解析gitbook擴展的語法,所以我們就需要手動去解析這些擴展的markdown語法。
marked.js如何擴展語法?想要擴展語法,也就需要深入了解marked.js的工作機制。
marked.js的工作機制:
創(chuàng)建Lexer(詞法解析器)實例:
const lexer = new marked.Lexer()
lexer將markdown字符串解析成tokens(tokens是官方文檔的叫法,按照我的理解應該是markdown節(jié)點(node)):
const tokens = lexer.lex(md)
接下來創(chuàng)建Parser(解析器)實例:
const parser = new marked.Parser()
調用parser.parse()來解析tokens,生成html字符串:
const html = parser.parse(tokens)
到這里,marked.js的工作機制就完了。
那么,我們需要修改的部分也就很清晰了,我們需要修改lexer的生成tokens函數(shù)、parser的解析函數(shù)以及渲染函數(shù)。
我看了marked.js的源碼發(fā)現(xiàn),lexer生成token的函數(shù)是一個名為token的函數(shù),這個函數(shù)的代碼是這樣的:
marked.Lexer.prototype.token = function (src, top) { src = src.replace(/^ +$/gm, ""); var next, // ...; while (src) { // newline if (cap = this.rules.newline.exec(src)) { src = src.substring(cap[0].length); if (cap[0].length > 1) { this.tokens.push({ type: "space" }); } } // ... } //... }
也就是,lexer用while循環(huán)來使用正則表達式進行判斷是否符合語法,如果符合就push到tokens,然后切割字符串進行下次循環(huán)。我們就如法炮制一條正則表達式和循環(huán)中的代碼塊:
const customRules = { hint: /^{% hint style="([a-z]+)" %} (.*)?( {% endhint %})/ } // 將自定義的rule添加到詞法解析器 marked.Lexer.rules = Object.assign(marked.Lexer.rules,customRules) const lexer = new marked.Lexer(); // 源代碼是merge了新的block rules,所以需要在實例上再添加一次 /* block.normal = merge({}, block); function Lexer(options) { this.tokens = []; this.tokens.links = Object.create(null); this.options = options || marked.defaults; this.rules = block.normal; } */ Object.assign(lexer.rules,customRules) // 重寫詞法解析器 marked.Lexer.prototype.token = function (src, top) { src = src.replace(/^ +$/gm, ""); var next, //...; while (src) { // newline if (cap = this.rules.newline.exec(src)) { src = src.substring(cap[0].length); if (cap[0].length > 1) { this.tokens.push({ type: "space" }); } } // 我們擴展的hint語法 // hint if (cap = this.rules.hint.exec(src)) { var lastToken = this.tokens[this.tokens.length - 1]; src = src.substring(cap[0].length); this.tokens.push({ type: "hint", state:cap[1], text: cap[2] }); continue; } } //...
這樣,詞法解析器就修改完成了,接下來我們需要修改Parser的解析函數(shù)用來生成對應hint語法的html
如何修改Parser解析?依然是翻源碼,找到parser的解析函數(shù):tok
marked.Parser.prototype.tok = function() { switch (this.token.type) { case "space": { return ""; } case "hr": { return this.renderer.hr(); } //... } //... }
我們需要在switch中增加一個case:
marked.Parser.prototype.tok = function() { switch (this.token.type) { case "space": { return ""; } case "hint": { return this.renderer.hint(this.token.state, this.token.text); } case "hr": { return this.renderer.hr(); } //... } //... }
這里的`this.renderer.hint`調用的是渲染器的函數(shù),也就是讓marked.js知道怎么渲染hint語法:
// 新建渲染器 const renderer = new marked.Renderer() const hintState={ info:``, warning: ``, danger: ``, success: `` } // 自定義語法的渲染函數(shù) renderer.hint = (state, text) => { return `${text.replace(/ /g,"` }
")}
最后,將renderer傳遞給marked.js:
marked.setOptions({ renderer })
這樣,我們就新擴展了markdown的語法啦
例:
const md = `{% hint style="info" %} 1 {% endhint %} {% hint style="warning" %} 2 {% endhint %} {% hint style="danger" %} 3 {% endhint %} {% hint style="success" %} 4 {% endhint %}` marked(md)
展示出來就是這個樣子:
上面的代碼是不完整的,下面貼上完整的代碼
import marked from "marked" import hljs from "highlight.js" const customRules = { hint: /^{% hint style="([a-z]+)" %} (.*)?( {% endhint %})/ } // 將自定義的rule添加到詞法解析器 marked.Lexer.rules = Object.assign(marked.Lexer.rules,customRules) const lexer = new marked.Lexer(); // 源代碼是merge了新的block rules,所以需要在實例上再添加一次 /* block.normal = merge({}, block); function Lexer(options) { this.tokens = []; this.tokens.links = Object.create(null); this.options = options || marked.defaults; this.rules = block.normal; } */ Object.assign(lexer.rules,customRules) // 重寫詞法解析器 marked.Lexer.prototype.token = function (src, top) { src = src.replace(/^ +$/gm, ""); var next, loose, cap, bull, b, item, listStart, listItems, t, space, i, tag, l, isordered, istask, ischecked; while (src) { // newline if (cap = this.rules.newline.exec(src)) { src = src.substring(cap[0].length); if (cap[0].length > 1) { this.tokens.push({ type: "space" }); } } // hint if (cap = this.rules.hint.exec(src)) { var lastToken = this.tokens[this.tokens.length - 1]; src = src.substring(cap[0].length); this.tokens.push({ type: "hint", state:cap[1], text: cap[2] }); continue; } // code if (cap = this.rules.code.exec(src)) { var lastToken = this.tokens[this.tokens.length - 1]; src = src.substring(cap[0].length); // An indented code block cannot interrupt a paragraph. if (lastToken && lastToken.type === "paragraph") { lastToken.text += " " + cap[0].trimRight(); } else { cap = cap[0].replace(/^ {4}/gm, ""); this.tokens.push({ type: "code", codeBlockStyle: "indented", text: !this.options.pedantic ? rtrim(cap, " ") : cap }); } continue; } // fences if (cap = this.rules.fences.exec(src)) { src = src.substring(cap[0].length); this.tokens.push({ type: "code", lang: cap[2] ? cap[2].trim() : cap[2], text: cap[3] || "" }); continue; } // heading if (cap = this.rules.heading.exec(src)) { src = src.substring(cap[0].length); this.tokens.push({ type: "heading", depth: cap[1].length, text: cap[2] }); continue; } // table no leading pipe (gfm) if (cap = this.rules.nptable.exec(src)) { item = { type: "table", header: splitCells(cap[1].replace(/^ *| *| *$/g, "")), align: cap[2].replace(/^ *|| *$/g, "").split(/ *| */), cells: cap[3] ? cap[3].replace(/ $/, "").split(" ") : [] }; if (item.header.length === item.align.length) { src = src.substring(cap[0].length); for (i = 0; i < item.align.length; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = "right"; } else if (/^ *:-+: *$/.test(item.align[i])) { item.align[i] = "center"; } else if (/^ *:-+ *$/.test(item.align[i])) { item.align[i] = "left"; } else { item.align[i] = null; } } for (i = 0; i < item.cells.length; i++) { item.cells[i] = splitCells(item.cells[i], item.header.length); } this.tokens.push(item); continue; } } // hr if (cap = this.rules.hr.exec(src)) { src = src.substring(cap[0].length); this.tokens.push({ type: "hr" }); continue; } // blockquote if (cap = this.rules.blockquote.exec(src)) { src = src.substring(cap[0].length); this.tokens.push({ type: "blockquote_start" }); cap = cap[0].replace(/^ *> ?/gm, ""); // Pass `top` to keep the current // "toplevel" state. This is exactly // how markdown.pl works. this.token(cap, top); this.tokens.push({ type: "blockquote_end" }); continue; } // list if (cap = this.rules.list.exec(src)) { src = src.substring(cap[0].length); bull = cap[2]; isordered = bull.length > 1; listStart = { type: "list_start", ordered: isordered, start: isordered ? +bull : "", loose: false }; this.tokens.push(listStart); // Get each top-level item. cap = cap[0].match(this.rules.item); listItems = []; next = false; l = cap.length; i = 0; for (; i < l; i++) { item = cap[i]; // Remove the list item"s bullet // so it is seen as the next token. space = item.length; item = item.replace(/^ *([*+-]|d+.) */, ""); // Outdent whatever the // list item contains. Hacky. if (~item.indexOf(" ")) { space -= item.length; item = !this.options.pedantic ? item.replace(new RegExp("^ {1," + space + "}", "gm"), "") : item.replace(/^ {1,4}/gm, ""); } // Determine whether the next list item belongs here. // Backpedal if it does not belong in this list. if (i !== l - 1) { const bullet = /(?:[*+-]|d{1,9}.)/ b = bullet.exec(cap[i + 1])[0]; if (bull.length > 1 ? b.length === 1 : (b.length > 1 || (this.options.smartLists && b !== bull))) { src = cap.slice(i + 1).join(" ") + src; i = l - 1; } } // Determine whether item is loose or not. // Use: /(^| )(?! )[^ ]+ (?!s*$)/ // for discount behavior. loose = next || / (?!s*$)/.test(item); if (i !== l - 1) { next = item.charAt(item.length - 1) === " "; if (!loose) loose = next; } if (loose) { listStart.loose = true; } // Check for task list items istask = /^[[ xX]] /.test(item); ischecked = undefined; if (istask) { ischecked = item[1] !== " "; item = item.replace(/^[[ xX]] +/, ""); } t = { type: "list_item_start", task: istask, checked: ischecked, loose: loose }; listItems.push(t); this.tokens.push(t); // Recurse. this.token(item, false); this.tokens.push({ type: "list_item_end" }); } if (listStart.loose) { l = listItems.length; i = 0; for (; i < l; i++) { listItems[i].loose = true; } } this.tokens.push({ type: "list_end" }); continue; } // html if (cap = this.rules.html.exec(src)) { src = src.substring(cap[0].length); this.tokens.push({ type: this.options.sanitize ? "paragraph" : "html", pre: !this.options.sanitizer && (cap[1] === "pre" || cap[1] === "script" || cap[1] === "style"), text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0] }); continue; } // def if (top && (cap = this.rules.def.exec(src))) { src = src.substring(cap[0].length); if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1); tag = cap[1].toLowerCase().replace(/s+/g, " "); if (!this.tokens.links[tag]) { this.tokens.links[tag] = { href: cap[2], title: cap[3] }; } continue; } // table (gfm) if (cap = this.rules.table.exec(src)) { item = { type: "table", header: splitCells(cap[1].replace(/^ *| *| *$/g, "")), align: cap[2].replace(/^ *|| *$/g, "").split(/ *| */), cells: cap[3] ? cap[3].replace(/ $/, "").split(" ") : [] }; if (item.header.length === item.align.length) { src = src.substring(cap[0].length); for (i = 0; i < item.align.length; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = "right"; } else if (/^ *:-+: *$/.test(item.align[i])) { item.align[i] = "center"; } else if (/^ *:-+ *$/.test(item.align[i])) { item.align[i] = "left"; } else { item.align[i] = null; } } for (i = 0; i < item.cells.length; i++) { item.cells[i] = splitCells( item.cells[i].replace(/^ *| *| *| *$/g, ""), item.header.length); } this.tokens.push(item); continue; } } // lheading if (cap = this.rules.lheading.exec(src)) { src = src.substring(cap[0].length); this.tokens.push({ type: "heading", depth: cap[2].charAt(0) === "=" ? 1 : 2, text: cap[1] }); continue; } // top-level paragraph if (top && (cap = this.rules.paragraph.exec(src))) { src = src.substring(cap[0].length); this.tokens.push({ type: "paragraph", text: cap[1].charAt(cap[1].length - 1) === " " ? cap[1].slice(0, -1) : cap[1] }); continue; } // text if (cap = this.rules.text.exec(src)) { // Top-level should never reach here. src = src.substring(cap[0].length); this.tokens.push({ type: "text", text: cap[0] }); continue; } if (src) { throw new Error("Infinite loop on byte: " + src.charCodeAt(0)); } } return this.tokens; } // 重寫解析器 marked.Parser.prototype.tok = function() { switch (this.token.type) { case "space": { return ""; } case "hint": { return this.renderer.hint(this.token.state, this.token.text); } case "hr": { return this.renderer.hr(); } case "heading": { return this.renderer.heading( this.inline.output(this.token.text), this.token.depth, unescape(this.inlineText.output(this.token.text)), this.slugger); } case "code": { return this.renderer.code(this.token.text, this.token.lang, this.token.escaped); } case "table": { var header = "", body = "", i, row, cell, j; // header cell = ""; for (i = 0; i < this.token.header.length; i++) { cell += this.renderer.tablecell( this.inline.output(this.token.header[i]), { header: true, align: this.token.align[i] } ); } header += this.renderer.tablerow(cell); for (i = 0; i < this.token.cells.length; i++) { row = this.token.cells[i]; cell = ""; for (j = 0; j < row.length; j++) { cell += this.renderer.tablecell( this.inline.output(row[j]), { header: false, align: this.token.align[j] } ); } body += this.renderer.tablerow(cell); } return this.renderer.table(header, body); } case "blockquote_start": { body = ""; while (this.next().type !== "blockquote_end") { body += this.tok(); } return this.renderer.blockquote(body); } case "list_start": { body = ""; var ordered = this.token.ordered, start = this.token.start; while (this.next().type !== "list_end") { body += this.tok(); } return this.renderer.list(body, ordered, start); } case "list_item_start": { body = ""; var loose = this.token.loose; var checked = this.token.checked; var task = this.token.task; if (this.token.task) { body += this.renderer.checkbox(checked); } while (this.next().type !== "list_item_end") { body += !loose && this.token.type === "text" ? this.parseText() : this.tok(); } return this.renderer.listitem(body, task, checked); } case "html": { // TODO parse inline content if parameter markdown=1 return this.renderer.html(this.token.text); } case "paragraph": { return this.renderer.paragraph(this.inline.output(this.token.text)); } case "text": { return this.renderer.paragraph(this.parseText()); } default: { var errMsg = "Token with "" + this.token.type + "" type was not found."; if (this.options.silent) { console.log(errMsg); } else { throw new Error(errMsg); } } } }; // 新建渲染器 const renderer = new marked.Renderer() const hintState={ info:``, warning: ``, danger: ``, success: `` } // 自定義語法的渲染函數(shù) renderer.hint = (state, text) => { return `${text.replace(/ /g,"` } // code高亮 renderer.code = (code, language, isEscaped) => { const isMarkup = language === "markup" let hled if (isMarkup) { hled = hljs.highlightAuto(code).value; } else { hled = hljs.highlight(language, code).value } return `
")}` } renderer.listitem = (body, task, checked) => { let className = "" if(task){ className = "task-item" } return `${hled}
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/105675.html
摘要:讀源碼造輪子織雪紗奈的源碼閱讀之路很好奇是怎么變成的偶然間看到上的這一篇星星很高的源碼就來研究研究下載打開在文件夾下的大概行左右的樣子沒有組件化所有的都是在一個頁面讀著很是順暢哈這里沒有貶低多個文件的啊不要生氣嘻嘻 讀源碼,造輪子,織雪紗奈的源碼閱讀之路 很好奇markdown是怎么變成html的,偶然間看到github上的這一篇星星?很高的源碼,就來研究研究下載打開在lib文件夾下的...
摘要:自動簡歷項目介紹一個可以自動播放書寫過程簡歷,主要運用原生和的知識點。方法如果展示窗口設置的是或者會自動拉到底部為滾動至底部為滾動至頂部其他參數(shù)可選定義緩動動畫,或之一。增加簡歷展示頁編寫增加編寫簡歷內(nèi)容的展示窗口。 自動簡歷 項目介紹 一個可以自動播放書寫過程簡歷,主要運用原生JS和CSS3的知識點。 用到的庫: prism marked 相關鏈接 預覽點我 源碼點我show...
摘要:自動簡歷項目介紹一個可以自動播放書寫過程簡歷,主要運用原生和的知識點。方法如果展示窗口設置的是或者會自動拉到底部為滾動至底部為滾動至頂部其他參數(shù)可選定義緩動動畫,或之一。增加簡歷展示頁編寫增加編寫簡歷內(nèi)容的展示窗口。 自動簡歷 項目介紹 一個可以自動播放書寫過程簡歷,主要運用原生JS和CSS3的知識點。 用到的庫: prism marked 相關鏈接 預覽點我 源碼點我show...
閱讀 3313·2023-04-26 00:58
閱讀 1267·2021-09-22 16:04
閱讀 3311·2021-09-02 15:11
閱讀 1554·2019-08-30 15:55
閱讀 2338·2019-08-30 15:55
閱讀 3247·2019-08-23 18:41
閱讀 3457·2019-08-23 18:18
閱讀 2751·2019-08-23 17:53