/
genFeed.ts
64 lines (57 loc) · 1.72 KB
/
genFeed.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
import path from 'node:path'
import { writeFileSync } from 'node:fs'
import { Feed } from 'feed'
import { type SiteConfig, createContentLoader } from 'vitepress'
import { site as baseUrl, description, name } from '../meta'
function reName(name: string) {
if (!name)
name = 'Choi Yang'
return name === 'Choi Yang' ? 'Chocolate1999' : name
}
function getGithubLink(name: string) {
return `https://github.com/${reName(name)}`
}
export async function genFeed(config: SiteConfig) {
const feed = new Feed({
title: name,
description,
id: baseUrl,
link: baseUrl,
language: 'zh-CN',
image: 'https://chodocs.cn/chodocs-logo.svg',
favicon: `${baseUrl}/favicon.ico`,
copyright:
'Copyright (c) 2022-present, Chocolate and ChoDocs contributors',
})
const posts = await createContentLoader('**/*.md', {
excerpt: true,
render: true,
}).load()
posts.sort(
(a, b) =>
+new Date(b.frontmatter?.date as string)
- +new Date(a.frontmatter?.date as string),
)
for (const { url, frontmatter, html } of posts) {
let postTitle = '无题'
postTitle = html?.match(/<h1 id=(.*)>(.*?)<a .*?>/)?.[2] || postTitle
feed.addItem({
title: frontmatter?.title || postTitle,
id: `${baseUrl}${url.slice(1)}`,
link: `${baseUrl}${url.slice(1)}`,
guid: `${baseUrl}${url.slice(1)}`,
description: html,
content: html,
author: [
{
name: frontmatter?.author || 'Choi Yang',
link: frontmatter?.author
? getGithubLink(frontmatter?.author)
: undefined,
},
],
date: frontmatter?.date || new Date('2021-07-01'),
})
}
writeFileSync(path.join(config.outDir, 'feed.xml'), feed.rss2())
}