/
blog.ts
93 lines (85 loc) · 2.38 KB
/
blog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import got from '@/utils/got';
import { art } from '@/utils/render';
import path from 'node:path';
import { parseDate } from '@/utils/parse-date';
const baseApiUrl = 'https://api.hashnode.com';
export const route: Route = {
path: '/blog/:username',
categories: ['blog'],
example: '/hashnode/blog/inklings',
parameters: { username: '博主名称,用户头像 URL 中找到' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['hashnode.dev/'],
},
],
name: '用户博客',
maintainers: ['hnrainll'],
handler,
url: 'hashnode.dev/',
description: `:::tip
username 为博主用户名,而非\`xxx.hashnode.dev\`中\`xxx\`所代表的 blog 地址。
:::`,
};
async function handler(ctx) {
const username = ctx.req.param('username');
if (!username) {
return;
}
const query = `
{
user(username: "${username}") {
publication {
posts{
slug
title
brief
coverImage
dateAdded
}
}
}
}
`;
const userUrl = `https://${username}.hashnode.dev`;
const response = await got({
method: 'POST',
url: baseApiUrl,
headers: {
Referer: userUrl,
'Content-type': 'application/json',
},
body: JSON.stringify({ query }),
});
const publication = response.data.data.user.publication;
if (!publication) {
return;
}
const list = publication.posts;
return {
title: `Hashnode by ${username}`,
link: userUrl,
item: list
.map((item) => ({
title: item.title,
description: art(path.join(__dirname, 'templates/description.art'), {
image: item.coverImage,
brief: item.brief,
}),
pubDate: parseDate(item.dateAdded),
link: `${userUrl}/${item.slug}`,
}))
.filter((item) => item !== ''),
};
}