-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateRSS.ts
More file actions
58 lines (47 loc) · 1.38 KB
/
Copy pathgenerateRSS.ts
File metadata and controls
58 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { resolve } from 'path';
import { writeFile } from 'fs';
import RSS from 'rss';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import { getPosts } from 'Helpers/content/posts';
import siteUrl from 'SiteUrl';
import blogAuthor from 'BlogAuthor';
import siteName from 'SiteName';
import addressSeparator from 'AddressSeparator';
import slogan from 'Slogan';
dayjs.extend(customParseFormat);
const generateRSS = async (): Promise<void> => {
const targetDir = resolve(__dirname, '../public');
const posts = await getPosts();
const feed = new RSS({
title: `${siteName} ${addressSeparator} ${slogan}`,
site_url: siteUrl,
feed_url: `${siteUrl}/rss.xml`,
language: 'pl',
copyright: `${new Date().getFullYear()} ${blogAuthor}`,
webMaster: blogAuthor,
managingEditor: blogAuthor,
});
posts.map(({ slug, title, abstract, date, topics }) => {
feed.item({
title: title,
description: abstract,
url: `${siteUrl}/${slug}`,
guid: slug,
categories: [...topics],
author: blogAuthor,
date: new Date(dayjs(date, 'DD.MM.YYYY').format()),
});
});
const rssCode = feed.xml({
indent: true,
});
writeFile(`${targetDir}/rss.xml`, rssCode, (err) => {
if (err) {
console.log(err);
} else {
console.log('RSS generated.');
}
});
};
export default generateRSS;