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