From e9f9f630bb7be941436aa2e44b06a4a689017521 Mon Sep 17 00:00:00 2001 From: MuenYu Date: Sun, 16 Feb 2025 01:00:47 +1000 Subject: [PATCH] feat(routes/mashiro): add route --- lib/routes/mashiro/index.ts | 61 +++++++++++++++++++++++++++++++++ lib/routes/mashiro/namespace.ts | 11 ++++++ 2 files changed, 72 insertions(+) create mode 100644 lib/routes/mashiro/index.ts create mode 100644 lib/routes/mashiro/namespace.ts diff --git a/lib/routes/mashiro/index.ts b/lib/routes/mashiro/index.ts new file mode 100644 index 00000000000000..31178371d2f183 --- /dev/null +++ b/lib/routes/mashiro/index.ts @@ -0,0 +1,61 @@ +import { Route } from '@/types'; +import { namespace } from './namespace'; +import ofetch from '@/utils/ofetch'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +const baseUrl = `https://${namespace.url}`; + +export const route: Route = { + path: '/:lang', + categories: ['blog'], + example: '/mashiro/en', + parameters: { lang: 'the language of the site. Can be either `en` or `zh-cn`. Default: `en`' }, + radar: [ + { + source: ['mashiro.best/', 'mashiro.best/:lang/'], + }, + ], + name: `Blog`, + maintainers: ['MuenYu'], + handler: async (ctx) => { + const { lang = 'en' } = ctx.req.param(); + const targetLink = lang === 'en' ? `${baseUrl}/archives/` : `${baseUrl}/${lang}/archives/`; + const response = await ofetch(targetLink); + const $ = load(response); + const links = $('.archives-group article') + .toArray() + .map((item) => { + item = $(item); + const a = item.find('a').first(); + + const title = a.find('.article-title').text(); + const link = `${baseUrl}${a.attr('href')}`; + const pubDate = parseDate(a.find('time').attr('datetime')); + + return { + title, + link, + pubDate, + }; + }); + + const items = await Promise.all( + links.map((item) => + cache.tryGet(item.link, async () => { + const response = await ofetch(item.link); + const $ = load(response); + item.description = $('.article-content').first().html(); + return item; + }) + ) + ); + + return { + title: namespace.name, + link: targetLink, + item: items, + }; + }, +}; diff --git a/lib/routes/mashiro/namespace.ts b/lib/routes/mashiro/namespace.ts new file mode 100644 index 00000000000000..4f0e39fcca94e2 --- /dev/null +++ b/lib/routes/mashiro/namespace.ts @@ -0,0 +1,11 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: `Mashiro's Baumkuchen`, + url: 'mashiro.best', + description: `Muen's blog posts`, + + zh: { + name: '真白的年轮面包', + }, +};