mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-03 18:48:12 +08:00
feat(route): add hashnode 用户博客 (#9987)
* 添加hashnode用户posts获取 * fix(route)(hashnode): update route v1 to v2 * fix(route)(hashnode): 优化代码名称和完善doc * update parseDate function Co-authored-by: wenhao <wenhao@mafengwo.com>
This commit is contained in:
13
docs/blog.md
13
docs/blog.md
@@ -30,6 +30,19 @@ pageClass: routes
|
||||
|
||||
<Route author="cerebrater" example="/gwern/newest" path="/gwern/:category" :paramsDesc="['網誌主頁的分類訊息']"/>
|
||||
|
||||
## hashnode
|
||||
|
||||
### 用户博客
|
||||
|
||||
<Route author="hnrainll" example="/hashnode/blog/inklings" path="/hashnode/blog/:username" :paramsDesc="['博主名称,用户头像 URL 中找到']">
|
||||
|
||||
::: tip 提示
|
||||
|
||||
username 为博主用户名,而非`xxx.hashnode.dev`中`xxx`所代表的 blog 地址。
|
||||
|
||||
:::
|
||||
|
||||
</Route>
|
||||
## Hedwig.pub
|
||||
|
||||
### 博客
|
||||
|
||||
62
lib/v2/hashnode/blog.js
Normal file
62
lib/v2/hashnode/blog.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const got = require('@/utils/got');
|
||||
const { art } = require('@/utils/render');
|
||||
const path = require('path');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
|
||||
const baseApiUrl = 'https://api.hashnode.com';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const username = ctx.params.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;
|
||||
ctx.state.data = {
|
||||
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 !== ''),
|
||||
};
|
||||
};
|
||||
3
lib/v2/hashnode/maintainer.js
Normal file
3
lib/v2/hashnode/maintainer.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
'/blog/:username': ['hnrainll'],
|
||||
};
|
||||
13
lib/v2/hashnode/radar.js
Normal file
13
lib/v2/hashnode/radar.js
Normal file
@@ -0,0 +1,13 @@
|
||||
module.exports = {
|
||||
'hashnode.dev': {
|
||||
_name: 'hashnode',
|
||||
'.': [
|
||||
{
|
||||
title: 'Hashnode Blog',
|
||||
docs: 'https://docs.rsshub.app/blog.html#hashnode',
|
||||
source: '/',
|
||||
target: '/hashnode/blog/:username',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
3
lib/v2/hashnode/router.js
Normal file
3
lib/v2/hashnode/router.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = (router) => {
|
||||
router.get('/blog/:username', require('./blog'));
|
||||
};
|
||||
2
lib/v2/hashnode/templates/description.art
Normal file
2
lib/v2/hashnode/templates/description.art
Normal file
@@ -0,0 +1,2 @@
|
||||
<img src="{{ image }}" />
|
||||
{{@ brief }}
|
||||
Reference in New Issue
Block a user