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