|
1 | | -import * as cheerio from 'cheerio'; |
| 1 | +import markdownit from 'markdown-it'; |
2 | 2 |
|
| 3 | +import type { NamespacesType } from '@/registry'; |
3 | 4 | import type { Route } from '@/types'; |
4 | 5 | import { ViewType } from '@/types'; |
5 | 6 | import ofetch from '@/utils/ofetch'; |
6 | 7 |
|
| 8 | +const md = markdownit({ |
| 9 | + breaks: true, |
| 10 | + html: true, |
| 11 | +}); |
| 12 | + |
7 | 13 | export const route: Route = { |
8 | 14 | path: '/routes/:lang?', |
9 | 15 | categories: ['program-update'], |
@@ -40,68 +46,24 @@ export const route: Route = { |
40 | 46 | async function handler(ctx) { |
41 | 47 | const isEnglish = ctx.req.param('lang') !== 'zh'; |
42 | 48 |
|
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'); |
72 | 50 |
|
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 | + })) |
78 | 60 | ); |
79 | | - const list = all.flatMap(({ page, item, type }) => item.map((item) => ({ page, item, type }))); |
80 | 61 |
|
81 | 62 | return { |
82 | 63 | title: isEnglish ? 'RSSHub has new routes' : 'RSSHub 有新路由啦', |
83 | 64 | link: 'https://docs.rsshub.app', |
84 | 65 | description: isEnglish ? 'Everything is RSSible' : '万物皆可 RSS', |
85 | 66 | 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, |
106 | 68 | }; |
107 | 69 | } |
0 commit comments