Skip to content

Commit

Permalink
feat(routes/mashiro): add route
Browse files Browse the repository at this point in the history
  • Loading branch information
MuenYu committed Feb 15, 2025
1 parent 8929c27 commit e9f9f63
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/routes/mashiro/index.ts
Original file line number Diff line number Diff line change
@@ -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,
};
},
};
11 changes: 11 additions & 0 deletions lib/routes/mashiro/namespace.ts
Original file line number Diff line number Diff line change
@@ -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: '真白的年轮面包',
},
};

0 comments on commit e9f9f63

Please sign in to comment.