]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/rss.js
a1fcb0801ed7c3a2db8551f62f6d9644889a1df9
[rbdr/blog] / lib / generators / rss.js
1 'use strict';
2
3 const { template } = require('dot');
4 const { encodeXML } = require('entities');
5 const { readFile, writeFile } = require('fs/promises');
6 const { join } = require('path');
7 const { debuglog } = require('util');
8
9 const internals = {
10 debuglog: debuglog('blog'),
11
12 kFeedName: 'feed.xml'
13 };
14
15 /**
16 * Generates an RSS feed XML file
17 *
18 * @name RSSGenerator
19 * @param {string} source the source directory
20 * @param {string} target the target directory
21 * @param {Array.<Blog.tPost>} posts the list of posts
22 */
23 module.exports = async function RSSGenerator(source, target, posts) {
24
25 internals.debuglog('Generating RSS');
26 const feedTarget = join(target, internals.kFeedName);
27 const feedLocation = join(source, internals.kFeedName);
28
29 internals.debuglog(`Reading ${feedLocation}`);
30 const feedTemplate = await readFile(feedLocation, { encoding: 'utf8' });
31
32 internals.debuglog('Writing RSS');
33 posts = posts.map((post) => ({
34 ...post,
35 createdOn: (new Date(post.createdOn)).toUTCString(),
36 html: encodeXML(post.html)
37 }));
38 const feedXml = template(feedTemplate)({ posts });
39 await writeFile(feedTarget, feedXml);
40 };
41
42