-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathrssfeed.cjs
More file actions
120 lines (105 loc) · 4.87 KB
/
Copy pathrssfeed.cjs
File metadata and controls
120 lines (105 loc) · 4.87 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
"use strict";
const cheerio = require("cheerio");
module.exports.register = function ({ config }) {
this.on("beforePublish", ({ siteCatalog, contentCatalog, playbook }) => {
const startTime = Date.now(); // Start the timer
try {
// Find the changelog page
const page = contentCatalog.findBy({
basename: config.inputFile,
})[0];
if (!page) {
throw new Error(`${config.inputFile} page not found.`);
}
// Destructure page attributes
const {
productname: productName,
productmajorversion: productMajorVersion,
doctitle: pageTitle,
description: pageDescription,
docname: pageName,
"page-component-name": pageComponentName,
"page-component-version": pageComponentVersion,
} = page.asciidoc.attributes;
// Construct site links
const siteLink = playbook.site.url;
const siteLinkWithVersion = `${siteLink}/${pageComponentName}/${pageComponentVersion}`;
// Load page content with cheerio
const $ = cheerio.load(page.contents.toString());
// Extract releases
const releases = $(".sect1")
.map((_, element) => {
const $element = $(element);
const linkElement = $element.find("a.xref");
let [version, date] = linkElement.text().split(" - ");
// Normalize version if it's missing the minor or patch version
const versionParts = version.split(".");
if (versionParts.length === 1) {
version += ".0.0";
} else if (versionParts.length === 2) {
version += ".0";
}
// Remove <p> tags inside <li> tags to fix rendering issues
const contentElement = $element.find(".sectionbody");
contentElement.find("li > p").each((_, pElem) => {
$(pElem).replaceWith($(pElem).html());
});
const content = contentElement.html();
return {
title: linkElement.text(),
link: `${siteLinkWithVersion}/${linkElement
.attr("href")
.replace(/\.\.\//g, "")}`,
description: `Changelog for TinyMCE ${version}`,
guid: version,
pubDate: new Date(date).toUTCString(),
content,
};
})
.get();
// Generate RSS feed items
const rssItems = releases
.map(
({ title, link, description, guid, pubDate, content }) => `
<item>
<title>${title}</title>
<link>${link}</link>
<description>${description}</description>
<guid isPermaLink="false">${guid}</guid>
<pubDate>${pubDate}</pubDate>
<content:encoded><![CDATA[${content}]]></content:encoded>
</item>`
)
.join("\n");
// Assemble the complete RSS feed
const rss = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
>
<channel>
<title>${productName} ${productMajorVersion} ${pageTitle}</title>
<link>${siteLinkWithVersion}/${pageName}</link>
<description>${pageDescription}</description>
<language>en</language>
<copyright>Creative Commons Legal Code - Attribution-NonCommercial-ShareAlike 3.0 Unported</copyright>
<atom:link href="${siteLink}/rss.xml" rel="self" type="application/rss+xml" />
${rssItems}
</channel>
</rss>`;
// Add RSS feed to site catalog
siteCatalog.addFile({
contents: Buffer.from(rss),
out: { path: config.outputFile },
});
const endTime = Date.now(); // End the timer
const duration = endTime - startTime; // Calculate the duration
console.log(
`RSS feed generated at ${config.outputFile} in ${duration}ms`
);
} catch (error) {
// Catch any errors to allow the build to continue
console.error("Error generating RSS feed:", error);
}
});
};