-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgenerateFeed.ts
85 lines (75 loc) · 2.27 KB
/
generateFeed.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
import Fs from 'fs/promises';
import Path from 'path';
import Url from 'url';
import Bluebird from 'bluebird';
import { Feed } from 'feed';
import AuthorsJson from './authors.json' assert { type: 'json' };
import { siteName, defaultDescription } from './constants';
import { postToProps } from './utils/postToProps';
import { readAllPosts } from './utils/posts';
async function generateFeed() {
const publicUrl = `https://${process.env.NEXT_PUBLIC_HOST ?? process.env.NEXT_PUBLIC_VERCEL_URL}`;
const { posts: allPosts } = await readAllPosts({ includePages: false, includeCommentsCount: false });
const feed = new Feed({
title: siteName,
description: defaultDescription,
id: publicUrl,
link: publicUrl,
language: 'pl',
image: `${publicUrl}/apple-touch-icon.png`,
favicon: `${publicUrl}/favicon.ico`,
copyright: 'Type of Web',
updated: new Date(),
generator: siteName,
feedLinks: {
rss: `${publicUrl}/feed`,
},
author: {
name: siteName,
link: publicUrl,
},
});
await Bluebird.map(
allPosts,
async (post) => {
return postToProps(post, AuthorsJson.authors, {
onlyExcerpt: true,
parseOembed: false,
includeCommentsCount: false,
includePlaiceholder: false,
});
},
{ concurrency: 10 },
).mapSeries((result) => {
feed.addItem({
title: result.frontmatter.title,
description: result.excerpt || undefined,
author: result.frontmatter.authors.map((a) => ({
name: a.displayName,
// link: a.website || a.facebook || a.linkedin || a.twitter || a.github || a.youtube || a.instagram || undefined,
})),
link: `${publicUrl}/${result.frontmatter.permalink}`,
date: new Date(result.frontmatter.date ?? 0),
id: result.frontmatter.permalink,
});
});
return feed;
}
async function run() {
console.log('generateFeed');
const feed = await generateFeed();
await Fs.writeFile(
Path.resolve(Path.dirname(Url.fileURLToPath(import.meta.url)), 'public', 'feed.xml'),
feed.rss2(),
'utf-8',
);
await Fs.writeFile(
Path.resolve(Path.dirname(Url.fileURLToPath(import.meta.url)), 'public', 'feed.json'),
feed.json1(),
'utf-8',
);
}
run().catch((err) => {
console.error(err);
process.exit(1);
});