UNPKG

rsshub

Version:
78 lines (76 loc) 2.49 kB
import "./dist-Bog6z_OM.mjs"; import "./config-MQ-7ebJ_.mjs"; import { t as ViewType } from "./types-gOySfSXJ.mjs"; import "./logger-C4avouvV.mjs"; import { t as ofetch_default } from "./ofetch-DKl_Ma9g.mjs"; import { t as parseDate } from "./parse-date-r5WDWHno.mjs"; import { load } from "cheerio"; //#region lib/routes/cognition/blog.ts const route = { path: "/blog", name: "Blog", url: "cognition.ai/blog", maintainers: ["Loongphy"], example: "/cognition/blog", categories: ["programming"], features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportRadar: true, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["cognition.ai/blog/1"], target: "/blog" }], view: ViewType.Articles, handler }; const splitAuthors = (text) => { if (!text) return; const names = text.split(",").map((name) => name.trim()).filter(Boolean); if (names.length === 0) return; return names.map((name) => ({ name })); }; async function handler() { const baseUrl = "https://cognition.ai"; const targetUrl = new URL("/blog/1", baseUrl).href; const $ = load(await ofetch_default(targetUrl)); const items = $("#blog-post-list__list li.blog-post-list__list-item").toArray().map((el) => { const linkElement = $(el).find("a.o-blog-preview").first(); const href = linkElement.attr("href"); const link = href ? new URL(href, baseUrl).href : void 0; if (!link) return; const title = linkElement.find("h3.o-blog-preview__title").text().trim(); if (!title) return; const summary = linkElement.find("p.o-blog-preview__intro").text().trim(); const dateNode = linkElement.find(".o-blog-preview__meta-date").clone(); dateNode.find(".o-blog-preview__meta").remove(); const dateText = dateNode.text().trim(); const authorText = linkElement.find(".o-blog-preview__meta-author").text().trim(); const dataItem = { title, link, pubDate: parseDate(dateText) }; if (summary) dataItem.description = summary; const authors = splitAuthors(authorText); if (authors) dataItem.author = authors; return dataItem; }).filter((item) => item !== void 0); const imageAttr = $("meta[property=\"og:image\"]").attr("content"); const image = imageAttr ? new URL(imageAttr, baseUrl).href : void 0; return { title: $("title").text(), description: $("meta[name=\"description\"]").attr("content"), link: targetUrl, allowEmpty: true, item: items, image }; } //#endregion export { handler, route };