|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const cheerio = require("cheerio"); |
| 4 | + |
| 5 | +module.exports.register = function () { |
| 6 | + this.on("beforePublish", ({ siteCatalog, contentCatalog, playbook }) => { |
| 7 | + try { |
| 8 | + // Find the changelog page |
| 9 | + const page = contentCatalog.findBy({ basename: "changelog.adoc" })[0]; |
| 10 | + |
| 11 | + if (!page) { |
| 12 | + console.error("changelog.adoc page not found."); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + // Destructure page attributes |
| 17 | + const { |
| 18 | + productname: productName, |
| 19 | + productmajorversion: productMajorVersion, |
| 20 | + doctitle: pageTitle, |
| 21 | + description: pageDescription, |
| 22 | + docname: pageName, |
| 23 | + "page-component-name": pageComponentName, |
| 24 | + "page-component-version": pageComponentVersion, |
| 25 | + } = page.asciidoc.attributes; |
| 26 | + |
| 27 | + // Construct site links |
| 28 | + const siteLink = playbook.site.url; |
| 29 | + const siteLinkWithVersion = `${siteLink}/${pageComponentName}/${pageComponentVersion}`; |
| 30 | + |
| 31 | + // Load page content with cheerio |
| 32 | + const $ = cheerio.load(page.contents.toString()); |
| 33 | + |
| 34 | + // Extract releases |
| 35 | + const releases = $(".sect1") |
| 36 | + .map((_, element) => { |
| 37 | + const $element = $(element); |
| 38 | + const linkElement = $element.find("a.xref"); |
| 39 | + const [version, date] = linkElement.text().split(" - "); |
| 40 | + |
| 41 | + // Remove <p> tags inside <li> tags to fix rendering issues |
| 42 | + const contentElement = $element.find(".sectionbody"); |
| 43 | + contentElement.find("li > p").each((_, pElem) => { |
| 44 | + $(pElem).replaceWith($(pElem).html()); |
| 45 | + }); |
| 46 | + const content = contentElement.html(); |
| 47 | + |
| 48 | + return { |
| 49 | + title: linkElement.text(), |
| 50 | + link: `${siteLinkWithVersion}/${linkElement |
| 51 | + .attr("href") |
| 52 | + .replace("../", "")}`, |
| 53 | + description: `Release notes for TinyMCE ${version}`, |
| 54 | + guid: version, |
| 55 | + pubDate: new Date(date).toUTCString(), |
| 56 | + content, |
| 57 | + }; |
| 58 | + }) |
| 59 | + .get(); |
| 60 | + |
| 61 | + // Generate RSS feed items |
| 62 | + const rssItems = releases |
| 63 | + .map( |
| 64 | + ({ title, link, description, guid, pubDate, content }) => ` |
| 65 | + <item> |
| 66 | + <title>${title}</title> |
| 67 | + <link>${link}</link> |
| 68 | + <description>${description}</description> |
| 69 | + <guid isPermaLink="false">${guid}</guid> |
| 70 | + <pubDate>${pubDate}</pubDate> |
| 71 | + <content:encoded><![CDATA[${content}]]></content:encoded> |
| 72 | + </item>` |
| 73 | + ) |
| 74 | + .join("\n"); |
| 75 | + |
| 76 | + // Assemble the complete RSS feed |
| 77 | + const rss = `<?xml version="1.0" encoding="UTF-8" ?> |
| 78 | + <rss version="2.0" |
| 79 | + xmlns:atom="http://www.w3.org/2005/Atom" |
| 80 | + xmlns:content="http://purl.org/rss/1.0/modules/content/" |
| 81 | + > |
| 82 | + <channel> |
| 83 | + <title>${productName} ${productMajorVersion} ${pageTitle}</title> |
| 84 | + <link>${siteLinkWithVersion}/${pageName}</link> |
| 85 | + <description>${pageDescription}</description> |
| 86 | + <language>en</language> |
| 87 | + <atom:link href="${siteLink}/rss.xml" rel="self" type="application/rss+xml" /> |
| 88 | + ${rssItems} |
| 89 | + </channel> |
| 90 | + </rss>`; |
| 91 | + |
| 92 | + // Add RSS feed to site catalog |
| 93 | + siteCatalog.addFile({ |
| 94 | + contents: Buffer.from(rss), |
| 95 | + out: { path: "rss.xml" }, |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + // Catch any errors to allow the build to continue |
| 99 | + console.error("Error generating RSS feed:", error); |
| 100 | + } |
| 101 | + }); |
| 102 | +}; |
0 commit comments