-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrss.js
More file actions
131 lines (114 loc) · 3.73 KB
/
rss.js
File metadata and controls
131 lines (114 loc) · 3.73 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const Feed = require('feed').Feed;
const showdown = require('showdown');
const { writeFileSync, readFileSync } = require('fs');
const { join } = require('path');
const { log, logError, yellow } = require('@scullyio/scully');
const asciidoctor = require('asciidoctor.js')();
const configFile = readFileSync(`${process.cwd()}/rss.config.json`, 'utf8');
const config = JSON.parse(configFile.toString());
const blogPostRouteSlug = config.blogPostRouteSlug || '/blog';
const filename = config.filename || 'feed';
const markupLanguage = config.markupLanguage || 'markdown';
const feed = new Feed(config);
config.categories.forEach((cat) => {
feed.addCategory(cat);
});
const rssPlugin = (routes) => {
log('Started @notiz/scully-plugin-rss');
const blogPosts = routes.filter(
(r) =>
r && r.data && r.data.published && r.route.includes(blogPostRouteSlug)
);
if (config.newestPostsFirst) {
blogPosts.sort((a, b) => {
return a.data.publishedAt > b.data.publishedAt ? -1 : 1;
});
} else {
blogPosts.sort((a, b) => {
return a.data.publishedAt > b.data.publishedAt ? 1 : -1;
});
}
log(
`Generating RSS Feed for ${yellow(blogPosts.length)} published blog ${
blogPosts.length === 1 ? 'post' : 'posts'
}`
);
blogPosts.forEach((r) => {
const item = createFeedItemFromRoute(r);
feed.addItem(item);
});
try {
writeFileSync(join(config.outDir || '', `${filename}.xml`), feed.rss2());
log(`✅ Created ${yellow(config.outDir + `/${filename}.xml`)}`);
writeFileSync(join(config.outDir || '', `${filename}.atom`), feed.atom1());
log(`✅ Created ${yellow(config.outDir + `/${filename}.atom`)}`);
writeFileSync(join(config.outDir || '', `${filename}.json`), feed.json1());
log(`✅ Created ${yellow(config.outDir + `/${filename}.json`)}`);
} catch (error) {
logError('❌ Failed to create RSS feed. Error:', error);
throw error;
}
log('Finished @notiz/scully-plugin-rss');
};
const createFeedItemFromRoute = (route) => {
let item;
try {
if (route.data.published) {
const articleString = readFileSync(route.templateFile, 'utf8').toString();
const article = articleString.slice(
nth_occurrence(articleString, '---', 2) + 3,
articleString.length - 1
);
const articleHTML = '';
if (markupLanguage === 'asciidoc') {
articleHTML = asciidoctor.convert(article);
} else {
articleHTML = new showdown.Converter().makeHtml(article);
}
item = {
title: route.data.title,
id: route.route,
link: config.link + route.route,
description: route.data.description,
content: articleHTML,
author: route.data.authors
? route.data.authors.map((a) => ({ name: a }))
: [],
contributor: route.data.authors
? route.data.authors.map((a) => ({
name: a.toLowerCase().replace(' ', '-'),
}))
: [],
date: route.data.updatedAt
? route.data.updatedAt
: route.data.publishedAt,
image: route.data.twitterBanner,
};
}
} catch (err) {
logError(`Error during feed item creation ${route.data.route}`, err);
}
return item;
};
function nth_occurrence(text, searchString, nth) {
const firstIndex = text.indexOf(searchString);
const lengthUpToFirstIndex = firstIndex + 1;
if (nth === 1) {
return firstIndex;
} else {
const stringAfterFirstOccurrence = text.slice(lengthUpToFirstIndex);
const nextOccurrence = nth_occurrence(
stringAfterFirstOccurrence,
searchString,
nth - 1
);
if (nextOccurrence === -1) {
return -1;
} else {
return lengthUpToFirstIndex + nextOccurrence;
}
}
}
module.exports = {
rssPlugin,
};