feat: remove dependency on jsdom

This commit is contained in:
Devin Haska 2025-05-29 16:31:10 -07:00
parent 446012b231
commit 7c621fedce
Signed by: wonderfulfrog
SSH key fingerprint: SHA256:ejOGyH9064rJiikox4ykOHLeuUg1f9l8wmJxs+MzNw0
3 changed files with 8 additions and 1208 deletions

View file

@ -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 {