feat(route): add hedwig.pub (#19245)
* feat(route): add hedwig.pub * feat(route): remove deprecated entry * docs: add author info from #4122
This commit is contained in:
parent
2d7aeebbb5
commit
af2252a8c5
|
|
@ -1002,9 +1002,6 @@ router.get('/haohaozhu/discover/:keyword?', lazyloadRouteHandler('./routes/haoha
|
|||
router.get('/magireco/announcements', lazyloadRouteHandler('./routes/magireco/announcements'));
|
||||
router.get('/magireco/event_banner', lazyloadRouteHandler('./routes/magireco/event-banner'));
|
||||
|
||||
// 我有一片芝麻地
|
||||
router.get('/blogs/hedwig/:type', lazyloadRouteHandler('./routes/blogs/hedwig'));
|
||||
|
||||
// 拉勾
|
||||
router.get('/lagou/jobs/:position/:city', lazyloadRouteHandler('./routes/lagou/jobs'));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const md = require('markdown-it')({
|
||||
html: true,
|
||||
});
|
||||
const dayjs = require('dayjs');
|
||||
const { isValidHost } = require('@/utils/valid-host');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const type = ctx.params.type;
|
||||
if (!isValidHost(type)) {
|
||||
throw new Error('Invalid type');
|
||||
}
|
||||
|
||||
const url = `https://${type}.hedwig.pub`;
|
||||
const res = await got({
|
||||
method: 'get',
|
||||
url,
|
||||
});
|
||||
const $ = cheerio.load(res.data);
|
||||
const content = JSON.parse($('#__NEXT_DATA__')[0].children[0].data);
|
||||
const title = $('title').text();
|
||||
|
||||
const list = content.props.pageProps.issuesByNewsletter.map((item) => {
|
||||
let description = '';
|
||||
for (const block of item.blocks) {
|
||||
description += md.render(block.markdown.text);
|
||||
}
|
||||
return {
|
||||
title: item.subject,
|
||||
description,
|
||||
pubDate: dayjs(`${item.publishAt} +0800`, "YYYY-MM-DD'T'HH:mm:ss.SSS'Z'").toString(),
|
||||
link: `https://${type}.hedwig.pub/i/${item.urlFriendlyName}`,
|
||||
};
|
||||
});
|
||||
ctx.state.data = {
|
||||
title: `Ⓙ Hedwig - ${title}`,
|
||||
description: content.props.pageProps.newsletter.about,
|
||||
link: `https://${type}.hedwig.pub`,
|
||||
item: list,
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'Hedwig',
|
||||
url: 'hedwig.pub',
|
||||
lang: 'zh-CN',
|
||||
};
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
import { Route, ViewType } from '@/types';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import timezone from '@/utils/timezone';
|
||||
import { isValidHost } from '@/utils/valid-host';
|
||||
import InvalidParameterError from '@/errors/types/invalid-parameter';
|
||||
|
||||
import MarkdownIt from 'markdown-it';
|
||||
const md = MarkdownIt({
|
||||
html: true,
|
||||
});
|
||||
|
||||
export const route: Route = {
|
||||
path: '/posts/:site',
|
||||
categories: ['blog'],
|
||||
example: '/posts/walnut',
|
||||
parameters: { site: '站点名,原则上只要是 `{site}.hedwig.pub` 都可以匹配' },
|
||||
features: {
|
||||
supportRadar: false,
|
||||
},
|
||||
name: 'Posts',
|
||||
url: 'hedwig.pub',
|
||||
maintainers: ['zwithz', 'GetToSet'],
|
||||
view: ViewType.Articles,
|
||||
handler: async (ctx) => {
|
||||
const { site } = ctx.req.param();
|
||||
|
||||
if (!isValidHost(site)) {
|
||||
throw new InvalidParameterError('Invalid site');
|
||||
}
|
||||
|
||||
const baseUrl = `https://${site}.hedwig.pub`;
|
||||
|
||||
const response = await ofetch(baseUrl);
|
||||
const $ = load(response);
|
||||
|
||||
const text = $('script#__NEXT_DATA__').text();
|
||||
const json = JSON.parse(text);
|
||||
|
||||
const pageProps = json.props.pageProps;
|
||||
|
||||
const list = pageProps.issuesByNewsletter.map((item) => {
|
||||
const description = item.blocks.reduce((desc, block) => desc + md.render(block.markdown.text), '');
|
||||
return {
|
||||
title: item.subject,
|
||||
description,
|
||||
pubDate: timezone(parseDate(item.publishAt, 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'), +0),
|
||||
link: `${baseUrl}/i/${item.urlFriendlyName}`,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
title: pageProps.newsletter.name,
|
||||
description: pageProps.newsletter.about,
|
||||
link: baseUrl,
|
||||
item: list,
|
||||
};
|
||||
},
|
||||
};
|
||||
Loading…
Reference in New Issue