UNPKG

rsshub

Version:
134 lines (132 loc) 4.57 kB
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs"; import "./config-C37vj7VH.mjs"; import { t as ViewType } from "./types-D84BRIt4.mjs"; import "./dist-BInvbO1W.mjs"; import "./logger-Czu8UMNd.mjs"; import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs"; import { t as parseDate } from "./parse-date-BrP7mxXf.mjs"; import { t as cache_default } from "./cache-Bo__VnGm.mjs"; import { t as art } from "./render-BQo6B4tL.mjs"; import path from "node:path"; import { load } from "cheerio"; //#region lib/routes/rockthejvm/articles.ts init_esm_shims(); const handler = async (ctx) => { const limit = Number.parseInt(ctx.req.query("limit") ?? "30", 10); const baseUrl = "https://rockthejvm.com"; const targetUrl = new URL("articles/1", baseUrl).href; const $ = load(await ofetch_default(targetUrl)); const language = $("html").attr("lang") ?? "en"; $("footer").remove(); let items = []; items = $("h2").slice(0, limit).toArray().map((el) => { const $el = $(el).parent().parent(); const $aEl = $el.find("h2 a"); const title = $aEl.text(); const description = art(path.join(__dirname, "templates/description-ca64252b.art"), { intro: $el.find("p.text-content").first().text() }); const pubDateStr = $el.find("time").attr("datetime"); const linkUrl = $aEl.attr("href"); const categoryEls = $el.find("a.bg-tag").toArray(); const categories = [...new Set(categoryEls.map((el$1) => $(el$1).text()).filter(Boolean))]; const authors = $el.find("picture").toArray().map((authorEl) => { const $authorEl = $(authorEl).parent(); return { name: $authorEl.find("p.text-content").text(), url: $authorEl.find("p.text-content a").attr("href") ? new URL($authorEl.find("p.text-content a").attr("href"), baseUrl).href : void 0, avatar: $authorEl.find("img").attr("src") ? new URL($authorEl.find("img").attr("src"), baseUrl).href : void 0 }; }); const upDatedStr = pubDateStr; return { title, description, pubDate: pubDateStr ? parseDate(pubDateStr) : void 0, link: linkUrl ? new URL(linkUrl, baseUrl).href : void 0, category: categories, author: authors, content: { html: description, text: description }, updated: upDatedStr ? parseDate(upDatedStr) : void 0, language }; }); items = await Promise.all(items.map((item) => { if (!item.link) return item; return cache_default.tryGet(item.link, async () => { const $$ = load(await ofetch_default(item.link)); const title = $$("meta[property=\"og:title\"]").attr("content") ?? item.title; const description = item.description + art(path.join(__dirname, "templates/description-ca64252b.art"), { description: $$("div.prose").html() }); const pubDateStr = $$("meta[property=\"article:published_time\"]").attr("content"); const categoryEls = $$("meta[property=\"article:tag\"]").toArray(); const categories = [...new Set(categoryEls.map((el) => $$(el).attr("content")).filter(Boolean))]; const authors = $$("meta[property=\"article:author\"]").toArray().map((authorEl) => { return { name: $$(authorEl).attr("content"), url: void 0, avatar: void 0 }; }); const image = $$("meta[property=\"og:image\"]").attr("content"); const upDatedStr = pubDateStr; const processedItem = { title, description, pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate, category: categories, author: authors, content: { html: description, text: description }, image, banner: image, updated: upDatedStr ? parseDate(upDatedStr) : item.updated, language }; return { ...item, ...processedItem }; }); })); return { title: $("title").text(), description: $("meta[property=\"og:description\"]").attr("content"), link: targetUrl, item: items, allowEmpty: true, image: $("meta[property=\"og:image\"]").attr("content"), author: $("meta[property=\"og:site_name\"]").attr("content"), language, id: $("meta[property=\"og:url\"]").attr("content") }; }; const route = { path: "/articles", name: "Article", url: "rockthejvm.com", maintainers: ["nczitzk"], handler, example: "/rockthejvm/articles", parameters: void 0, description: void 0, categories: ["programming"], features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportRadar: true, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["rockthejvm.com/articles"], target: "/articles" }], view: ViewType.Articles }; //#endregion export { handler, route };