UNPKG

rsshub

Version:
52 lines (51 loc) 1.76 kB
import { t as rofetch } from "./ofetch-W1aeTHCo.mjs"; import { t as cache_default } from "./cache-CiDoUSLo.mjs"; import { t as parser } from "./rss-parser-Dv4dw4PQ.mjs"; import { load } from "cheerio"; //#region lib/routes/theguardian/utils.ts const rebuildFigure = ($e) => { const img = $e.find("img"); if (img.length === 0) { $e.remove(); return; } const caption = $e.find("figcaption").first().text().trim(); const src = img.attr("src")?.replace(/width=\d+/, "width=700"); $e.replaceWith(`<figure><img alt='${img.attr("alt")}' src='${src}'><br><figcaption>${caption}</figcaption></figure>`); }; const processFeed = (data) => { const $ = load(data); const content = $("#maincontent"); if (content.length === 0) return; content.find("gu-island, ul > li > p> em > strong").remove(); content.find("figure").each((_, e) => rebuildFigure($(e))); const cover = $("div[data-gu-name=\"media\"] figure"); if (cover.length > 0) { rebuildFigure(cover); $("div[data-gu-name=\"media\"] figure").insertBefore(content.children().first()); } return content.html(); }; const getFeed = async (cfg) => { const feed = await parser.parseURL(cfg.rss); const items = await Promise.all(feed.items.slice(0, 10).map((item) => cache_default.tryGet(item.link, async () => { const response = await rofetch(item.link); const description = processFeed(response); const rawCategories = item.categories; return { title: item.title, description: description ?? item.content, pubDate: item.pubDate, link: item.link, category: rawCategories?.map((c) => c._) }; }))); return { title: `The Guardian - ${cfg.title}`, link: cfg.link, description: `The Guardian - ${cfg.title}`, item: items }; }; //#endregion export { getFeed as t };