mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-11-02 16:13:56 +08:00
1 line
3.9 KiB
JavaScript
Generated
1 line
3.9 KiB
JavaScript
Generated
"use strict";"require baseclass";const MD_MAX_DEPTH=2,MD_HEADING_OFFSET=3,MD_FENCE="```",MD_HEADING_RE=/^(#{1,3}) +(.*)$/,MD_RULE_RE=/^-{3,} *$/,MD_QUOTE_RE=/^> ?/,MD_BULLET_RE=/^[-*] +/,MD_NUMBER_RE=/^[0-9]{1,9}[.] +/,MD_EXTERNAL_RE=/^https:/;function mdClean(e){const t="string"==typeof e?e:"";let n="",l=0;for(let e=0;e<t.length;e+=1){const i=t.charCodeAt(e);(i<32&&9!==i&&10!==i||127===i)&&(n+=t.slice(l,e),l=e+1)}return n+t.slice(l)}function mdInline(e,t){const n=[],l={};let i="",r=0;const s=()=>{i&&n.push({type:"text",text:i}),i=""},o=(t,n)=>{if(l[t])return-1;const i=e.indexOf(t,n);return-1===i&&(l[t]=!0),i},c=e=>t<2?mdInline(e,t+1):[{type:"text",text:e}];for(;r<e.length;){const t=e[r];if("`"===t){const t=o("`",r+1);if(t>r+1){s(),n.push({type:"code",text:e.slice(r+1,t)}),r=t+1;continue}}else if("*"===t){const t="*"===e[r+1]?"**":"*",l=r+t.length;if(l<e.length&&" "!==e[l]){const i=o(t,l);if(i>l&&" "!==e[i-1]){s(),n.push({type:"**"===t?"strong":"em",children:c(e.slice(l,i))}),r=i+t.length;continue}}if("**"===t){i+=t,r+=2;continue}}else if("["===t){let t=r+1;for(;t<e.length&&"["!==e[t]&&"]"!==e[t];)t+=1;if(t>r+1&&"]"===e[t]&&"("===e[t+1]){let l=t+2;for(;l<e.length&&")"!==e[l]&&"("!==e[l]&&" "!==e[l];)l+=1;if(l>t+2&&")"===e[l]){s(),n.push({type:"link",text:e.slice(r+1,t),url:e.slice(t+2,l)}),r=l+1;continue}}}i+=t,r+=1}return s(),n}function mdBlocks(e){const t=mdClean(e).split("\n"),n=[];let l=null;const i=()=>{l=null},r=(e,t)=>{l&&l.type===e?l.lines.push(t):(l={type:e,lines:[t]},n.push(l))},s=(e,t)=>{l&&l.type===e?l.items.push(t):(l={type:e,items:[t]},n.push(l))};for(let e=0;e<t.length;e+=1){const o=t[e].trim();if(o.startsWith("```")){const l=[];for(e+=1;e<t.length&&!t[e].trim().startsWith("```");)l.push(t[e]),e+=1;i(),n.push({type:"code",text:l.join("\n")});continue}if(""===o){i();continue}const c=MD_HEADING_RE.exec(o);c?(i(),n.push({type:"heading",level:c[1].length,text:c[2]})):MD_RULE_RE.test(o)?(i(),n.push({type:"rule"})):MD_QUOTE_RE.test(o)?r("quote",o.replace(MD_QUOTE_RE,"")):MD_BULLET_RE.test(o)?s("bullets",o.replace(MD_BULLET_RE,"")):MD_NUMBER_RE.test(o)?s("numbers",o.replace(MD_NUMBER_RE,"")):l&&l.items?l.items[l.items.length-1]+=" "+o:r("paragraph",o)}return n}function mdPlain(e){return e.map(e=>e.children?mdPlain(e.children):e.text).join("")}function renderMarkdown(e,t,n){const l=t.createDocumentFragment(),i=(e,l)=>(l.forEach(l=>{if("text"===l.type)e.appendChild(t.createTextNode(l.text));else if("code"===l.type){const n=t.createElement("code");n.appendChild(t.createTextNode(l.text)),e.appendChild(n)}else if("link"===l.type){const i="function"==typeof n?n(l.url):"";if("string"!=typeof i||""===i)return void e.appendChild(t.createTextNode(l.text));const r=t.createElement("a");r.setAttribute("href",i),MD_EXTERNAL_RE.test(i)&&(r.setAttribute("target","_blank"),r.setAttribute("rel","noreferrer")),r.appendChild(t.createTextNode(l.text)),e.appendChild(r)}else{const n=t.createElement("strong"===l.type?"strong":"em");i(n,l.children),e.appendChild(n)}}),e),r=(e,n)=>i(t.createElement(e),mdInline(n,0));return mdBlocks(e).forEach(e=>{if("code"===e.type){const n=t.createElement("pre"),i=t.createElement("code");i.appendChild(t.createTextNode(e.text)),n.appendChild(i),l.appendChild(n)}else if("rule"===e.type)l.appendChild(t.createElement("hr"));else if("heading"===e.type)l.appendChild(r("h"+(e.level+3),e.text));else if(e.items){const n=t.createElement("numbers"===e.type?"ol":"ul");e.items.forEach(e=>n.appendChild(r("li",e))),l.appendChild(n)}else l.appendChild(r("quote"===e.type?"blockquote":"p",e.lines.join(" ")))}),l}function markdownToText(e){const t=mdBlocks(e);for(let e=0;e<t.length;e+=1){const n=t[e];let l="";if("code"===n.type?l=n.text.split("\n").filter(e=>""!==e.trim())[0]||"":"heading"===n.type?l=mdPlain(mdInline(n.text,0)):n.items?l=mdPlain(mdInline(n.items[0],0)):n.lines&&(l=mdPlain(mdInline(n.lines[0],0))),""!==l.trim())return l.trim()}return""}return baseclass.extend({render:renderMarkdown,toText:markdownToText}); |