fuc-new/doc/node_modules/@vuepress/plugin-shiki/lib/node/index.js
hellcat 6adbc04d4b 1
2025-10-03 17:17:08 +08:00

15 lines
7.4 KiB
JavaScript

import{Logger as T,getModulePath as h,isModuleAvailable as P}from"@vuepress/helper";import{resolveWhitespacePosition as N,lineNumbers as A,collapsedLines as C,codeBlockTitle as H}from"@vuepress/highlighter-helper";import{isPlainObject as w}from"vuepress/shared";import{colors as v}from"vuepress/utils";import{customAlphabet as W}from"nanoid";import{createRequire as E}from"node:module";import{createHighlighter as x,isSpecialLang as M}from"shiki";import{createSyncFn as S}from"synckit";import{transformerNotationDiff as F,transformerNotationFocus as j,transformerNotationHighlight as O,transformerNotationErrorLevel as B,transformerNotationWordHighlight as D,transformerMetaWordHighlight as R,transformerRenderWhitespace as _,transformerCompactLineOptions as q}from"@shikijs/transformers";const G=s=>{const e={},i=s.render.bind(s);return s.render=(t,a={})=>(e.path=a.filePathRelative,i(t,a)),()=>e.path||"dynamic pages"},I=/\[\\!code/g,U={name:"vuepress:add-class",pre(s){this.addClassToHast(s,"vp-code")}},z={name:"vuepress:cleanup",pre(s){delete s.properties.tabindex}},J={name:"vuepress:remove-escape",postprocess(s){return s.replace(I,"[!code")}},K={name:"vuepress:empty-line",code(s){s.children.forEach(e=>{e.type==="element"&&e.tagName==="span"&&Array.isArray(e.properties.class)&&e.properties.class.includes("line")&&e.children.length===0&&e.children.push({type:"element",tagName:"wbr",properties:{},children:[]})})}},Q={name:"vuepress:v-pre",pre(s){s.properties["v-pre"]=""}},V=/-vue$/,X=/\btwoslash\b/,L="@vuepress/plugin-shiki",y=new T(L),Y=W("abcdefghijklmnopqrstuvwxyz",10),$=s=>s.match(/^([^ :[{]+)/)?.[1]?.replace(V,"").toLowerCase()??"",Z=s=>{const e=s.replace(/^(?:\[.*?\])?.*?\{([\d,-]+)\}.*/,"$1").trim(),i=[];return e?(e.split(",").map(t=>t.split("-").map(a=>parseInt(a,10))).forEach(([t,a])=>{t&&a?i.push(...Array.from({length:a-t+1},(r,l)=>t+l)):i.push(t)}),i.map(t=>({line:t,classes:["highlighted"]}))):[]},ee=E(import.meta.url),se=S(ee.resolve("@vuepress/plugin-shiki/resolveLang")),te=async(s,{langs:e=[],langAlias:i={},defaultLang:t,shikiSetup:a,...r}={},l=!0)=>{const p=await x({langs:[...e,...Object.values(i)],langAlias:i,themes:"themes"in r?Object.values(r.themes):[r.theme??"nord"]}),c=n=>{if(M(n))return!0;if(!p.getLoadedLanguages().includes(n)){const o=se(n);if(!o.length)return!1;p.loadLanguageSync(o)}return!0},d=p.getLanguage;p.getLanguage=n=>{const o=typeof n=="string"?n:n.name;return c($(o)),d.call(p,n)};const m=[];if(l&&m.push(Q),r.twoslash){const{createTwoslashTransformer:n,createFileSystemTypesCache:o}=await import("@vuepress/shiki-twoslash"),{typesCache:u,...g}=w(r.twoslash)?r.twoslash:{};m.push(await n({...g,typesCache:u===!0||typeof u>"u"?o({dir:s.dir.cache("markdown/twoslash")}):u}))}return await a?.(p),{highlighter:p,loadLang:c,extraTransformers:m}},re=s=>{const e=[];return s.notationDiff&&e.push(F()),s.notationFocus&&e.push(j({classActiveLine:"has-focus",classActivePre:"has-focused-lines"})),s.notationHighlight&&e.push(O()),s.notationErrorLevel&&e.push(B()),s.notationWordHighlight&&(e.push(D()),e.push(R())),e.push(U,z,J,K),e},ie=(s,e=!0)=>{const i=N(s,e);return i?[_({position:i})]:[]},b=new Set,ae=(s,{defaultLang:e,logLevel:i},t,a)=>{let r=$(s);return r&&!t(r)&&(i!=="silent"&&!b.has(r)&&(y.warn(`Missing ${v.cyan(s)} highlighter, ${e?`use ${v.cyan(e)} to highlight instead.`:"skip highlighting"}`),b.add(r)),i==="debug"&&y.info(`Unknown language ${v.cyan(r)} found in ${v.cyan(a())}`),r=e||"plain"),r},oe=/\{\{[^]*?\}\}/g,ne=(s,e)=>s.replace(oe,i=>{let t=e.get(i);return t||(t=Y(),e.set(i,t)),t}),le=(s,e)=>{let i=s;return e.forEach((t,a)=>{i=i.replaceAll(t,a)}),i},pe=(s,e)=>{const i=new Map;return le(e(ne(s,i).trimEnd()),i)},he=(s,e,i,t,a)=>{const r=re(e);return(l,p,c)=>pe(l,d=>s.codeToHtml(d,{lang:ae(p,e,t,a),meta:{__raw:c},transformers:[...r,...e.highlightLines??!0?[q(Z(c))]:[],...e.whitespace?ie(c,e.whitespace):[],...i??[],...e.transformers??[]],..."themes"in e?{themes:e.themes,defaultColor:!1}:{theme:e.theme??"nord"}}))},ce=/<pre([\s\S]*?)style="([^"]*)"([^>]*)>/,ue=(s,{preWrapper:e=!0}={})=>{const i=s.renderer.rules.fence;s.renderer.rules.fence=(...t)=>{let a=i(...t);if(!a.startsWith("<pre"))return a;const[r,l,p]=t,c=r[l],d=c.info?s.utils.unescapeAll(c.info).trim():"",m=$(d),n=`${p.langPrefix}${m}`;if(a=a.replace(/<code[^]*?>/,`<code class="${n}">`),!e)return a=`<pre class="${n} ${a.slice(12)}`,a;let o="";return a=a.replace(ce,(u,g,f,k)=>(o=f.trim(),`<pre ${g.trim()}${k.trimEnd()}>`)),`<div class="${n}" data-highlighter="shiki" data-ext="${m}" style="${o}">${a}</div>`}},me=(s,{lineNumbers:e=!0,highlightLines:i=!0,collapsedLines:t="disable",codeBlockTitle:a=!0,notationDiff:r,notationErrorLevel:l,notationFocus:p,notationHighlight:c,notationWordHighlight:d,whitespace:m,twoslash:n})=>{const o=[`import "${h("@vuepress/highlighter-helper/styles/base.css",import.meta)}"`,`import "${h(`${L}/styles/shiki.css`,import.meta)}"`],u=[],g=[];e!=="disable"&&o.push(`import "${h("@vuepress/highlighter-helper/styles/line-numbers.css",import.meta)}"`),(i||c)&&o.push(`import "${h("@vuepress/highlighter-helper/styles/notation-highlight.css",import.meta)}"`),r&&o.push(`import "${h("@vuepress/highlighter-helper/styles/notation-diff.css",import.meta)}"`),l&&o.push(`import "${h("@vuepress/highlighter-helper/styles/notation-error-level.css",import.meta)}"`),p&&o.push(`import "${h("@vuepress/highlighter-helper/styles/notation-focus.css",import.meta)}"`),c&&o.push(`import "${h("@vuepress/highlighter-helper/styles/notation-highlight.css",import.meta)}"`),d&&o.push(`import "${h("@vuepress/highlighter-helper/styles/notation-word-highlight.css",import.meta)}"`),m&&o.push(`import "${h("@vuepress/highlighter-helper/styles/whitespace.css",import.meta)}"`),t!=="disable"&&(o.push(`import "${h("@vuepress/highlighter-helper/styles/collapsed-lines.css",import.meta)}"`,`import { setupCollapsedLines } from "${h("@vuepress/highlighter-helper/client",import.meta)}"`),g.push("setupCollapsedLines()")),a&&o.push(`import "${h("@vuepress/highlighter-helper/styles/code-block-title.css",import.meta)}"`),n&&(o.push(`import { enhanceTwoslash } from "${h("@vuepress/shiki-twoslash/client",import.meta)}"`),o.push(`import "${h("@vuepress/shiki-twoslash/styles/twoslash.css",import.meta)}"`),u.push("enhanceTwoslash(app)"));let f=o.join(`
`);return(g.length||u.length)&&(f+=`
export default {
`,u.length&&(f+=` enhance({ app }) {
${u.join(`
`)}
},
`),g.length&&(f+=` setup() {
${g.join(`
`)}
},
`),f+=`}
`),s.writeTemp("shiki/config.js",f)},ge=(s={})=>e=>{const{code:i}=e.options.markdown,t={...w(i)?i:{},...s};t.logLevel??=e.env.isDebug?"debug":"warn",t.preWrapper??=!0,t.lineNumbers??=!0,t.collapsedLines??="disable",t.codeBlockTitle??=!0,t.twoslash&&!P("@vuepress/shiki-twoslash",import.meta)&&(y.error(`${v.cyan("twoslash")} is enabled, but ${v.magenta("@vuepress/shiki-twoslash")} is not installed, please install it manually`),t.twoslash=!1);let a=!0;return{name:"@vuepress/plugin-shiki",extendsMarkdownOptions:r=>{if(r.vPre!==!1){const l=w(r.vPre)?r.vPre:{block:!0};l.block&&(r.vPre??={},r.vPre.block=!1),a=l.block??!0}else a=!1},extendsMarkdown:async r=>{const{preWrapper:l,lineNumbers:p,collapsedLines:c,codeBlockTitle:d}=t,m=G(r),{highlighter:n,loadLang:o,extraTransformers:u}=await te(e,t,a);r.options.highlight=he(n,t,u,o,m),r.use(ue,{preWrapper:l}),l&&(r.use(A,{lineNumbers:p,resolveLineNumbers(g){return t.twoslash&&X.test(g)?!1:void 0}}),r.use(C,{collapsedLines:c}),r.use(H,{codeBlockTitle:d}))},clientConfigFile:()=>me(e,t)}};export{ge as shikiPlugin};
//# sourceMappingURL=index.js.map