UNPKG

mermaid-it-markdown

Version:

一个markdown-it的插件,用于支持mermaid语法的渲染

2 lines (1 loc) 2.29 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("mermaid")):"function"==typeof define&&define.amd?define(["exports","mermaid"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).MermaidItMarkdown={},e.mermaid)}(this,(function(e,t){"use strict";let n;const r=new Uint8Array(16);function o(){if(!n&&(n="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!n))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return n(r)}const i=[];for(let f=0;f<256;++f)i.push((f+256).toString(16).slice(1));const d={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function s(e,t,n){if(d.randomUUID&&!t&&!e)return d.randomUUID();const r=(e=e||{}).random||(e.rng||o)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return function(e,t=0){return i[e[t+0]]+i[e[t+1]]+i[e[t+2]]+i[e[t+3]]+"-"+i[e[t+4]]+i[e[t+5]]+"-"+i[e[t+6]]+i[e[t+7]]+"-"+i[e[t+8]]+i[e[t+9]]+"-"+i[e[t+10]]+i[e[t+11]]+i[e[t+12]]+i[e[t+13]]+i[e[t+14]]+i[e[t+15]]}(r)}const a=new Map,c=new Map;let u=0;const l=async(e,n)=>{const r=document.getElementById(n);if(r)try{const{svg:o}=await t.render("mermaid-svg-"+((e="")=>s().split("-").join(e))(),e);r.innerHTML=o,a.set(n,o),c.set(n,u)}catch(o){r.innerHTML=`<div style="color: red; padding: 10px; border: 1px solid red;">Mermaid Error: ${o.message||"Invalid syntax"}</div>`}};e.default=function(e){t.initialize({startOnLoad:!1});const n=e.renderer.rules.fence.bind(e.renderer.rules);e.renderer.rules.fence=(e,t,r,o,i)=>{u++,(()=>{if(a.size<=200)return;const e=Array.from(c.entries());e.sort(((e,t)=>e[1]-t[1]));const t=Math.floor(e.length/2);for(let n=0;n<t;n++){const t=e[n][0];a.delete(t),c.delete(t)}})();const d=e[t];if("mermaid"===d.info.trim()){const e="mermaid-container-"+(e=>{let t=0;if(0===e.length)return t;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n),t&=t;return Math.abs(t)})(d.content),t=a.get(e);return t?(c.set(e,u),`<div id="${e}">${t}</div>`):(setTimeout((()=>l(d.content,e)),0),`<div id="${e}">Loading mermaid...</div>`)}return n(e,t,r,o,i)}},Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));