fix: image base path

This commit is contained in:
lisonge 2024-04-19 15:23:55 +08:00
parent afae025044
commit 78d3ee7dba

View File

@ -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 });
};