-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateFeed.js
57 lines (51 loc) · 1.9 KB
/
generateFeed.js
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
import path from "node:path";
import { mkdir, writeFile } from "node:fs/promises";
import { Feed } from "feed";
import { canonicalHostnameUrl } from "@constants/constants";
import { getAllPostsForHome } from "@lib/api";
export default async function generateFeed() {
const basedir = process.cwd();
const feedUrlPath = "blog/posts/feed.xml";
const atomUrlPath = "blog/posts/atom.xml";
const jsonUrlPath = "blog/posts/feed.json";
const feedPath = path.join(basedir, "public", feedUrlPath);
const atomPath = path.join(basedir, "public", atomUrlPath);
const jsonPath = path.join(basedir, "public", jsonUrlPath);
const allPosts = await getAllPostsForHome();
const feed = new Feed({
title: "Luis Alejandro's Blog",
description: "Some of this and that",
language: "es",
id: canonicalHostnameUrl,
link: canonicalHostnameUrl,
image: `${canonicalHostnameUrl}/logo.png`,
favicon: `${canonicalHostnameUrl}/favicon.png`,
copyright: `All rights reserved ${new Date().getFullYear()}, Luis Martínez`,
updated: new Date(),
generator: "Feed for Node.js",
feedLinks: {
rss2: `${canonicalHostnameUrl}/${feedUrlPath}`,
atom: `${canonicalHostnameUrl}/${atomUrlPath}`,
json: `${canonicalHostnameUrl}/${jsonUrlPath}`,
},
author: {
name: "Luis Martínez",
email: "[email protected]",
link: `${canonicalHostnameUrl}/portfolio`,
},
});
allPosts.forEach((post) => {
feed.addItem({
title: post.title,
id: `${canonicalHostnameUrl}/blog/posts/${post.slug}`,
link: `${canonicalHostnameUrl}/blog/posts/${post.slug}`,
description: post.teaser,
date: new Date(post.created_at),
image: post.metadata.hero.url
});
});
await mkdir(path.dirname(feedPath), { recursive: true });
await writeFile(feedPath, feed.rss2());
await writeFile(atomPath, feed.atom1());
await writeFile(jsonPath, feed.json1());
}