UNPKG

rsshub

Version:
92 lines (91 loc) 3.09 kB
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs"; import { t as got_default } from "./got-BTowW50G.mjs"; import { t as timezone } from "./timezone-BBvDwS_3.mjs"; import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime"; import { load } from "cheerio"; import markdownit from "markdown-it"; import { renderToString } from "hono/jsx/dom/server"; import { raw } from "hono/html"; //#region lib/routes/farmatters/index.tsx const md = markdownit({ html: true }); const ids = { 1: 0, 2: 2, 3: 1 }; const route = { path: [ "/exclusive/:locale?", "/news/:locale?", "/:locale?", "/:type/:id/:locale?" ], categories: ["new-media"], example: "/farmatters/exclusive", parameters: { locale: "Locale, `zh-CN` or `en-US`, `zh-CN` by default" }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["farmatters.com/exclusive"], target: "/exclusive" }], name: "Exclusive", maintainers: ["nczitzk"], handler, url: "farmatters.com/news" }; async function handler(ctx) { const { type, id, locale = "zh-CN" } = ctx.req.param(); const limit = ctx.req.query("limit") ? Number(ctx.req.query("limit")) : 50; const searchParams = { locale, page: 0, pagesize: limit }; if (type === "wiki" && id) searchParams.subCatalogId = id; else if (type && id) searchParams[type] = id; const rootUrl = "https://farmatters.com"; const apiUrl = new URL("api/v1/doc/list", rootUrl).href; const currentUrl = new URL(`${locale === "zh-CN" ? "" : "en/"}${type ? type === "wiki" ? "wiki" : `tag/${id}` : "news"}`, rootUrl).href; const { data: response } = await got_default(apiUrl, { searchParams }); const items = response.data.list.slice(0, limit).map((item) => ({ title: item.title, link: new URL(`doc/${item.id}`, rootUrl).href, description: renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [item.headImageUrl ? /* @__PURE__ */ jsx("figure", { children: /* @__PURE__ */ jsx("img", { src: item.headImageUrl, alt: item.title }) }) : null, item.content || item.summary ? raw(md.render(item.content ?? item.summary)) : null] })), author: item.author, category: [ item.catalogName, item.subCatalogName ?? void 0, ...item.tags?.map((t) => t.tagName) ?? [] ].filter(Boolean), guid: `farmatters-${item.id}`, pubDate: timezone(parseDate(item.createdAt), 8) })); const { data: currentResponse } = await got_default(currentUrl); const $ = load(currentResponse); const subtitle = `${$("h4").first().text()}${type === "wiki" ? ` - ${$("div.css-6f6728 div.MuiBox-root").eq(ids[id]).text()}` : ""}`; const icon = new URL("favicon.ico", rootUrl).href; return { item: items, title: `${$("title").text().split(/-/, 1)[0].trim()} - ${subtitle}`, link: currentUrl, description: $("meta[name=\"description\"]").prop("content"), language: $("html").prop("lang"), image: new URL($("img").first().prop("src"), rootUrl).href, icon, logo: icon, subtitle, allowEmpty: true }; } //#endregion export { route };