]>
Commit | Line | Data |
---|---|---|
6cd62e79 RBR |
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'; | |
67fdfa7c BB |
6 | |
7 | const internals = { | |
8 | debuglog: debuglog('blog'), | |
9 | ||
9e355758 RBR |
10 | kFeedName: 'feed.xml', |
11 | ||
12 | extractTitle(postText) { | |
13 | ||
a9c02cac RBR |
14 | return postText.trim() |
15 | .split('\n')[0] | |
16 | .replace('#', '') | |
17 | .replace(/&/g, '&') | |
18 | .trim(); | |
9e355758 | 19 | } |
67fdfa7c BB |
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 | */ | |
6cd62e79 | 30 | export default async function RSSGenerator(source, target, posts) { |
67fdfa7c BB |
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(), | |
9e355758 | 43 | title: internals.extractTitle(post.raw), |
67fdfa7c BB |
44 | html: encodeXML(post.html) |
45 | })); | |
6cd62e79 RBR |
46 | const feedXml = Dot.template(feedTemplate, { |
47 | ...Dot.templateSettings, | |
9e355758 RBR |
48 | strip: false |
49 | })({ posts }); | |
67fdfa7c | 50 | await writeFile(feedTarget, feedXml); |
10a76a5b | 51 | } |