fix(route): Kun Cheng Essay (#13183)

* fix(route): Kun Cheng Essay

* fix typo
This commit is contained in:
Ethan Shen
2023-09-02 01:08:09 +08:00
committed by GitHub
parent 26a5d268e7
commit ab6423f505
3 changed files with 37 additions and 21 deletions
+35 -19
View File
@@ -4,47 +4,63 @@ const { parseDate } = require('@/utils/parse-date');
const { SourceMapConsumer } = require('source-map');
module.exports = async (ctx) => {
const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 100;
const rootUrl = 'https://www.kunchengblog.com';
const currentUrl = new URL('essay', rootUrl).href;
let response = await got({
method: 'get',
url: rootUrl,
});
const { data: currentResponse } = await got(currentUrl);
const currentUrl = `${rootUrl}/essay`;
const mapUrl = `${rootUrl}${response.data.match(/"(\/static\/js\/main.*?\.js)">/)[1]}.map`;
const $ = cheerio.load(currentResponse);
response = await got({
method: 'get',
url: mapUrl,
});
const title = $('title').text();
const items = await SourceMapConsumer.with(response.data, null, (consumer) =>
const mapUrl = new URL(`${$('script').first().prop('src')}.map`, rootUrl).href;
const { data: response } = await got(mapUrl);
const items = await SourceMapConsumer.with(response, null, (consumer) =>
consumer.sources
.filter((s) => /routes\/essays/.test(s))
.reverse()
.slice(0, limit)
.map((item) => {
const source = consumer.sourceContentFor(item).replace(/\n/g, '');
const source = consumer.sourceContentFor(item).replace(/\s\n/g, '');
const content = cheerio.load(source);
const processedSource = source.replace(/(\w+)=\{+([^{}]+)\}+/g, (match, key, value) => {
const processedValue = value.slice(1, -1).replace(/"/g, "'").trim();
return `${key}="${processedValue}"`;
});
const content = cheerio.load(processedSource);
return {
title: content('title').text(),
pubDate: parseDate(content('b').last().text(), 'MMM YYYY'),
link: `${rootUrl}/essay/${item.match(/\/essays\/(.*?)\.js/)[1].toLowerCase()}`,
description: content('p')
pubDate: parseDate(content('p[className="App-essay-article"]').last().find('b').first().text(), 'MMM YYYY'),
link: new URL(`essay/${item.match(/\/(\w+)\.js/)[1].toLowerCase()}`, currentUrl).href,
description: content('p[className="App-essay-article"]')
.toArray()
.map((p) => content(p).html())
.join(),
author: title,
};
})
);
const description = $('meta[name="description"]').prop('content');
const icon = new URL($('link[rel="icon"]').prop('href'), rootUrl).href;
ctx.state.data = {
title: 'KUN CHENG - Essay',
link: currentUrl,
item: items,
title: `${title} - Essay`,
link: currentUrl,
description,
language: $('html').prop('lang'),
image: icon,
icon,
logo: icon,
subtitle: description,
author: title,
allowEmpty: true,
};
};
+1 -1
View File
@@ -5,7 +5,7 @@ module.exports = {
{
title: 'Essay',
docs: 'https://docs.rsshub.app/routes/blog#kun-cheng-essay',
source: ['/essay', '/'],
source: ['/essay'],
target: '/kunchengblog/essay',
},
],
+1 -1
View File
@@ -1,3 +1,3 @@
module.exports = function (router) {
module.exports = (router) => {
router.get('/essay', require('./essay'));
};