-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprebuild.ts
73 lines (67 loc) · 2 KB
/
prebuild.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
import Frontmatter from 'front-matter';
import { readdirSync, readFileSync } from 'node:fs';
import { Glob } from 'bun';
import type { MarkdownMetaData } from '../src/lib/core/types';
const createRssFeed = async (
packageJson: any,
articles: { slug: string; attributes: MarkdownMetaData }[]
) => {
const feed = `
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${packageJson.title}</title>
<link>${packageJson.homepage}</link>
<atom:link href="${packageJson.homepage}/rss.xml" rel="self" type="application/rss+xml"/>
<description>${packageJson.description}</description>
${articles
.map(
(p) => `
<item>
<title>${p.attributes.title}</title>
<description>${p.attributes.description}</description>
<link>${packageJson.homepage}/articles/${p.slug}</link>
<guid>${packageJson.homepage}/articles/${p.slug}</guid>
<pubDate>${new Date(p.attributes.createdAt).toUTCString()}</pubDate>
</item>
`
)
.join('')}
</channel>
</rss>
`;
await Bun.write('./static/rss.xml', feed);
};
const file = Bun.file('package.json');
const packageJson = await file.json();
const glob = new Glob('src/lib/markdown/articles/**/*.md');
let articles: { slug: string; attributes: MarkdownMetaData }[] = [];
for await (const file of glob.scan('.')) {
const splitedPath = file.split('/');
const rawCtn = readFileSync(file).toString();
const fm = Frontmatter<MarkdownMetaData>(rawCtn);
const attributes = fm.attributes;
articles = [
...articles,
{
slug: splitedPath[splitedPath.length - 1].split('.')[0],
attributes: attributes
}
];
}
articles.sort((a, b) => {
// Desc
const d1 = a.attributes.updatedAt
? new Date(a.attributes.updatedAt)
: new Date(a.attributes.createdAt);
const d2 = b.attributes.updatedAt
? new Date(b.attributes.updatedAt)
: new Date(b.attributes.createdAt);
if (d1 > d2) {
return -1;
}
if (d1 < d2) {
return 1;
}
return 0;
});
await createRssFeed(packageJson, articles);