jccp_a/vuepress/node_modules/@vuepress/plugin-shiki/lib/node/index.js
hellcat ac91e1fbe4 1
2025-11-12 17:57:30 +08:00

15 lines
7.7 KiB
JavaScript

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