]>
Commit | Line | Data |
---|---|---|
67fdfa7c BB |
1 | 'use strict'; |
2 | ||
9e355758 | 3 | const { template, templateSettings } = require('dot'); |
67fdfa7c BB |
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 | ||
9e355758 RBR |
12 | kFeedName: 'feed.xml', |
13 | ||
14 | extractTitle(postText) { | |
15 | ||
16 | return postText.trim().split('\n')[0].replace('#', '').trim(); | |
17 | } | |
67fdfa7c BB |
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(), | |
9e355758 | 41 | title: internals.extractTitle(post.raw), |
67fdfa7c BB |
42 | html: encodeXML(post.html) |
43 | })); | |
9e355758 RBR |
44 | const feedXml = template(feedTemplate, { |
45 | ...templateSettings, | |
46 | strip: false | |
47 | })({ posts }); | |
67fdfa7c BB |
48 | await writeFile(feedTarget, feedXml); |
49 | }; |