pyseoa-ts
Version:
SEO analysis tools for the web, ported from pyseoa (Python) to TypeScript
22 lines (21 loc) • 568 B
JavaScript
import * as cheerio from 'cheerio';
export function extractLinks(html, baseUrl) {
const $ = cheerio.load(html);
const base = new URL(baseUrl);
const links = [];
$("a[href]").each((_, el) => {
let href = $(el).attr("href");
if (!href)
return;
try {
const url = new URL(href, base);
if (url.hostname === base.hostname) {
links.push(url.toString());
}
}
catch {
// ignore malformed URLs
}
});
return [...new Set(links)];
}