]>
git.r.bdr.sh - rbdr/blog/blob - lib/generators/rss.js
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');
10 debuglog: debuglog('blog'),
12 kFeedName: 'feed.xml',
14 extractTitle(postText
) {
16 return postText
.trim().split('\n')[0].replace('#', '').trim();
21 * Generates an RSS feed XML file
24 * @param {string} source the source directory
25 * @param {string} target the target directory
26 * @param {Array.<Blog.tPost>} posts the list of posts
28 module
.exports
= async
function RSSGenerator(source
, target
, posts
) {
30 internals
.debuglog('Generating RSS');
31 const feedTarget
= join(target
, internals
.kFeedName
);
32 const feedLocation
= join(source
, internals
.kFeedName
);
34 internals
.debuglog(`Reading ${feedLocation}`);
35 const feedTemplate
= await
readFile(feedLocation
, { encoding: 'utf8' });
37 internals
.debuglog('Writing RSS');
38 posts
= posts
.map((post
) => ({
40 createdOn: (new Date(post
.createdOn
)).toUTCString(),
41 title: internals
.extractTitle(post
.raw
),
42 html: encodeXML(post
.html
)
44 const feedXml
= template(feedTemplate
, {
48 await
writeFile(feedTarget
, feedXml
);