Skip to content

Commit

Permalink
fix: elements
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 14, 2024
1 parent e6d448f commit d9f3878
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions docs/.vitepress/plugins/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ export const transformHtml = (code: string) => {
if (!code.includes('/assets/')) return;
// 注意: 如果使用 htmlparser2+dom-serializer, 当 md 文件包含 `<<n` 将出现 Hydration mismatches 错误
const doc = new Parser().parseFromString(code, 'text/html');
doc.querySelectorAll('link[href^="/assets/"]').forEach((e) => {
e.setAttribute('href', mirrorBaseUrl + e.getAttribute('href'));
});
doc.querySelectorAll('script[src^="/assets/"]').forEach((e) => {
e.setAttribute('href', mirrorBaseUrl + e.getAttribute('src'));
const elements = {
link: 'href',
script: 'src',
};
Object.entries(elements).forEach(([tag, attr]) => {
doc.querySelectorAll(`${tag}[${attr}^="/assets/"]`).forEach((e) => {
e.setAttribute(attr, mirrorBaseUrl + e.getAttribute(attr));
});
});
return doc.documentElement.outerHTML;
};

0 comments on commit d9f3878

Please sign in to comment.