Skip to content

Commit

Permalink
fix: image base path
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Apr 19, 2024
1 parent afae025 commit 78d3ee7
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions docs/.vitepress/plugins/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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`) &&
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
};

0 comments on commit 78d3ee7

Please sign in to comment.