2019-01-04 13:14:53 -05: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";
|
|
|
|
|
2020-08-29 12:09:42 -04:00
|
|
|
async function generateFeed(posts: Page[], permalink: string, tag?: string) {
|
2019-01-04 13:14:53 -05:00
|
|
|
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-04 21:59:52 -05:00
|
|
|
text = util.render(text, {
|
2019-01-04 13:14:53 -05:00
|
|
|
posts,
|
2020-08-29 12:09:42 -04:00
|
|
|
tag,
|
2019-01-04 13:14:53 -05:00
|
|
|
permalink,
|
|
|
|
feedPath: dest
|
2019-01-04 21:59:52 -05:00
|
|
|
}, "site/feed.xml.ejs");
|
2019-01-04 13:14:53 -05:00
|
|
|
|
|
|
|
util.write(dest, text);
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:10:39 -04:00
|
|
|
export async function posts(posts: Page[]) {
|
2019-01-04 13:14:53 -05:00
|
|
|
generateFeed(posts, "/");
|
2020-07-09 14:10:39 -04:00
|
|
|
}
|
|
|
|
|
2020-08-29 12:09:42 -04:00
|
|
|
export async function tags(tags: Map<string, Page[]>) {
|
|
|
|
tags.forEach((posts, tag) => {
|
|
|
|
generateFeed(posts, `/${tag}/`, tag);
|
2019-01-04 13:14:53 -05:00
|
|
|
});
|
2020-07-09 14:10:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function tutorials(tutorials: TutorialSeries[]) {
|
2019-01-04 13:14:53 -05:00
|
|
|
tutorials.forEach(series => {
|
|
|
|
generateFeed(series.posts, `/tutorials/${series.series}/`, series.seriesName);
|
|
|
|
});
|
2020-07-09 14:10:39 -04:00
|
|
|
}
|