]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/rss.js
Add title to html/rss generators
[rbdr/blog] / lib / generators / rss.js
1 'use strict';
2
3 const { template, templateSettings } = 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 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 */
28 module.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 };