chore(package.json): replace JSDom with cheerio for faster relative link parsing

This commit is contained in:
Cory Dransfeldt 2025-06-03 14:01:30 -07:00
parent 13cb918edb
commit 3ec3dfc949
No known key found for this signature in database
3 changed files with 171 additions and 444 deletions

View file

@ -1,27 +1,25 @@
import { JSDOM } from "jsdom";
import * as cheerio from 'cheerio';
export default {
convertRelativeLinks: (htmlContent, domain) => {
if (!htmlContent || !domain) return htmlContent;
const dom = new JSDOM(htmlContent);
const document = dom.window.document;
const $ = cheerio.load(htmlContent);
document.querySelectorAll("a[href]").forEach(link => {
let href = link.getAttribute("href");
$("a[href]").each((_, link) => {
const $link = $(link);
let href = $link.attr("href");
if (href.startsWith("#")) {
const span = document.createElement("span");
span.textContent = link.textContent;
link.replaceWith(span);
return;
$link.replaceWith($("<span>").text($link.text()));
} else if (!href.startsWith("http://") && !href.startsWith("https://")) {
const normalizedDomain = domain.replace(/\/$/, '');
const normalizedHref = href.replace(/^\/+/, '');
$link.attr("href", `${normalizedDomain}/${normalizedHref}`);
}
if (!href.startsWith("http://") && !href.startsWith("https://"))
link.setAttribute("href", `${domain.replace(/\/$/, '')}/${href.replace(/^\/+/, '')}`);
});
return document.body.innerHTML;
return $.html();
},
generatePermalink: (url, baseUrl) => {
if (url?.includes("http") || !baseUrl) return url