UNPKG

rsshub

Version:
119 lines (117 loc) 3.58 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/mathpix/blog.ts init_esm_shims(); const handler = async (ctx) => { const limit = Number.parseInt(ctx.req.query("limit") ?? "30", 10); const baseUrl = "https://mathpix.com"; const targetUrl = new URL("blog", baseUrl).href; const $ = load(await ofetch_default(targetUrl)); const language = $("html").attr("lang") ?? "en"; const categoryMap = {}; $("div.navbar-menu a.blog-category").each((_, el) => { const $el = $(el); const id = $el.attr("data-category"); const name = $el.text()?.trim(); if (id && name) categoryMap[id] = name; }); let items = []; items = $("li.articles__item").slice(0, limit).toArray().map((el) => { const $el = $(el); const title = $el.find("a.articles__title").text(); const image = $el.find("div.articles__image img").attr("srcset") ? new URL($el.find("div.articles__image img").attr("srcset"), baseUrl).href : void 0; const description = art(path.join(__dirname, "templates/description-881baa33.art"), { images: image ? [{ src: image, alt: title }] : void 0, intro: $el.find("div.articles__text").text() }); const pubDateStr = $el.find("time.articles__date").attr("datetime"); const linkUrl = $el.find("a.articles__title").attr("href"); const categories = ($el.attr("data-category")?.split(/,\s/).map((category) => category.trim()) ?? []).map((id) => categoryMap[id]).filter(Boolean); const upDatedStr = pubDateStr; return { title, description, pubDate: pubDateStr ? parseDate(pubDateStr) : void 0, link: linkUrl ? new URL(linkUrl, baseUrl).href : void 0, category: categories, content: { html: description, text: description }, image, banner: image, 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 = $$("h1.article__title").text(); const description = $$("div#setText").html(); const processedItem = { title, description, content: { html: description, text: description }, 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"), language, id: $("meta[property=\"og:url\"]").attr("content") }; }; const route = { path: "/blog", name: "Blog", url: "mathpix.com", maintainers: ["nczitzk"], handler, example: "/mathpix/blog", parameters: void 0, description: void 0, categories: ["blog"], features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportRadar: true, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["mathpix.com/blog"], target: "/blog" }], view: ViewType.Articles }; //#endregion export { handler, route };