on master help sign in
urbit.org/lib/rss.js
95 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 const leadingDir = dir.substr(dir.indexOf("content") + 8);
28 const name = post.name.replace(/.md$/, "");
30 return {
31 title: pageData.title,
32 descripton: pageData.description || "",
33 author: pageData.extra ? pageData.extra.author || "" : "",
34 date: pageData.date || pageData.starts,
35 link: `https://urbit.org/${leadingDir}/${name}`,
36 content: pageContent,
37 };
38 });
39 };
40 posts.push(
41 ...getAllChildren(path.join(process.cwd(), "content/blog")),
42 ...getAllChildren(path.join(process.cwd(), "content/updates")),
43 ...getAllChildren(path.join(process.cwd(), "content/events"))
44 );
45 posts = posts.sort((a, b) => (a.date > b.date ? -1 : 1));
47 const baseUrl = "https://urbit.org";
48 const author = {
49 name: "Urbit.org",
50 email: "support@urbit.org",
51 link: "https://urbit.org",
52 };
54 // Construct a new Feed object
55 const feed = new Feed({
56 title: "Urbit",
57 description: "Urbit is a personal server built from scratch",
58 id: baseUrl,
59 link: baseUrl,
60 language: "en",
61 feedLinks: {
62 rss2: `${baseUrl}/rss.xml`,
63 },
64 author,
65 });
67 // Add each article to the feed
68 posts.forEach((post) => {
69 const { title, description, author, date, link, content } = post;
70 feed.addItem({
71 title,
72 id: link,
73 link: link,
74 description,
75 content,
76 author: [
77 {
78 name: author,
79 email: "",
80 link: "",
81 },
82 ],
83 date: new Date(date),
84 });
85 });
86 try {
87 fs.readdirSync("public");
88 } catch (err) {
89 fs.mkdirSync("public");
90 }
92 fs.writeFileSync("public/rss.xml", feed.rss2());
93};
95generateRSSFeed();