on 956acfc help sign in
urbit.org/lib/rss.js
97 lines 2.4 kB history raw
1const fs = require("fs");
2const path = require("path");
3const matter = require("gray-matter");
4const toml = require("@iarna/toml");
5const Feed = require("feed").Feed;
7const options = {
8 engines: {
9 toml: toml.parse.bind(toml),
10 },
11 language: "toml",
12 delimiters: "+++",
13};
15const generateRSSFeed = () => {
16 let posts = [];
18 const getAllChildren = (dir) => {
19 const children = fs.readdirSync(dir, { withFileTypes: true });
20 return children
21 .filter((f) => f.isFile() && f.name !== "_index.md")
22 .map((post) => {
23 const { data: pageData, content: pageContent } = matter(
24 fs.readFileSync(path.join(dir, post.name)),
25 options
26 );
27 return {
28 title: pageData.title,
29 descripton: pageData.description || "",
30 author: pageData.extra.author || "",
31 date: pageData.date,
32 link: path.join(
33 "https://urbit.org/",
34 dir.substr(dir.indexOf("content") + 7),
35 "/",
36 post.name.replace(/.md$/, "")
37 ),
38 content: pageContent,
39 };
40 });
41 };
42 posts.push(
43 ...getAllChildren(path.join(process.cwd(), "content/blog")),
44 ...getAllChildren(path.join(process.cwd(), "content/updates")),
45 ...getAllChildren(path.join(process.cwd(), "content/events"))
46 );
47 posts = posts.sort((a, b) => (a.date > b.date ? -1 : 1));
49 const baseUrl = "https://urbit.org";
50 const author = {
51 name: "Urbit.org",
52 email: "support@urbit.org",
53 link: "https://urbit.org",
54 };
56 // Construct a new Feed object
57 const feed = new Feed({
58 title: "Urbit",
59 description: "Urbit is a personal server built from scratch",
60 id: baseUrl,
61 link: baseUrl,
62 language: "en",
63 feedLinks: {
64 rss2: `${baseUrl}/rss.xml`,
65 },
66 author,
67 });
69 // Add each article to the feed
70 posts.forEach((post) => {
71 const { title, description, author, date, link, content } = post;
72 feed.addItem({
73 title,
74 id: link,
75 link: link,
76 description,
77 content,
78 author: [
79 {
80 name: author,
81 email: "",
82 link: "",
83 },
84 ],
85 date: new Date(date),
86 });
87 });
88 try {
89 fs.readdirSync("public");
90 } catch (err) {
91 fs.mkdirSync("public");
92 }
94 fs.writeFileSync("public/rss.xml", feed.rss2());
95};
97generateRSSFeed();