vuepress-plugin-md-enhance
Version:
Markdown enhancement plugin for vuepress
43 lines (40 loc) • 33.5 kB
JavaScript
import{encodeData as k,entries as v,Logger as he,ensureEndingSlash as ke,isModuleAvailable as be,isDef as $e,isPlainObject as C,keys as x,endsWith as M,deepAssign as T,fromEntries as I,getModulePath as ve,isArray as Pe,addViteSsrNoExternal as we,addViteOptimizeDepsExclude as O,addViteSsrExternal as P,addViteOptimizeDepsInclude as D,addViteOptimizeDepsNeedsInterop as Ce,chainWebpack as je}from"@vuepress/helper";import{useSassPalettePlugin as Se}from"@vuepress/plugin-sass-palette";import{hash as xe,path as Me,getDirname as _e,colors as w,logger as Ie}from"vuepress/utils";import{createConverter as Oe}from"vuepress-shared/node";import{container as _}from"@mdit/plugin-container";import{demo as De}from"@mdit/plugin-demo";import{uml as Re}from"@mdit/plugin-uml";import{plantuml as Ee}from"@mdit/plugin-plantuml";const z=e=>{_(e,{name:"chartjs",openRender:(t,n)=>{const a=t[n].info.trimStart().slice(7).trim(),s=`chartjs-${n}`;let o="{}",r="";for(let i=n;i<t.length;i++){const{type:l,content:f,info:u}=t[i];if(l==="container_chartjs_close")break;f&&(l==="fence"&&(u==="json"?(o=k(f),r="json"):(u==="js"||u==="javascript")&&(o=k(f),r="js")),t[i].type="chartjs_empty",t[i].hidden=!0)}return`<ChartJS id="${s}" config="${o}" ${a?`title="${encodeURIComponent(a)}" `:""}type="${r}">`},closeRender:()=>"</ChartJS>"})},R=e=>e.replace(/&/gu,"&").replace(/</gu,"<").replace(/>/gu,">").replace(/"/gu,""").replace(/'/gu,"'"),F={useBabel:!1,jsLib:[],cssLib:[],codepenLayout:"left",codepenEditors:"101",babel:"https://unpkg.com/@babel/standalone/babel.min.js",vue:"https://unpkg.com/vue/dist/vue.global.prod.js",react:"https://unpkg.com/react/umd/react.production.min.js",reactDOM:"https://unpkg.com/react-dom/umd/react-dom.production.min.js"},E=e=>t=>{_(t,{name:e,openRender:(n,a)=>{const s=n[a].info.trimStart().slice(e.length).trim();let o="";const r={};for(let i=a;i<n.length;i++){const{type:l,content:f,info:u}=n[i],m=u?/^([^ :[{]+)/.exec(t.utils.unescapeAll(u).trim())?.[1]??"text":"";if(l===`container_${e}_close`)break;f&&l==="fence"&&(m==="json"?o=k(f):r[m]=f)}return`<CodeDemo id="code-demo-${a}" type="${e.split("-")[0]}"${s?` title="${encodeURIComponent(s)}"`:""}${o?` config="${o}"`:""} code="${k(JSON.stringify(r))}">
`},closeRender:()=>`</CodeDemo>
`})},J=E("normal-demo"),L=E("vue-demo"),V=E("react-demo"),G=e=>{e.use(De,{name:"md-demo",openRender:(t,n)=>`<MdDemo title="${R(t[n].info)}" id="md-demo-${n}">
`,codeRender:(t,n,a,s,o)=>{const r=t[n];return r.type="fence",r.info="md",r.markup="```",r.content=r.content.split(`
`).filter(i=>!i.startsWith("@")||!/^@include-p(?:ush\(.*\)|op)$/.test(i)).join(`
`),`<template #code>
${o.rules.fence(t,n,a,s,o)}</template>
`},contentOpenRender:()=>`<template #default>
`,contentCloseRender:()=>`</template>
`,closeRender:()=>`</MdDemo>
`})},q=(e,t)=>{const n=e[t],a=`echarts-${t}`,{content:s,info:o}=n,r=o.trim().split(":",2)[1];return`<ECharts id="${a}" config="${k(s)}"${r?` title="${encodeURIComponent(r)}"`:""}></ECharts>`},B=e=>{const{fence:t}=e.renderer.rules;e.renderer.rules.fence=(...n)=>{const[a,s]=n,{info:o}=a[s];return o.split(":",2)[0]==="echarts"?q(a,s):t(...n)},e.renderer.rules.echarts=q,_(e,{name:"echarts",openRender:(n,a)=>{const s=n[a].info.trimStart().slice(7).trim(),o=`echarts-${a}`;let r="{}",i=!1;for(let l=a;l<n.length;l++){const{type:f,content:u,info:m}=n[l];if(f==="container_echarts_close")break;u&&(f==="fence"&&(m==="json"?r=u:(m==="js"||m==="javascript")&&(r=u,i=!0)),n[l].type="echarts_empty",n[l].hidden=!0)}return`<ECharts id="${o}" config="${k(r)}"${s?` title="${encodeURIComponent(s)}"`:""}${i?' type="js"':""}>`},closeRender:()=>"</ECharts>"})},Ue=["ant","pie","vue"],K=(e,t)=>{const n=e[t],a=`flowchart-${t}`,{content:s,info:o}=n,r=o.trim().split(":",2)[1];return`<FlowChart id="${a}" code="${k(s)}" preset="${Ue.includes(r)?r:"vue"}"></FlowChart>`},W=e=>{const{fence:t}=e.renderer.rules;e.renderer.rules.fence=(...n)=>{const[a,s]=n,{info:o}=a[s],[r]=o.split(":",2);return r==="flow"||r==="flowchart"?K(a,s):t(...n)},e.renderer.rules.flowchart=K},H="@",Ae=["file","import","setting"],Ne=e=>(t,n,a,s)=>{let o=t.bMarks[n]+t.tShift[n],r=t.eMarks[n];if(t.src[o]!==":")return!1;let i=o+1;for(;i<=r&&t.src[i]===":";)i+=1;const l=i-o;if(l<3)return!1;const f=t.src.slice(o,i),u=t.src.slice(i,r),m=u.trimStart().split(" ",2)[0],y=u.trimStart().slice(e.length).trim();if(m.trim()!==e)return!1;if(s)return!0;let c=n,d=!1;for(;c<a&&(c+=1,o=t.bMarks[c]+t.tShift[c],r=t.eMarks[c],!(o<r&&t.sCount[c]<t.blkIndent));)if(t.src[o]===":"&&t.sCount[c]-t.blkIndent<4){for(i=o+1;i<=r&&t.src[i]===":";i++);if(i-o>=l&&(i=t.skipSpaces(i),i>=r)){d=!0;break}}const p=t.parentType,g=t.lineMax;t.parentType=e,t.lineMax=c-(d?1:0);const h=t.push(`${e}_open`,"template",1);h.markup=f,h.block=!0,h.info=y,h.map=[n,c-(d?1:0)],t.md.block.tokenize(t,n+1,c-(d?1:0));const S=t.push(`${e}_close`,"template",-1);return S.markup=t.src.slice(o,i),S.block=!0,t.parentType=p,t.lineMax=g,t.line=c+(d?1:0),!0},Te=e=>(t,n,a,s)=>{let o=t.bMarks[n]+t.tShift[n],r=t.eMarks[n];const i=`${H}${e}`;if(t.src.charAt(o)!=="@")return!1;let l;for(l=0;l<i.length;l++)if(i[l]!==t.src[o+l])return!1;const f=t.src.slice(o,o+l),u=t.src.slice(o+l,r);if(s)return!0;let m=n,y=!1;for(;m<a&&(m+=1,o=t.bMarks[m]+t.tShift[m],r=t.eMarks[m],!(o<r&&t.sCount[m]<t.blkIndent));)if(t.src[o]===H&&t.sCount[m]<=t.sCount[n]){let h=!0;for(l=0;l<i.length;l++)if(i[l]!==t.src[o+l]){h=!1;break}if(h){y=!0,m-=1;break}}const c=t.parentType,d=t.lineMax;t.parentType=e,t.lineMax=m;const p=t.push(`${e}_open`,"template",1);p.block=!0,p.markup=f,p.info=u.trim(),p.map=[n,m],t.md.block.tokenize(t,n+1,m);const g=t.push(`${e}_close`,"template",-1);return g.block=!0,g.markup="",t.parentType=c,t.lineMax=d,t.line=m+(y?1:0),!0},Y=e=>({key:e.key,title:e.title??"",files:encodeURIComponent(JSON.stringify(e.files)),settings:encodeURIComponent(JSON.stringify(e.settings))}),j=(e,{name:t="playground",component:n="Playground",propsGetter:a=Y}={name:"playground",component:"Playground",propsGetter:Y})=>{e.block.ruler.before("fence",t,Ne(t),{alt:["paragraph","reference","blockquote","list"]}),Ae.forEach(s=>{e.block.ruler.__rules__.find(({name:o})=>o===`at-${s}`)||e.block.ruler.before("fence",`at-${s}`,Te(s),{alt:["paragraph","reference","blockquote","list"]})}),e.renderer.rules[`${t}_open`]=(s,o)=>{const{info:r}=s[o],i={key:xe(`playground${o}-${r}`),title:encodeURIComponent(r),settings:{},files:{}};let l=null,f=!1;for(let m=o;m<s.length;m++){const{block:y,type:c,info:d,content:p}=s[m];if(y){if(c===`${t}_close`)break;if(c===`${t}_open`)continue;if(c==="file_open"){if(!d)continue;l=d}else if(c==="import_open")i.importMap=l=d||"import-map.json";else if(c==="setting_open")f=!0;else if(c==="file_close"||c==="import_close"||c==="setting_close"||!p){s[m].type=`${t}_empty`,s[m].hidden=!0,c==="setting_close"&&(f=!1);continue}f?c==="fence"&&d==="json"&&(i.settings=JSON.parse(p.trim())):c==="fence"&&l&&(i.files[l]={ext:d,content:p}),s[m].type=`${t}_empty`,s[m].hidden=!0}}const u=a(i);return`<${n} ${v(u).map(([m,y])=>`${m}="${R(y)}"`).join(" ")}>
`},e.renderer.rules[`${t}_close`]=()=>`</${n}>
`},ze="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$",Fe=(e,t,n)=>{let a,s,o,r={},i={},l="",f="",u="",m=2,y=3,c=2,d=[],p=0,g=0;for(o=0;o<e.length;o+=1)if(l=e.charAt(o),Object.prototype.hasOwnProperty.call(r,l)||(r[l]=y++,i[l]=!0),f=u+l,Object.prototype.hasOwnProperty.call(r,f))u=f;else{if(Object.prototype.hasOwnProperty.call(i,u)){if(u.charCodeAt(0)<256){for(a=0;a<c;a++)p<<=1,g==t-1?(g=0,d.push(n(p)),p=0):g++;for(s=u.charCodeAt(0),a=0;a<8;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1}else{for(s=1,a=0;a<c;a++)p=p<<1|s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s=0;for(s=f.charCodeAt(0),a=0;a<16;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1}--m==0&&(m=Math.pow(2,c),c++),delete i[u]}else for(s=r[u],a=0;a<c;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1;--m==0&&(m=Math.pow(2,c),c++),r[f]=y++,u=String(l)}if(u!==""){if(Object.prototype.hasOwnProperty.call(i,u)){if(u.charCodeAt(0)<256){for(a=0;a<c;a++)p<<=1,g==t-1?(g=0,d.push(n(p)),p=0):g++;for(s=u.charCodeAt(0),a=0;a<8;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1}else{for(s=1,a=0;a<c;a++)p=p<<1|s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s=0;for(s=u.charCodeAt(0),a=0;a<16;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1}--m==0&&(m=Math.pow(2,c),c++),delete i[u]}else for(s=r[u],a=0;a<c;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1;--m==0&&(m=Math.pow(2,c),c++)}for(s=2,a=0;a<c;a++)p=p<<1|1&s,g==t-1?(g=0,d.push(n(p)),p=0):g++,s>>=1;for(;;){if(p<<=1,g==t-1){d.push(n(p));break}g++}return d.join("")},Q=e=>Fe(e,6,t=>ze.charAt(t)),Je=[{name:"help",type:"boolean"},{name:"help",type:"boolean"},{name:"watch",type:"boolean"},{name:"preserveWatchOutput",type:"boolean"},{name:"listFiles",type:"boolean"},{name:"explainFiles",type:"boolean"},{name:"listEmittedFiles",type:"boolean"},{name:"pretty",type:"boolean"},{name:"traceResolution",type:"boolean"},{name:"diagnostics",type:"boolean"},{name:"extendedDiagnostics",type:"boolean"},{name:"generateCpuProfile",type:"string"},{name:"generateTrace",type:"string"},{name:"incremental",type:"boolean"},{name:"declaration",type:"boolean"},{name:"declarationMap",type:"boolean"},{name:"emitDeclarationOnly",type:"boolean"},{name:"sourceMap",type:"boolean"},{name:"inlineSourceMap",type:"boolean"},{name:"noCheck",type:"boolean"},{name:"noEmit",type:"boolean"},{name:"assumeChangesOnlyAffectDirectDependencies",type:"boolean"},{name:"locale",type:"string"},{name:"all",type:"boolean"},{name:"version",type:"boolean"},{name:"init",type:"boolean"},{name:"project",type:"string"},{name:"showConfig",type:"boolean"},{name:"listFilesOnly",type:"boolean"},{name:"target",type:{es3:0,es5:1,es6:2,es2015:2,es2016:3,es2017:4,es2018:5,es2019:6,es2020:7,es2021:8,es2022:9,es2023:10,es2024:11,esnext:99}},{name:"module",type:{none:0,commonjs:1,amd:2,system:4,umd:3,es6:5,es2015:5,es2020:6,es2022:7,esnext:99,node16:100,node18:101,nodenext:199,preserve:200}},{name:"lib",type:"list"},{name:"allowJs",type:"boolean"},{name:"checkJs",type:"boolean"},{name:"jsx",type:{preserve:1,"react-native":3,react:2,"react-jsx":4,"react-jsxdev":5}},{name:"outFile",type:"string"},{name:"outDir",type:"string"},{name:"rootDir",type:"string"},{name:"composite",type:"boolean"},{name:"tsBuildInfoFile",type:"string"},{name:"removeComments",type:"boolean"},{name:"importHelpers",type:"boolean"},{name:"importsNotUsedAsValues",type:{remove:0,preserve:1,error:2}},{name:"downlevelIteration",type:"boolean"},{name:"isolatedModules",type:"boolean"},{name:"verbatimModuleSyntax",type:"boolean"},{name:"isolatedDeclarations",type:"boolean"},{name:"erasableSyntaxOnly",type:"boolean"},{name:"libReplacement",type:"boolean"},{name:"strict",type:"boolean"},{name:"noImplicitAny",type:"boolean"},{name:"strictNullChecks",type:"boolean"},{name:"strictFunctionTypes",type:"boolean"},{name:"strictBindCallApply",type:"boolean"},{name:"strictPropertyInitialization",type:"boolean"},{name:"strictBuiltinIteratorReturn",type:"boolean"},{name:"noImplicitThis",type:"boolean"},{name:"useUnknownInCatchVariables",type:"boolean"},{name:"alwaysStrict",type:"boolean"},{name:"noUnusedLocals",type:"boolean"},{name:"noUnusedParameters",type:"boolean"},{name:"exactOptionalPropertyTypes",type:"boolean"},{name:"noImplicitReturns",type:"boolean"},{name:"noFallthroughCasesInSwitch",type:"boolean"},{name:"noUncheckedIndexedAccess",type:"boolean"},{name:"noImplicitOverride",type:"boolean"},{name:"noPropertyAccessFromIndexSignature",type:"boolean"},{name:"moduleResolution",type:{node10:2,node:2,classic:1,node16:3,nodenext:99,bundler:100}},{name:"baseUrl",type:"string"},{name:"paths",type:"object"},{name:"rootDirs",type:"list"},{name:"typeRoots",type:"list"},{name:"types",type:"list"},{name:"allowSyntheticDefaultImports",type:"boolean"},{name:"esModuleInterop",type:"boolean"},{name:"preserveSymlinks",type:"boolean"},{name:"allowUmdGlobalAccess",type:"boolean"},{name:"moduleSuffixes",type:"list"},{name:"allowImportingTsExtensions",type:"boolean"},{name:"rewriteRelativeImportExtensions",type:"boolean"},{name:"resolvePackageJsonExports",type:"boolean"},{name:"resolvePackageJsonImports",type:"boolean"},{name:"customConditions",type:"list"},{name:"noUncheckedSideEffectImports",type:"boolean"},{name:"sourceRoot",type:"string"},{name:"mapRoot",type:"string"},{name:"inlineSources",type:"boolean"},{name:"experimentalDecorators",type:"boolean"},{name:"emitDecoratorMetadata",type:"boolean"},{name:"jsxFactory",type:"string"},{name:"jsxFragmentFactory",type:"string"},{name:"jsxImportSource",type:"string"},{name:"resolveJsonModule",type:"boolean"},{name:"allowArbitraryExtensions",type:"boolean"},{name:"out",type:"string"},{name:"reactNamespace",type:"string"},{name:"skipDefaultLibCheck",type:"boolean"},{name:"charset",type:"string"},{name:"emitBOM",type:"boolean"},{name:"newLine",type:{crlf:0,lf:1}},{name:"noErrorTruncation",type:"boolean"},{name:"noLib",type:"boolean"},{name:"noResolve",type:"boolean"},{name:"stripInternal",type:"boolean"},{name:"disableSizeLimit",type:"boolean"},{name:"disableSourceOfProjectReferenceRedirect",type:"boolean"},{name:"disableSolutionSearching",type:"boolean"},{name:"disableReferencedProjectLoad",type:"boolean"},{name:"noImplicitUseStrict",type:"boolean"},{name:"noEmitHelpers",type:"boolean"},{name:"noEmitOnError",type:"boolean"},{name:"preserveConstEnums",type:"boolean"},{name:"declarationDir",type:"string"},{name:"skipLibCheck",type:"boolean"},{name:"allowUnusedLabels",type:"boolean"},{name:"allowUnreachableCode",type:"boolean"},{name:"suppressExcessPropertyErrors",type:"boolean"},{name:"suppressImplicitAnyIndexErrors",type:"boolean"},{name:"forceConsistentCasingInFileNames",type:"boolean"},{name:"maxNodeModuleJsDepth",type:"number"},{name:"noStrictGenericChecks",type:"boolean"},{name:"useDefineForClassFields",type:"boolean"},{name:"preserveValueImports",type:"boolean"},{name:"keyofStringsOnly",type:"boolean"},{name:"plugins",type:"list"},{name:"moduleDetection",type:{auto:2,legacy:1,force:3}},{name:"ignoreDeprecations",type:"string"}],Le=_e(import.meta.url),X="vuepress-plugin-md-enhance",$=new he(X),b=ke(Me.resolve(Le,"../client")),Ve=(e,t=!0)=>{const n=be(e,import.meta);return t&&!n&&$.error(`Package ${e} is not installed, please install it manually!`),n},Z=(e,t={})=>{const n=`#code/${Q(e)}`,a=v(t).map(([s,o])=>{const r=Je.find(l=>l.name===s);if(!r||o===null||!$e(o))return"";const{type:i}=r;return C(i)?i[o]?.toString()??"":`${s}=${encodeURIComponent(o)}`}).filter(s=>s.length).join("&");return`${a?`?${a}`:""}${n}`},ee=({service:e="https://www.typescriptlang.org/play",...t}={})=>({name:"playground#ts",propsGetter:({title:n="",files:a,settings:s})=>{const o=x(a).filter(i=>M(i,".ts"));o.length!==1&&$.error("TS playground only support 1 ts file");const r=`${e}${Z(a[o[0]].content,T({},s,t))}`;return{title:n,link:encodeURIComponent(r)}}}),Ge=["html","js","ts","vue","jsx","tsx","json"],te="https://sfc.vuejs.org/vue.runtime.esm-browser.js",ne="https://sfc.vuejs.org/server-renderer.esm-browser.js",oe=(e={})=>({name:"playground#vue",propsGetter:t=>{const{title:n="",files:a,settings:s}=t,o={service:"https://sfc.vuejs.org/",dev:!1,ssr:!1,...e,...s},r=I(v(a).filter(([,{ext:i}])=>Ge.includes(i)).map(([i,{content:l}])=>{if(i==="import-map.json"){const f=JSON.parse(l);return[i,JSON.stringify(T({imports:{vue:te,...o.ssr?{"vue/server-renderer":ne}:{}}},f),null,2)]}return[i,l]}));return o.ssr&&!r["import-map.json"]&&(r["import-map.json"]=JSON.stringify({imports:{vue:te,"vue/server-renderer":ne}},null,2)),{title:n,link:encodeURIComponent(`${o.service}#${o.dev?"__DEV__":""}${o.ssr?"__SSR__":""}${Buffer.from(JSON.stringify(r)).toString("base64")}`)}}}),U=(e,t,n="&")=>t?`${n}${e}=${Q(t)}`:"",se=(e,t,n,a)=>{const s=`/${U("html",t,"?")}${U("config",a)}${U("css",n)}`;return`${e}${s}`},ae=({service:e="https://unocss.dev/play"}={})=>({name:"playground#unocss",propsGetter:({title:t="",files:n})=>{const a=x(n).filter(l=>M(l,".html")),s=x(n).filter(l=>M(l,".css")),o=x(n).filter(l=>M(l,".js")||M(l,".ts"));(a.length>1||s.length>1||o.length>1)&&$.error("UnoCSS playground only support 1 html/css/config file");const r=l=>l.length===1?n[l[0]].content:"",i=se(e,r(a),r(s),r(o));return{title:t,link:encodeURIComponent(i)}}}),re=e=>{e.use(j,{name:"kotlin-playground",component:"KotlinPlayground",propsGetter:({title:t="",key:n,files:a,settings:s})=>({title:t,key:n,settings:encodeURIComponent(JSON.stringify(s)),files:k(JSON.stringify(v(a).map(([,{content:o}])=>o)))})})},ie=(e,t)=>{const n=e[t],a=`markmap-${t}`,{content:s}=n;return`<MarkMap id="${a}" content="${k(s)}"></MarkMap>`},le=e=>{const{fence:t}=e.renderer.rules;e.renderer.rules.fence=(...n)=>{const[a,s]=n,{info:o}=a[s],[r]=o.split(":",2);return r==="markmap"?ie(a,s):t(...n)},e.renderer.rules.markmap=ie},qe=(e,t)=>`<Mermaid id="mermaid-${t}" code="${k(e[t].content)}"></Mermaid>`,pe=({diagram:e="mermaid",content:t,title:n="",indent:a=e!=="mermaid"})=>{const[,s,o]=/^\s*---\n([^]*?)\n---\n\n([\s\S]*)\s*$/m.exec(t)??[null,"",t],r=(!n||/^title:\s*(.*)/m.test(s)?s:`title: ${n}
${s}`).trim();return`${r?`---
${r}
---
`:""}${e==="mermaid"?"":`${e}
`}${a?o.split(`
`).map(i=>i?` ${i}`:"").join(`
`):o}`},me=(e,t)=>`<Mermaid id="mermaid-${t}" code="${k(pe(e))}"${e.title?` title="${k(e.title)}"`:""}></Mermaid>`,A={class:["classDiagram"],c4c:["C4Context"],er:["erDiagram"],gantt:[""],"git-graph":["gitGraph"],journey:[""],mindmap:[""],kanban:[""],pie:[""],quadrant:["quadrantChart"],requirement:["requirementDiagram"],sequence:["sequenceDiagram"],state:["stateDiagram-v2"],timeline:[""],architecture:["architecture-beta"],block:["block-beta",!1],packet:["packet-beta",!1],sankey:["sankey-beta",!1],xy:["xychart-beta",!1],radar:["radar-beta"]},ce=e=>{const{fence:t}=e.renderer.rules;e.renderer.rules.fence=(...n)=>{const[a,s]=n,{content:o,info:r}=a[s],i=r.trim();if(i==="mermaid")return me({content:o,indent:!1},s);const[l,...f]=i.split(" ");return l in A?me({diagram:A[l][0]||l,title:f.join(" "),content:o,indent:A[l][1]??!0},s):t(...n)},e.renderer.rules.mermaid=qe},N=(e,t=["chronology","gantt","json","latex","math","mindmap","regex","salt","uml","wbs","yaml"].map(n=>({name:n})))=>{t.forEach(n=>{e.use(Ee,n)})},Be=["html","js","ts","vue","jsx","tsx","json"],Ke=e=>Buffer.from(JSON.stringify(I(v(e).filter(([,{ext:t}])=>Be.includes(t)).map(([t,{content:n}])=>[t,n])))).toString("base64"),de=e=>{e.use(j,{name:"vue-playground",component:"VuePlayground",propsGetter:({title:t="",key:n,files:a,settings:s})=>({title:t,key:n,settings:encodeURIComponent(JSON.stringify(s)),files:encodeURIComponent(Ke(a))})})},We=e=>JSON.stringify(I(v(e).map(([t,n])=>[t,typeof n=="string"||n.active||n.hidden||n.readOnly?n:n.code]))),ue=e=>{const t=/.*(?<!\\)\[([^}]*)\].*/.exec(e);if(t?.[1]){const n=t[1].split(" ");return I(n.filter(a=>a.trim().length>0).map(a=>{const[s,o=""]=a.trim().split("=",2);return[s,o]}))}return{}},fe="@",He=["file","options","setup"],Ye=e=>({title:e.title??"",template:e.template??"",files:k(We(e.files)),options:k(JSON.stringify(e.options??{})),customSetup:k(JSON.stringify(e.customSetup??{}))}),ge=e=>new Function(`return ${e};`)(),Qe=(e,t,n,a)=>{let s=e.bMarks[t]+e.tShift[t],o=e.eMarks[t];if(e.src[s]!==":")return!1;let r=s+1;for(;r<=o&&e.src[r]===":";)r+=1;const i=r-s;if(i<3)return!1;const l=e.src.slice(s,r),f=e.src.slice(r,o).trim(),u=f.indexOf(" ");let m="",y="";if(u>0?(m=f.substring(0,u),y=f.substring(u+1).replace(/(?<!\\)\[([^}]*)\]/g,"")):m=f,!m.startsWith("sandpack"))return!1;if(a)return!0;let c=t,d=!1;for(;c<n&&(c+=1,s=e.bMarks[c]+e.tShift[c],o=e.eMarks[c],!(s<o&&e.sCount[c]<e.blkIndent));)if(e.src[s]===":"&&e.sCount[c]-e.blkIndent<4){for(r=s+1;r<=o&&e.src[r]===":";r++);if(r-s>=i&&(r=e.skipSpaces(r),r>=o)){d=!0;break}}const p=e.parentType,g=e.lineMax;e.parentType="sandpack",e.lineMax=c-(d?1:0);const h=e.push("sandpack_open","template",1);h.markup=l,h.content=f,h.block=!0,h.info=y,h.map=[t,c-(d?1:0)],e.md.block.tokenize(e,t+1,c-(d?1:0));const S=e.push("sandpack_close","template",-1);return S.markup=e.src.slice(s,r),S.block=!0,e.parentType=p,e.lineMax=g,e.line=c+(d?1:0),!0},Xe=e=>(t,n,a,s)=>{let o=t.bMarks[n]+t.tShift[n],r=t.eMarks[n];const i=`${fe}${e}`;if(t.src.charAt(o)!=="@")return!1;let l;for(l=0;l<i.length;l++)if(i[l]!==t.src[o+l])return!1;const f=t.src.slice(o,o+l),u=t.src.slice(o+l,r);if(s)return!0;let m=n,y=!1;for(;m<a&&(m+=1,o=t.bMarks[m]+t.tShift[m],r=t.eMarks[m],!(o<r&&t.sCount[m]<t.blkIndent));)if(t.src[o]===fe&&t.sCount[m]<=t.sCount[n]){let h=!0;for(l=0;l<i.length;l++)if(i[l]!==t.src[o+l]){h=!1;break}if(h){y=!0,m-=1;break}}const c=t.parentType,d=t.lineMax;t.parentType=e,t.lineMax=m;const p=t.push(`${e}_open`,"template",1);p.block=!0,p.markup=f,p.info=u.trim(),p.map=[n,m],t.md.block.tokenize(t,n+1,m);const g=t.push(`${e}_close`,"template",-1);return g.block=!0,g.markup="",t.parentType=c,t.lineMax=d,t.line=m+(y?1:0),!0},ye=e=>{e.block.ruler.before("fence","sandpack",Qe,{alt:["paragraph","reference","blockquote","list"]}),He.forEach(t=>{e.block.ruler.__rules__.find(({name:n})=>n===`at-${t}`)||e.block.ruler.before("fence",`at-${t}`,Xe(t),{alt:["paragraph","reference","blockquote","list"]})}),e.renderer.rules.sandpack_open=(t,n)=>{const{content:a,info:s}=t[n],o=ue(a),r={title:encodeURIComponent(s),files:{},options:{},customSetup:{}},i=a.split(" ",2)[0].trim().split("#");i.length>1&&(r.template=i[1]);let l=null,f=!1,u=!1;for(let y=n;y<t.length;y++){const{block:c,type:d,info:p,content:g}=t[y];if(c){if(d==="sandpack_close")break;if(d==="sandpack_open")continue;if(d==="file_open"){if(!p)continue;l=p.trim().split(" ")[0];const h=ue(p);r.files[l]={code:"",active:"active"in h,hidden:"hidden"in h,readOnly:"readOnly"in h}}else(d==="file_close"||d==="setup_open"||d==="options_open")&&(l=null);if(d==="setup_open"?u=!0:d==="setup_close"?u=!1:d==="options_open"?f=!0:d==="options_close"&&(f=!1),d==="file_close"||d==="setup_close"||d==="options_close"||!g){t[y].type="sandpack_empty",t[y].hidden=!0;continue}f&&(d==="fence"&&(p==="js"||p==="javascript")&&(r.options=ge(g.trim())),f=!1),u&&(d==="fence"&&(p==="js"||p==="javascript")&&(r.customSetup=ge(g.trim())),u=!1),d==="fence"&&l&&(r.files[l].code=g),t[y].type="sandpack_empty",t[y].hidden=!0}}const m=Ye(r);return`<SandPack ${x(o).length?`${v(o).map(([y,c])=>c?`${y}="${R(c)}"`:y).join(" ")} `:""}${v(m).map(([y,c])=>c?`${y}="${R(c)}"`:null).filter(Boolean).join(" ")}>
`},e.renderer.rules.sandpack_close=()=>`</SandPack>
`},Ze=e=>{_(e,{name:"chart",openRender:(t,n,a,s)=>{$.warn(`chart container is deprecated, use chartjs container instead${s.filePathRelative?` found in ${s.filePathRelative}`:""}.`);const o=t[n].info.trimStart().slice(5).trim(),r=`chart-${n}`;let i="{}",l="";for(let f=n;f<t.length;f++){const{type:u,content:m,info:y}=t[f];if(u==="container_chart_close")break;m&&(u==="fence"&&(y==="json"?(i=k(m),l="json"):(y==="js"||y==="javascript")&&(i=k(m),l="js")),t[f].type="chart_empty",t[f].hidden=!0)}return`<ChartJS id="${r}" config="${i}" ${o?`title="${encodeURIComponent(o)}" `:""}type="${l}">`},closeRender:()=>"</ChartJS>"})},et=e=>{const{deprecatedLogger:t,droppedLogger:n}=Oe("md-enhance");t({options:e,old:"chart",new:"chartjs"}),n({options:e,old:"enableAll",msg:"Please manually enable the features you need."}),n({options:e,old:"imageFix",msg:"This option is no longer needed."}),n({options:e,old:"lineNumbers",msg:"Please use lineNumbers option in @vuepress/plugin-prismjs or @vuepress/plugin-shiki !"}),n({options:e,old:"alert",msg:"Please use alert option in @vuepress/plugin-markdown-hint instead."}),n({options:e,old:"container",msg:"Please use hint option in @vuepress/plugin-markdown-hint instead."}),n({options:e,old:"hint",msg:"Please use hint option in @vuepress/plugin-markdown-hint instead."}),n({options:e,old:"mdImport",msg:"Please use @vuepress/plugin-markdown-include instead."}),n({options:e,old:"include",msg:"Please use @vuepress/plugin-markdown-include instead."}),n({options:e,old:"imageTitle",msg:"Please use figure option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"figure",msg:"Please use figure option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"imageMark",msg:"Please use mark option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"imgMark",msg:"Please use mark option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"imgSize",msg:"Please use size option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"imageSize",msg:"Please use size option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"lazyload",msg:"Please use lazyload option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"imageLazyload",msg:"Please use lazyload option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"imgLazyload",msg:"Please use lazyload option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"obsidianImgSize",msg:"Please use obsidianSize option in @vuepress/plugin-markdown-image instead."}),n({options:e,old:"linkCheck",msg:"Please use @vuepress/plugin-links-check instead."}),n({options:e,old:"checkLinks",msg:"Please use @vuepress/plugin-links-check instead."}),n({options:e,old:"tex",msg:"Please use @vuepress/plugin-markdown-math instead."}),n({options:e,old:"katex",msg:"Please use @vuepress/plugin-markdown-math instead."}),n({options:e,old:"mathjax",msg:"Please use @vuepress/plugin-markdown-math instead."}),n({options:e,old:"revealjs",msg:"Please use @vuepress/plugin-revealjs instead."}),n({options:e,old:"revealJs",msg:"Please use @vuepress/plugin-revealjs instead."}),n({options:e,old:"presentation",msg:"Please use @vuepress/plugin-revealjs instead."}),n({options:e,old:"codegroup",msg:"Please use codeTabs option in @vuepress/plugin-markdown-tab instead."}),n({options:e,old:"codetabs",msg:"Please use codeTabs option in @vuepress/plugin-markdown-tab instead."}),n({options:e,old:"tabs",msg:"Please use tabs option in @vuepress/plugin-markdown-tab instead."}),n({options:e,old:"vpre",msg:"Please use vPre option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"vPre",msg:"Please use vPre option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"breaks",msg:"Please use breaks option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"linkify",msg:"Please use linkify option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"gfm",msg:"Please use gfm option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"footnote",msg:"Please use footnote option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"tasklist",msg:"Please use tasklist option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"component",msg:"Please use component option in @vuepress/plugin-markdown-ext instead."}),n({options:e,old:"align",msg:"Please use align option in @vuepress/plugin-markdown-stylize instead."}),n({options:e,old:"attrs",msg:"Please use attrs option in @vuepress/plugin-markdown-stylize instead."}),n({options:e,old:"mark",msg:"Please use mark option in @vuepress/plugin-markdown-stylize instead."}),n({options:e,old:"spoiler",msg:"Please use spoiler option in @vuepress/plugin-markdown-stylize instead."}),n({options:e,old:"sub",msg:"Please use sub option in @vuepress/plugin-markdown-stylize instead."}),n({options:e,old:"sup",msg:"Please use sup option in @vuepress/plugin-markdown-stylize instead."}),n({options:e,old:"stylize",msg:"Please use custom option in @vuepress/plugin-markdown-stylize instead."}),e.card&&$.error(`${w.magenta("card")} is deprecated, please import ${w.magenta("VPCard")} from "vuepress-plugin-components" and use it instead.`),C(e.mermaid)&&($.error(`Customizing mermaid with option ${w.magenta("mermaid")} is no longer supported, please import and use ${w.magenta("defineMermaidConfig")} from ${w.magenta("vuepress-plugin-md-enhance/client")} instead.`),e.mermaid=!0),C(e.vuePlayground)&&($.error(`Customizing @vue/repl with option ${w.magenta("vuePlayground")} is no longer supported, please import and use ${w.magenta("defineVuePlaygroundConfig")} from ${w.magenta("vuepress-plugin-md-enhance/client")} instead.`),e.vuePlayground=!0)},tt=e=>{e.use(_,{name:"demo",openRender:(t,n)=>{$.warn("demo container is deprecated, you should use normal-demo, react-demo and vue-demo container instead.");const{info:a}=t[n],s=/\[(.*)\]/u.exec(a),o=/^ demo\s*(?:\[.*?\])?\s*(.*)\s*$/u.exec(a);let r="";const i={};for(let l=n;l<t.length;l++){const{type:f,content:u,info:m}=t[l],y=m?/^([^ :[{]+)/.exec(e.utils.unescapeAll(m).trim())?.[1]??"text":"";if(f==="container_demo_close")break;u&&f==="fence"&&(y==="json"?r=k(u):i[y]=u)}return`
<CodeDemo id="code-demo-${n}" type="${s?.[1]??"normal"}"${o?` title="${encodeURIComponent(o[1])}"`:""}${r?` config="${r}"`:""} code="${k(JSON.stringify(i))}">
`},closeRender:()=>"</CodeDemo>"})},nt=e=>{Re(e,{name:"flowchart",open:"flowstart",close:"flowend",render:(t,n)=>{Ie.warn('"@flowstart ... @flowend" is deprecated, you should use ```flow ... ``` instead.');const a=t[n],s=`flowchart-${n}`,{content:o,info:r}=a;return`<FlowChart id="${s}" code="${k(o)}" preset="${r.trim()||"vue"}"></FlowChart>`}})},ot=async(e,t,n)=>{const a=new Set,s=new Set;return n.chartjs&&(a.add(`import ChartJS from "${b}components/ChartJS.js";`),s.add('app.component("ChartJS", ChartJS)')),t.demo&&(a.add(`import CodeDemo from "${b}components/CodeDemo.js";`),a.add(`import MdDemo from "${b}components/MdDemo.js";`),s.add('app.component("CodeDemo", CodeDemo);'),s.add('app.component("MdDemo", MdDemo);')),n.echarts&&(a.add(`import ECharts from "${b}components/ECharts.js";`),s.add('app.component("ECharts", ECharts);')),n.flowchart&&(a.add(`import FlowChart from "${b}components/FlowChart.js";`),s.add('app.component("FlowChart", FlowChart);')),n.kotlinPlayground&&(a.add(`import KotlinPlayground from "${b}components/KotlinPlayground.js";`),s.add('app.component("KotlinPlayground", KotlinPlayground);')),n.markmap&&(a.add(`import MarkMap from "${b}components/MarkMap.js";`),s.add('app.component("MarkMap", MarkMap);')),n.mermaid&&(a.add(`import Mermaid from "${b}components/Mermaid.js";`),s.add('app.component("Mermaid", Mermaid);')),t.playground&&(a.add(`import Playground from "${b}components/Playground.js";`),s.add('app.component("Playground", Playground);')),n.sandpack&&(a.add('import { defineAsyncComponent } from "vue";'),a.add(`import { LoadingIcon } from "${ve("@vuepress/helper/client",import.meta)}";`),s.add(`app.component(
"SandPack",
defineAsyncComponent({
loader: () => import("${b}components/SandPack.js"),
loadingComponent: LoadingIcon,
})
);`)),n.vuePlayground&&(a.add(`import VuePlayground from "${b}components/VuePlayground.js";`),s.add('app.component("VuePlayground", VuePlayground);')),e.writeTemp("md-enhance/config.js",`${Array.from(a.values()).join(`
`)}
export default {
enhance: ({ app }) => {
${Array.from(s.values()).map(o=>o.split(`
`).map(r=>` ${r}`)).flat().join(`
`)}
},
};
`)},st=(e={},t=!0)=>n=>{t&&et(e),n.env.isDebug&&$.info("Options:",e);const a=(o,r=[])=>!!e[o]&&r.every(i=>Ve(i,!!e[o])),s={chartjs:a("chartjs",["chart.js"]),echarts:a("echarts",["echarts"]),flowchart:a("flowchart",["flowchart.ts"]),mark:a("mark"),markmap:a("markmap",["markmap-lib","markmap-toolbar","markmap-view"]),mermaid:a("mermaid",["mermaid"]),kotlinPlayground:a("kotlinPlayground",["kotlin-playground"]),sandpack:a("sandpack",["sandpack-vue3"]),vuePlayground:a("vuePlayground",["@vue/repl"])};return Se(n,{id:"hope"}),{name:X,define:()=>({CODE_DEMO_OPTIONS:{...F,...C(e.demo)?e.demo:{}},VUE_PLAYGROUND_MONACO:C(e.vuePlayground)&&e.vuePlayground.editor==="monaco"}),extendsBundlerOptions:(o,r)=>{we(o,r,["@vuepress/helper","fflate","vuepress-shared"]),s.chartjs&&(O(o,r,"chart.js/auto/auto.mjs"),P(o,r,"chart.js")),s.echarts&&(O(o,r,"echarts"),P(o,r,"echarts")),s.flowchart&&(O(o,r,"flowchart.ts"),P(o,r,"flowchart.ts")),s.markmap&&(D(o,r,["markmap-lib","markmap-toolbar","markmap-view"]),P(o,r,["markmap-lib","markmap-toolbar","markmap-view"])),s.mermaid&&(O(o,r,"mermaid/dist/mermaid.esm.min.mjs"),P(o,r,"mermaid")),s.kotlinPlayground&&(D(o,r,"kotlin-playground"),Ce(o,r,"kotlin-playground"),P(o,r,"kotlin-playground")),s.sandpack&&(D(o,r,"sandpack-vue3"),P(o,r,"sandpack-vue3")),s.vuePlayground&&(D(o,r,"@vue/repl"),P(o,r,"@vue/repl"),je(o,r,i=>{i.resolve.set("conditionNames",["browser","import","module"]),i.module.set("exprContextCritical",!1),i.module.set("unknownContextCritical",!1)}))},extendsMarkdown:o=>{if(s.flowchart&&(o.use(W),t&&o.use(nt)),s.chartjs&&(o.use(z),t&&o.use(Ze)),s.echarts&&o.use(B),Pe(e.plantuml)?o.use(N,e.plantuml):e.plantuml&&o.use(N),e.demo&&(o.use(G),o.use(J),o.use(L),o.use(V),t&&o.use(tt)),s.markmap&&o.use(le),s.mermaid&&o.use(ce),C(e.playground)){const{presets:r=[],config:i={}}=e.playground;r.forEach(l=>{l==="ts"?o.use(j,ee(i.ts??{})):l==="vue"?o.use(j,oe(i.vue??{})):l==="unocss"?o.use(j,ae(i.unocss??{})):C(l)&&o.use(j,l)})}s.kotlinPlayground&&o.use(re),s.vuePlayground&&o.use(de),s.sandpack&&o.use(ye)},clientConfigFile:o=>ot(o,e,s)}};export{F as CODE_DEMO_DEFAULT_SETTING,z as chartjs,B as echarts,W as flowchart,se as generateUnoURL,pe as getMermaidContent,ee as getTSPlaygroundPreset,Z as getURL,ae as getUnoPlaygroundPreset,oe as getVuePlaygroundPreset,re as kotlinPlayground,le as markmap,G as mdDemo,st as mdEnhancePlugin,ce as mermaid,J as normalDemo,N as plantuml,j as playground,V as reactDemo,ye as sandpack,L as vueDemo,de as vuePlayground};
//# sourceMappingURL=index.js.map