rsshub
Version:
Make RSS Great Again!
82 lines (81 loc) • 3.4 kB
JavaScript
import { t as rofetch } from "./ofetch-C3ts-Hud.mjs";
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs";
import { t as cache_default } from "./cache-BkqOokyU.mjs";
import { load } from "cheerio";
//#region lib/routes/quantamagazine/archive.ts
const rootUrl = "https://www.quantamagazine.org";
const processArticleContent = (html, articleLink) => {
if (!html) return "";
let processed = html.replaceAll(/\$latex([\s\S]+?)\$/g, "<img align=\"center\" src=\"https://latex.codecogs.com/png.latex?$1\"/>");
processed = processed.replaceAll(/<div id=[\s\S]+?"src":"(https?:?[\s\S]+?)",[\s\S]+?"caption":"([\s\S]*?)",[\s\S]+?<\/div>?/g, (_match, src, cap) => {
return `<figure>${`<img src="${src.replaceAll(/\\([^nu])/g, "$1")}" />`}<figcaption>${cap.replaceAll(/\\([^nu])/g, "$1").replaceAll(String.raw`\n`, "").replaceAll(/\\u(\d{1,3}[a-z]\d?|\d{4})/g, (_omit, s) => String.fromCodePoint(Number.parseInt(s, 16)))}</figcaption></figure>`;
});
const lottieMatches = processed.matchAll(/<lottie-player[^>]*src="([^"]+)"[^>]*><\/lottie-player>/g).toArray();
const uniqueAnimations = /* @__PURE__ */ new Set();
for (const match of lottieMatches) {
const animName = match[1].split("/").pop()?.replace(/-(Desktop|Mobile).*\.json$/, "") || "animation";
if (uniqueAnimations.has(animName)) processed = processed.replace(match[0], "");
else {
uniqueAnimations.add(animName);
const replacement = `<p style="text-align: center; margin: 20px 0;"><a href="${articleLink || rootUrl}" target="_blank"><img src="https://img.shields.io/badge/🎬-View_Interactive_Animation-0066CC?style=for-the-badge" alt="View Interactive Animation" /></a></p>`;
processed = processed.replace(match[0], () => replacement);
}
}
return processed;
};
const handler = async (ctx) => {
const limit = ctx.req.query("limit") ? Number(ctx.req.query("limit")) : 20;
const posts = await rofetch(`${rootUrl}/wp-json/wp/v2/posts`, { query: {
per_page: limit,
page: 1,
_embed: "author"
} });
const items = await Promise.all(posts.map((item) => cache_default.tryGet(item.link, async () => {
const authorName = item._embedded?.author?.[0]?.name || "";
const $ = load(await rofetch(item.link, { parseResponse: (txt) => txt }));
$(".header-spacer, .scale1.mha, .post__title__author-date, .post__aside--divider").remove();
$(".hide-on-print, .post__aside__pullquote, aside.post__sidebar.hide, nav[data-glide-el]").remove();
$(".post__footer, .post__title__author-date").remove();
$(".iframe-placeholder").remove();
const contents = processArticleContent($("#postBody").html(), item.link);
return {
title: item.title.rendered,
author: authorName,
description: contents,
link: item.link,
guid: item.link,
pubDate: parseDate(item.date)
};
})));
return {
title: "Quanta Magazine",
link: rootUrl,
item: items
};
};
const route = {
path: "/archive",
name: "Archive",
url: "quantamagazine.org",
maintainers: ["emdoe"],
handler,
example: "/quantamagazine/archive",
parameters: {},
description: "Get the latest articles from Quanta Magazine.",
categories: ["new-media"],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{
source: ["quantamagazine.org"],
target: "/archive"
}]
};
//#endregion
export { handler, route };