import Sitemapper from '@context-labs/sitemapper'; import * as cheerio from 'cheerio'; const getFavicon = async (url: string): Promise => { const res = await fetch(url); const html = await res.text(); const $ = cheerio.load(html); const favicon = $('link[rel="shortcut icon"]').prop('href') ?? $('link[rel="icon"]').prop('href') ?? $('link[rel="apple-touch-icon"]').prop('href'); if (favicon?.includes?.('http')) { return favicon; } return `${url}${favicon}`; }; const parse = async (url: string) => { const sitemap = new Sitemapper({ url, timeout: 60000, }); const { sites } = await sitemap.fetch(); const { origin, host } = new URL(url); const favicon = await getFavicon(origin); return { title: host, sites: [...new Set(sites)], // dedupe imageUrl: favicon, }; }; const SiteMpaUtil = { parse, }; export default SiteMpaUtil;