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 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 => { export const mirror = (): Plugin | undefined => {
if (!useMirror) return; if (!useMirror) return;
return { return {
@ -34,6 +45,9 @@ export const mirror = (): Plugin | undefined => {
`${mirrorBaseUrl}/assets/`, `${mirrorBaseUrl}/assets/`,
); );
} }
if (chunk.type == 'chunk' && chunk.fileName.endsWith(`.js`)) {
chunk.code = replaceImageSrc(chunk.code);
}
if ( if (
chunk.type == 'chunk' && chunk.type == 'chunk' &&
chunk.fileName.endsWith(`.js`) && 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) => { export const transformHtml = (code: string) => {
if (!useMirror) return; if (!useMirror) return;
const doc = parseDocument(code); const doc = parseDocument(code);
@ -102,10 +112,7 @@ export const transformHtml = (code: string) => {
); );
}, doc.children); }, doc.children);
images.forEach((e) => { images.forEach((e) => {
e.attribs.src = e.attribs.src.replace( e.attribs.src = replaceImageSrc(e.attribs.src);
'https://a.gkd.li/',
`https://registry.npmmirror.com/@gkd-kit/assets/${imageAssetVersion}/files/assets/`,
);
}); });
return render(doc, { encodeEntities: false }); return render(doc, { encodeEntities: false });
}; };