Skip to content

Commit 671f05e

Browse files
authored
fix(route/rsshub): route updates (#20888)
* fix(route/rsshub): route updates * fix(handler): simplify data parsing and improve route link formatting
1 parent 1f3b11c commit 671f05e

File tree

1 file changed

+18
-56
lines changed

1 file changed

+18
-56
lines changed

lib/routes/rsshub/routes.ts

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import * as cheerio from 'cheerio';
1+
import markdownit from 'markdown-it';
22

3+
import type { NamespacesType } from '@/registry';
34
import type { Route } from '@/types';
45
import { ViewType } from '@/types';
56
import ofetch from '@/utils/ofetch';
67

8+
const md = markdownit({
9+
breaks: true,
10+
html: true,
11+
});
12+
713
export const route: Route = {
814
path: '/routes/:lang?',
915
categories: ['program-update'],
@@ -40,68 +46,24 @@ export const route: Route = {
4046
async function handler(ctx) {
4147
const isEnglish = ctx.req.param('lang') !== 'zh';
4248

43-
const lang = isEnglish ? '' : 'zh/';
44-
const types = [
45-
'social-media',
46-
'new-media',
47-
'traditional-media',
48-
'bbs',
49-
'blog',
50-
'programming',
51-
'design',
52-
'live',
53-
'multimedia',
54-
'picture',
55-
'anime',
56-
'program-update',
57-
'university',
58-
'forecast',
59-
'travel',
60-
'shopping',
61-
'game',
62-
'reading',
63-
'government',
64-
'study',
65-
'journal',
66-
'finance',
67-
'other',
68-
];
69-
const all = await Promise.all(
70-
types.map(async (type) => {
71-
const response = await ofetch(`https://docs.rsshub.app/${lang}routes/${type}`);
49+
const data = await ofetch<NamespacesType>('https://docs.rsshub.app/routes.json');
7250

73-
const $ = cheerio.load(response);
74-
const page = $('.page').toArray();
75-
const item = $('.routeBlock').toArray();
76-
return { page, item, type };
77-
})
51+
const items = Object.entries(data).flatMap(([namespace, namespaceData]) =>
52+
Object.entries(namespaceData.routes).map(([routePath, routeData]) => ({
53+
title: `${namespaceData.name} - ${routeData.name}`,
54+
description: routeData.description ? md.render(routeData.description) : '',
55+
link: `https://docs.rsshub.app/${isEnglish ? '' : 'zh/'}routes/${namespace}`,
56+
category: routeData.categories,
57+
guid: `/${namespace}${routePath === '/' ? '' : routePath}`,
58+
author: routeData.maintainers.join(', '),
59+
}))
7860
);
79-
const list = all.flatMap(({ page, item, type }) => item.map((item) => ({ page, item, type })));
8061

8162
return {
8263
title: isEnglish ? 'RSSHub has new routes' : 'RSSHub 有新路由啦',
8364
link: 'https://docs.rsshub.app',
8465
description: isEnglish ? 'Everything is RSSible' : '万物皆可 RSS',
8566
language: isEnglish ? 'en-us' : 'zh-cn',
86-
item: list.map(({ page, item, type }) => {
87-
const $ = cheerio.load(page);
88-
const $item = $(item);
89-
const h2Title = $item.prevAll('h2').eq(0);
90-
const h3Title = $item.prevAll('h3').eq(0);
91-
92-
$item.find('.VPBadge').each((_, ele) => {
93-
const $ele = $(ele);
94-
if ($ele.text().includes('Test')) {
95-
$ele.remove();
96-
}
97-
});
98-
99-
return {
100-
title: `${h2Title.text().trim()} - ${h3Title.text().trim()}`,
101-
description: $item.html()?.replaceAll(/<!--.*?-->/g, ''),
102-
link: `https://docs.rsshub.app/${lang}routes/${type}#${encodeURIComponent(h2Title.find('.header-anchor').attr('href') && h3Title.find('.header-anchor').attr('href')?.slice(1))}`,
103-
guid: $item.attr('id'),
104-
};
105-
}),
67+
item: items,
10668
};
10769
}

0 commit comments

Comments
 (0)