]> git.r.bdr.sh - rbdr/blog/blame - lib/generators/rss.js
Remove unnecessary static files
[rbdr/blog] / lib / generators / rss.js
CommitLineData
6cd62e79
RBR
1import Dot from 'dot';
2import { encodeXML } from 'entities';
3import { readFile, writeFile } from 'fs/promises';
4import { join } from 'path';
5import { debuglog } from 'util';
67fdfa7c
BB
6
7const internals = {
8 debuglog: debuglog('blog'),
9
9e355758
RBR
10 kFeedName: 'feed.xml',
11
12 extractTitle(postText) {
13
a9c02cac
RBR
14 return postText.trim()
15 .split('\n')[0]
16 .replace('#', '')
17 .replace(/&/g, '&')
18 .trim();
9e355758 19 }
67fdfa7c
BB
20};
21
22/**
23 * Generates an RSS feed XML file
24 *
25 * @name RSSGenerator
26 * @param {string} source the source directory
27 * @param {string} target the target directory
28 * @param {Array.<Blog.tPost>} posts the list of posts
29 */
6cd62e79 30export default async function RSSGenerator(source, target, posts) {
67fdfa7c
BB
31
32 internals.debuglog('Generating RSS');
33 const feedTarget = join(target, internals.kFeedName);
34 const feedLocation = join(source, internals.kFeedName);
35
36 internals.debuglog(`Reading ${feedLocation}`);
37 const feedTemplate = await readFile(feedLocation, { encoding: 'utf8' });
38
39 internals.debuglog('Writing RSS');
40 posts = posts.map((post) => ({
41 ...post,
42 createdOn: (new Date(post.createdOn)).toUTCString(),
9e355758 43 title: internals.extractTitle(post.raw),
67fdfa7c
BB
44 html: encodeXML(post.html)
45 }));
6cd62e79
RBR
46 const feedXml = Dot.template(feedTemplate, {
47 ...Dot.templateSettings,
9e355758
RBR
48 strip: false
49 })({ posts });
67fdfa7c 50 await writeFile(feedTarget, feedXml);
10a76a5b 51}