-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
312 lines (274 loc) · 10.5 KB
/
main.js
File metadata and controls
312 lines (274 loc) · 10.5 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { i18n, plugin, format } from "@shevky/base";
const PLUGIN_NAME = "shevky-rss";
const PLUGIN_VERSION = "0.0.4";
const FEED_FILENAME = "feed.xml";
const FEED_TTL = 1440;
const GENERATOR_NAME = "Shevky Static Site Generator";
const DEFAULT_INCLUDED_SCHEMA_TYPES = ["post", "job-post"];
const escape = (value) => format.escape(value);
const rssDate = (value) => format.rssDate(value);
const atomDate = (value) => {
const date = value instanceof Date ? value : new Date(value);
return Number.isNaN(date.getTime()) ? "" : date.toISOString();
};
const joinLines = (lines) => lines.filter(Boolean).join("\n");
const parseDate = (date) => (date ? Date.parse(String(date)) || 0 : 0);
const uniqStrings = (items) => [
...new Set(
items
.map((value) => (typeof value === "string" ? value.trim() : ""))
.filter(Boolean),
),
];
const toLookupSet = (items) =>
new Set(uniqStrings(items).map((value) => value.toLowerCase()));
const normalizeFilterValue = (value) =>
typeof value === "string" ? value.trim().toLowerCase() : "";
const getLangConfig = (lang) => i18n.build[lang] ?? i18n.build[i18n.default];
const normalizeBaseUrl = (baseUrl) =>
baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
const withCdata = (value) => (value ? `<![CDATA[ ${value} ]]>` : "");
const buildCategoryLines = (categories, domain) =>
uniqStrings(categories)
.map(
(category) =>
` <category domain="${escape(domain)}">${escape(category)}</category>`,
)
.join("\n");
const buildAlternateFeedLinks = (feedUrls, lang) =>
Object.keys(feedUrls)
.filter(
(alternateLang) => alternateLang !== lang && feedUrls[alternateLang],
)
.map(
(alternateLang) =>
` <atom:link href="${escape(feedUrls[alternateLang])}" rel="alternate" hreflang="${escape(alternateLang)}" type="application/rss+xml" />`,
)
.join("\n");
class RssFeedBuilder {
async build(ctx) {
const pluginConfig = ctx.config.get(PLUGIN_NAME) ?? {};
const feedFilename =
typeof pluginConfig.feedFilename === "string" &&
pluginConfig.feedFilename.trim().length > 0
? pluginConfig.feedFilename.trim()
: FEED_FILENAME;
const feedTtl = Number.isFinite(pluginConfig.feedTtl)
? pluginConfig.feedTtl
: FEED_TTL;
const feedItemCount = Number.isFinite(pluginConfig.feedItemCount)
? pluginConfig.feedItemCount
: undefined;
const filters = this._buildFilters(pluginConfig);
const entries = this._collectEntries(ctx.contentFiles ?? [], ctx, filters);
if (!entries.length) {
return;
}
const entriesByLang = this._groupEntriesByLang(entries);
const languages = this._resolveLanguages();
const feedUrls = this._buildFeedUrls(languages, ctx, feedFilename);
for (const lang of languages) {
const langEntries = entriesByLang[lang] ?? [];
if (!langEntries.length) {
continue;
}
const limitedEntries =
Number.isFinite(feedItemCount) && feedItemCount > 0
? langEntries.slice(0, feedItemCount)
: langEntries;
const channel = this._buildChannelMeta(
lang,
feedUrls,
limitedEntries,
ctx,
feedFilename,
);
const itemsXml = this._renderItems(limitedEntries, ctx);
const rssXml = this._renderFeed(channel, itemsXml, ctx, feedTtl);
await this._writeFeed(
ctx,
lang,
rssXml,
limitedEntries.length,
feedFilename,
);
}
}
_buildFilters(pluginConfig) {
const schemaTypes = Array.isArray(pluginConfig.includedSchemaTypes)
? pluginConfig.includedSchemaTypes
: DEFAULT_INCLUDED_SCHEMA_TYPES;
const includedCategorySet = Array.isArray(pluginConfig.includedCategories)
? toLookupSet(pluginConfig.includedCategories)
: null;
return {
includedSchemaTypeSet: toLookupSet(schemaTypes),
includedCategorySet,
};
}
_isSelectedForFeed(file, filters) {
if (!file?.isValid || file.isDraft || !file.isPublished) {
return false;
}
const schemaType = normalizeFilterValue(file.schemaType);
if (!schemaType || !filters.includedSchemaTypeSet.has(schemaType)) {
return false;
}
if (!filters.includedCategorySet) {
return true;
}
const category = normalizeFilterValue(file.category);
return Boolean(category) && filters.includedCategorySet.has(category);
}
_collectEntries(contentFiles, ctx, filters) {
return contentFiles
.filter((file) => this._isSelectedForFeed(file, filters))
.map((file) => ({
title: file.title,
lang: file.lang,
description: file.description,
link: format.resolveUrl(file.canonical, ctx.config.identity.url),
guid: format.resolveUrl(file.canonical, ctx.config.identity.url),
date: file.date,
updated: file.updated ?? file.date,
category: file.category,
categories: Array.isArray(file.tags) ? file.tags : [],
}))
.sort((a, b) => parseDate(b.date) - parseDate(a.date));
}
_groupEntriesByLang(entries) {
return entries.reduce((acc, entry) => {
const lang = entry.lang || i18n.default;
(acc[lang] ||= []).push(entry);
return acc;
}, /** @type {Record<string, typeof entries>} */ ({}));
}
_resolveLanguages() {
return i18n.supported.length ? i18n.supported : [i18n.default];
}
_buildFeedUrls(languages, ctx, feedFilename) {
return languages.reduce((acc, lang) => {
const langConfig = getLangConfig(lang);
const baseUrl = (langConfig?.canonical ?? ctx.config.identity.url) || "";
acc[lang] = `${normalizeBaseUrl(baseUrl)}${feedFilename}`;
return acc;
}, /** @type {Record<string, string>} */ ({}));
}
_buildChannelMeta(lang, feedUrls, entries, ctx, feedFilename) {
const siteTitle = i18n.t(lang, "site.title", ctx.config.identity.author);
const siteDescription = i18n.t(lang, "site.description", "");
const langConfig = getLangConfig(lang);
const channelLink = langConfig?.canonical ?? ctx.config.identity.url;
const selfFeedLink = feedUrls[lang] ?? `${channelLink}${feedFilename}`;
const languageCulture = i18n.culture(lang) ?? lang;
const languageCode = languageCulture.replace("_", "-");
const lastBuildDate = rssDate(new Date());
const alternateFeedLinks = buildAlternateFeedLinks(feedUrls, lang);
const oldestEntry = entries.length ? entries[entries.length - 1] : null;
const pubDate = oldestEntry?.date ? rssDate(oldestEntry.date) : "";
const currentYear = new Date().getFullYear();
const oldestDate = oldestEntry?.date ? new Date(oldestEntry.date) : null;
const startYear =
oldestDate && !Number.isNaN(oldestDate.getTime())
? oldestDate.getFullYear()
: currentYear;
const authorName = ctx.config.identity.author || "";
const rightsText =
String(lang).toLowerCase().startsWith("en") ||
String(languageCulture).toLowerCase().startsWith("en")
? "All rights reserved."
: "Tüm hakları saklıdır.";
const copyright = authorName
? `© ${startYear} - ${currentYear} ${authorName}. ${rightsText}`
: `© ${startYear} - ${currentYear}. ${rightsText}`;
return {
siteTitle,
siteDescription,
channelLink,
selfFeedLink,
languageCode,
lastBuildDate,
alternateFeedLinks,
pubDate,
copyright,
};
}
_renderItems(entries, ctx) {
const authorField =
ctx.config.identity.email && ctx.config.identity.author
? `${ctx.config.identity.email} (${ctx.config.identity.author})`
: ctx.config.identity.email || ctx.config.identity.author || "";
return entries
.map((entry) => this._renderItem(entry, authorField))
.join("\n");
}
_renderItem(entry, authorField) {
const description = entry.description?.trim() || "";
const descriptionCdata = withCdata(description);
const categoryLines = buildCategoryLines([entry.category], "category");
const tagLines = buildCategoryLines(
Array.isArray(entry.categories) ? entry.categories : [],
"tag",
);
return joinLines([
" <item>",
` <title>${escape(entry.title)}</title>`,
` <link>${escape(entry.link)}</link>`,
` <guid isPermaLink=\"true\">${escape(entry.guid)}</guid>`,
entry.date ? ` <pubDate>${rssDate(entry.date)}</pubDate>` : "",
entry.updated
? ` <atom:updated>${atomDate(entry.updated)}</atom:updated>`
: "",
descriptionCdata
? ` <description>${descriptionCdata}</description>`
: "",
authorField ? ` <author>${escape(authorField)}</author>` : "",
categoryLines,
tagLines,
" </item>",
]);
}
_renderFeed(channel, itemsXml, ctx, feedTtl) {
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<?xml-stylesheet type="text/xsl" href="/assets/rss.xsl"?>',
'<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">',
" <channel>",
` <title>${escape(channel.siteTitle)}</title>`,
` <link>${escape(channel.channelLink)}</link>`,
` <atom:link href="${escape(channel.selfFeedLink)}" rel="self" type="application/rss+xml" />`,
...(channel.alternateFeedLinks ? [channel.alternateFeedLinks] : []),
` <description>${escape(channel.siteDescription)}</description>`,
` <language>${escape(channel.languageCode)}</language>`,
` <lastBuildDate>${channel.lastBuildDate}</lastBuildDate>`,
channel.pubDate ? ` <pubDate>${channel.pubDate}</pubDate>` : "",
` <copyright>${escape(channel.copyright)}</copyright>`,
` <generator>${GENERATOR_NAME}</generator>`,
` <managingEditor>${ctx.config.identity.email} (${ctx.config.identity.author})</managingEditor>`,
` <ttl>${feedTtl}</ttl>`,
itemsXml,
" </channel>",
"</rss>",
"",
].join("\n");
}
async _writeFeed(ctx, lang, rssXml, itemCount, feedFilename) {
const relativePath =
lang === i18n.default
? feedFilename
: ctx.path.combine(lang, feedFilename);
const targetPath = ctx.path.combine(ctx.paths.dist, relativePath);
await ctx.directory.create(ctx.path.name(targetPath));
await ctx.file.write(targetPath, rssXml);
ctx.log.debug(`[${PLUGIN_NAME}] RSS '${lang}' feed has been created.`);
}
}
const rssBuilder = new RssFeedBuilder();
/** @type {import("@shevky/base").PluginHooks} */
const hooks = {
[plugin.hooks.CONTENT_READY]: async function (ctx) {
await rssBuilder.build(ctx);
},
};
const PLUGIN = { name: PLUGIN_NAME, version: PLUGIN_VERSION, hooks };
export default PLUGIN;