// author: simon-pierre boucher import { parse } from "./dom.js"; import type { PageLink } from "./types.js"; export function extractLinksFromHtml(html: string, base: string): PageLink[] { return extractLinks(parse(html).document, base); } export function extractLinks(document: Document, base: string): PageLink[] { let baseHost: string; try { baseHost = new URL(base).hostname.toLowerCase(); } catch { baseHost = ""; } const seen = new Set(); const links: PageLink[] = []; for (const a of Array.from(document.querySelectorAll("a[href]"))) { const raw = a.getAttribute("href"); if (raw === null || raw === "" || /^(javascript:|mailto:|tel:|#)/i.test(raw)) continue; let abs: URL; try { abs = new URL(raw, base); } catch { continue; } if (abs.protocol !== "http:" && abs.protocol !== "https:") continue; const url = abs.toString(); if (seen.has(url)) continue; seen.add(url); links.push({ url, text: (a.textContent ?? "").replace(/\s+/g, " ").trim(), rel: a.getAttribute("rel"), isInternal: abs.hostname.toLowerCase() === baseHost, }); } return links; }