]>
git.r.bdr.sh - rbdr/blog/blob - lib/generators/rss.js
c47ba038ea1ccd3c81849af42cce8bca04fb8b68
2 import { encodeXML
} from 'entities';
3 import { readFile
, writeFile
} from 'fs/promises';
4 import { join
} from 'path';
5 import { debuglog
} from 'util';
8 debuglog: debuglog('blog'),
10 kFeedName: 'feed.xml',
12 extractTitle(postText
) {
14 return postText
.trim()
17 .replace(/&/g
, '&')
23 * Generates an RSS feed XML file
26 * @param {string} source the source directory
27 * @param {string} target the target directory
28 * @param {Array.<Blog.tPost>} posts the list of posts
30 export default async
function RSSGenerator(source
, target
, posts
) {
32 internals
.debuglog('Generating RSS');
33 const feedTarget
= join(target
, internals
.kFeedName
);
34 const feedLocation
= join(source
, internals
.kFeedName
);
36 internals
.debuglog(`Reading ${feedLocation}`);
37 const feedTemplate
= await
readFile(feedLocation
, { encoding: 'utf8' });
39 internals
.debuglog('Writing RSS');
40 posts
= posts
.map((post
) => ({
42 createdOn: (new Date(post
.createdOn
)).toUTCString(),
43 title: internals
.extractTitle(post
.raw
),
44 html: encodeXML(post
.html
)
46 const feedXml
= Dot
.template(feedTemplate
, {
47 ...Dot
.templateSettings
,
50 await
writeFile(feedTarget
, feedXml
);