shadowfacts.net/lib/generate/rss.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-01-04 18:14:53 +00:00
import { promises as fs } from "fs";
import path from "path";
import { Page, PostMetadata } from "../metadata";
import * as util from "../util";
import { TutorialSeries } from "./tutorials";
async function generateFeed(posts: Page[], permalink: string, category?: string) {
posts = posts.sort((a, b) => {
const aDate = <Date>(<PostMetadata>a.metadata).date;
const bDate = <Date>(<PostMetadata>b.metadata).date;
return bDate.getTime() - aDate.getTime();
});
const dest = path.join(permalink, "feed.xml");
let text = (await fs.readFile("site/feed.xml.ejs")).toString();
2019-01-05 02:59:52 +00:00
text = util.render(text, {
2019-01-04 18:14:53 +00:00
posts,
category,
permalink,
feedPath: dest
2019-01-05 02:59:52 +00:00
}, "site/feed.xml.ejs");
2019-01-04 18:14:53 +00:00
util.write(dest, text);
}
2020-07-09 18:10:39 +00:00
export async function posts(posts: Page[]) {
2019-01-04 18:14:53 +00:00
generateFeed(posts, "/");
2020-07-09 18:10:39 +00:00
}
export async function categories(categories: Map<string, Page[]>) {
2019-01-04 18:14:53 +00:00
categories.forEach((posts, category) => {
generateFeed(posts, `/${category}/`, category);
});
2020-07-09 18:10:39 +00:00
}
export async function tutorials(tutorials: TutorialSeries[]) {
2019-01-04 18:14:53 +00:00
tutorials.forEach(series => {
generateFeed(series.posts, `/tutorials/${series.series}/`, series.seriesName);
});
2020-07-09 18:10:39 +00:00
}