rsshub
Version:
Make RSS Great Again!
89 lines (88 loc) • 3.05 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/caicai/blog.ts
const route = {
path: "/blog/:lang?",
categories: ["blog"],
example: "/caicai/blog",
parameters: { lang: {
description: "Language",
options: [{
value: "en",
label: "English"
}, {
value: "zh",
label: "中文"
}],
default: "en"
} },
radar: [{ source: ["www.caicai.me/blogs"] }, {
source: ["www.caicai.me/zh/blogs"],
target: "/blog/zh"
}],
name: "Blog",
maintainers: ["TonyRL"],
handler,
url: "www.caicai.me/blogs"
};
const baseUrl = "https://www.caicai.me";
const renderAnnotation = (rich) => rich.map((t) => {
let s = t.content;
const a = t.annotations;
if (a.code) s = `<code>${s}</code>`;
if (a.bold) s = `<strong>${s}</strong>`;
if (a.italic) s = `<em>${s}</em>`;
if (a.strikethrough) s = `<s>${s}</s>`;
if (a.underline) s = `<u>${s}</u>`;
if (t.href) s = `<a href="${t.href}">${s}</a>`;
return s;
}).join("");
const renderBlocks = (blocks) => blocks.map((b, i) => {
switch (b.type) {
case "paragraph": return b.text.length ? `<p>${renderAnnotation(b.text)}</p>` : "";
case "heading_2": return b.text.length ? `<h2>${renderAnnotation(b.text)}</h2>` : "";
case "heading_3": return b.text.length ? `<h3>${renderAnnotation(b.text)}</h3>` : "";
case "bulleted_list_item": {
if (!b.text.length) return "";
const open = blocks[i - 1]?.type === "bulleted_list_item" ? "" : "<ul>";
const close = blocks[i + 1]?.type === "bulleted_list_item" ? "" : "</ul>";
return `${open}<li>${renderAnnotation(b.text)}</li>${close}`;
}
case "image": return `<img src="${new URL(b.url, baseUrl).href}">`;
case "divider": return "<hr>";
default: return "";
}
}).join("");
async function handler(ctx) {
const { lang = "en" } = ctx.req.param();
const prefix = lang === "en" ? "" : `/${lang}`;
const link = `${baseUrl}${prefix}/blogs`;
const $ = load(await rofetch(link));
const list = JSON.parse($("script#__NEXT_DATA__").text()).props.pageProps.posts.map((post) => ({
title: post.frontMatter.title,
link: `${baseUrl}${prefix}/blogs/${post.slug}`,
description: post.frontMatter.excerpt,
pubDate: parseDate(post.frontMatter.dateIso),
category: [post.frontMatter.group],
image: new URL(post.frontMatter.cover_image, baseUrl).href
}));
const items = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => {
const $ = load(await rofetch(item.link));
const { pageProps } = JSON.parse($("script#__NEXT_DATA__").text()).props;
const blocks = lang === "en" ? pageProps.blocks : pageProps.chineseBlocks;
item.description = renderBlocks(blocks);
return item;
})));
return {
title: $("head title").text(),
description: $("head meta[name=\"description\"]").attr("content"),
link,
language: lang === "en" ? "en" : "zh-CN",
image: `${baseUrl}/favicon.ico`,
item: items
};
}
//#endregion
export { route };