feat: refine HTML transformation to handle root-relative links for img and link tags

This commit is contained in:
二刺螈 2024-11-07 17:12:59 +08:00
parent cc14590e5c
commit 33ae1240bb

View File

@ -87,9 +87,15 @@ export const transformHtml = (code: string) => {
e.setAttribute(attr, mirrorBaseUrl + e.getAttribute(attr));
});
});
doc.querySelectorAll('link[href^="/"]').forEach((e) => {
doc.querySelectorAll('[href^="/"]').forEach((e) => {
const tag = e.tagName.toLowerCase();
const href = e.getAttribute('href');
if (href && href.lastIndexOf('/') === 0) {
if (
(tag === 'img' || tag === 'link') &&
href &&
href.lastIndexOf('/') === 0
) {
e.setAttribute('href', mirrorBaseUrl + href);
}
});