feat: remove dependency on jsdom
This commit is contained in:
parent
446012b231
commit
7c621fedce
3 changed files with 8 additions and 1208 deletions
|
@ -1,29 +1,15 @@
|
|||
import { JSDOM } from "jsdom";
|
||||
|
||||
// From coryd.dev
|
||||
// https://www.coryd.dev/posts/2025/generating-absolute-urls-in-my-rss-feeds/
|
||||
const convertRelativeLinks = (htmlContent, url) => {
|
||||
if (!htmlContent || !url) return htmlContent;
|
||||
|
||||
const dom = new JSDOM(htmlContent);
|
||||
const document = dom.window.document;
|
||||
const baseUrl = url.replace(/\/$/, "");
|
||||
|
||||
document.querySelectorAll("a[href]").forEach((link) => {
|
||||
let href = link.getAttribute("href");
|
||||
|
||||
if (href.startsWith("#")) {
|
||||
link.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!href.startsWith("http://") && !href.startsWith("https://"))
|
||||
link.setAttribute(
|
||||
"href",
|
||||
`${url.replace(/\/$/, "")}/${href.replace(/^\/+/, "")}`,
|
||||
);
|
||||
});
|
||||
|
||||
return document.body.innerHTML;
|
||||
return htmlContent.replace(
|
||||
/(href|src)=(['"])(?![a-z][a-z0-9+.-]*:|\/\/)([^'"]+)\2/gi,
|
||||
(_, attr, quote, relUrl) => {
|
||||
const clean = relUrl.replace(/^\//, "");
|
||||
return `${attr}=${quote}${baseUrl}/${clean}${quote}`;
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export default {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue