diff --git a/docs/.vitepress/plugins/mirror.ts b/docs/.vitepress/plugins/mirror.ts index b225a4e..13be4fe 100644 --- a/docs/.vitepress/plugins/mirror.ts +++ b/docs/.vitepress/plugins/mirror.ts @@ -16,6 +16,17 @@ const mirrorBaseUrl = `https://registry.npmmirror.com/@gkd-kit/docs/${selfPkg.ve const includesDynamicImport = /import\s*\(/; +const imageAssetVersion = await fetch( + 'https://registry.npmmirror.com/@gkd-kit/assets/latest/files/package.json', +).then((r) => r.json().then((j) => j.version as string)); + +const replaceImageSrc = (src: string): string => { + return src.replaceAll( + 'https://a.gkd.li/', + `https://registry.npmmirror.com/@gkd-kit/assets/${imageAssetVersion}/files/assets/`, + ); +}; + export const mirror = (): Plugin | undefined => { if (!useMirror) return; return { @@ -34,6 +45,9 @@ export const mirror = (): Plugin | undefined => { `${mirrorBaseUrl}/assets/`, ); } + if (chunk.type == 'chunk' && chunk.fileName.endsWith(`.js`)) { + chunk.code = replaceImageSrc(chunk.code); + } if ( chunk.type == 'chunk' && chunk.fileName.endsWith(`.js`) && @@ -69,10 +83,6 @@ export const mirror = (): Plugin | undefined => { }; }; -const imageAssetVersion = await fetch( - 'https://registry.npmmirror.com/@gkd-kit/assets/latest/files/package.json', -).then((r) => r.json().then((j) => j.version as string)); - export const transformHtml = (code: string) => { if (!useMirror) return; const doc = parseDocument(code); @@ -102,10 +112,7 @@ export const transformHtml = (code: string) => { ); }, doc.children); images.forEach((e) => { - e.attribs.src = e.attribs.src.replace( - 'https://a.gkd.li/', - `https://registry.npmmirror.com/@gkd-kit/assets/${imageAssetVersion}/files/assets/`, - ); + e.attribs.src = replaceImageSrc(e.attribs.src); }); return render(doc, { encodeEntities: false }); };