feat(route): add /react/blog (#16055)
* feat(route): add `/react/blog` * fix: remove unnecessary cache according to #16038 * fix: cache individual post * fix: cache item * Update lib/routes/react/blog.ts ---------
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import type { DataItem, Route } from '@/types';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import cache from '@/utils/cache';
|
||||
import { load } from 'cheerio';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
|
||||
const handler: Route['handler'] = async () => {
|
||||
const data = await ofetch('https://react.dev/blog');
|
||||
|
||||
const $ = load(data);
|
||||
|
||||
const item = (await Promise.all(
|
||||
$('a[href^="/blog/"]')
|
||||
.toArray()
|
||||
.slice(0, 20)
|
||||
.map((item) => {
|
||||
const link = `https://react.dev${item.attribs.href}`;
|
||||
|
||||
return cache.tryGet(`react:blog:${link}`, async () => {
|
||||
const data = await ofetch(link);
|
||||
|
||||
const $ = load(data);
|
||||
|
||||
return {
|
||||
title: $('h1').first().text().trim(),
|
||||
link,
|
||||
description: $('article div:nth-child(2)').html() ?? '',
|
||||
pubDate: parseDate($('p.whitespace-pre-wrap').first().text().split(/\s+by/)[0]),
|
||||
};
|
||||
});
|
||||
})
|
||||
)) as DataItem[];
|
||||
|
||||
return {
|
||||
title: 'React Blog',
|
||||
link: 'https://react.dev/blog',
|
||||
language: 'en-US',
|
||||
item,
|
||||
};
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/blog',
|
||||
name: 'Blog',
|
||||
categories: ['blog'],
|
||||
maintainers: ['equt'],
|
||||
example: '/react/blog',
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
handler,
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'React',
|
||||
url: 'react.dev',
|
||||
};
|
||||
Reference in New Issue
Block a user