feat(route): add Node.js News (#8902)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const language = ctx.params.language ?? 'en';
|
||||
|
||||
const rootUrl = 'https://nodejs.org';
|
||||
const currentUrl = `${rootUrl}/${language}/blog`;
|
||||
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: currentUrl,
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
$('.summary').remove();
|
||||
|
||||
let items = $('ul.blog-index li a')
|
||||
.toArray()
|
||||
.map((item) => {
|
||||
item = $(item);
|
||||
|
||||
return {
|
||||
title: item.text(),
|
||||
link: `${rootUrl}${item.attr('href')}`,
|
||||
};
|
||||
});
|
||||
|
||||
items = await Promise.all(
|
||||
items.map((item) =>
|
||||
ctx.cache.tryGet(item.link, async () => {
|
||||
const detailResponse = await got({
|
||||
method: 'get',
|
||||
url: item.link,
|
||||
});
|
||||
|
||||
const content = cheerio.load(detailResponse.data);
|
||||
|
||||
item.pubDate = parseDate(content('time').attr('datetime'));
|
||||
item.author = content('.blogpost-meta')
|
||||
.text()
|
||||
.match(/by (.*), /)[1];
|
||||
|
||||
content('.blogpost-header').remove();
|
||||
|
||||
item.description = content('article').html();
|
||||
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
ctx.state.data = {
|
||||
title: 'News - Node.js',
|
||||
link: currentUrl,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
'/blog/:language?': ['nczitzk'],
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
module.exports = {
|
||||
'nodejs.org': {
|
||||
_name: 'Node.js',
|
||||
'.': [
|
||||
{
|
||||
title: 'News',
|
||||
docs: 'https://docs.rsshub.app/programming.html#nodejs-news',
|
||||
source: ['/:language/blog', '/'],
|
||||
target: '/nodejs/blog/:language?',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = function (router) {
|
||||
router.get('/blog/:language?', require('./blog'));
|
||||
};
|
||||
Reference in New Issue
Block a user