Skip to content

Commit 765b0bf

Browse files
authored
fix: escape MDX characters in changelog (Docusaurus v3) (#1184)
1 parent d4dc765 commit 765b0bf

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

apify-docs-theme/src/markdown.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
function updateChangelog(changelog) {
2-
changelog = addHeader(changelog);
2+
changelog = addFrontmatter(changelog);
33
changelog = pushHeadings(changelog);
4-
changelog = linkUsers(changelog);
5-
changelog = linkPRs(changelog);
4+
changelog = fixUserLinks(changelog);
5+
changelog = fixPRLinks(changelog);
6+
changelog = escapeMDXCharacters(changelog);
67
return changelog;
78
}
89

9-
function addHeader(changelog, header = 'Changelog') {
10+
function addFrontmatter(changelog, header = 'Changelog') {
1011
return `---
1112
title: ${header}
1213
sidebar_label: ${header}
@@ -19,14 +20,22 @@ function pushHeadings(changelog) {
1920
return changelog.replaceAll(/\n#[^#]/g, '\n## ');
2021
}
2122

22-
function linkUsers(changelog) {
23+
function fixUserLinks(changelog) {
2324
return changelog.replaceAll(/by @([a-zA-Z0-9-]+)/g, 'by [@$1](https://github.com/$1)');
2425
}
2526

26-
function linkPRs(changelog) {
27+
function fixPRLinks(changelog) {
2728
return changelog.replaceAll(/(((https?:\/\/)?(www.)?)?github.com\/[^\s]*?\/pull\/([0-9]+))/g, '[#$5]($1)');
2829
}
2930

31+
function escapeMDXCharacters(changelog) {
32+
return changelog.replaceAll(/<|>/g, (match) => {
33+
return match === '<' ? '&lt;' : '&gt;';
34+
}).replaceAll(/\{|\}/g, (match) => {
35+
return match === '{' ? '&#123;' : '&#125;';
36+
});
37+
}
38+
3039
module.exports = {
3140
updateChangelog,
3241
};

apify-docs-theme/src/theme.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function findPathInParentOrThrow(endPath) {
2525
return filePath;
2626
}
2727

28-
async function copyChangelogFromReleases(paths, repo) {
28+
async function generateChangelogFromGitHubReleases(paths, repo) {
2929
const response = await axios.get(`https://api.github.com/repos/${repo}/releases`);
3030
const releases = response.data;
3131

@@ -81,6 +81,12 @@ function theme(
8181
),
8282
];
8383

84+
if (options.changelogFromRoot) {
85+
copyChangelogFromRoot(pathsToCopyChangelog);
86+
} else {
87+
await generateChangelogFromGitHubReleases(pathsToCopyChangelog, `${context.siteConfig.organizationName}/${context.siteConfig.projectName}`);
88+
}
89+
8490
for (const p of pathsToCopyChangelog) {
8591
// the changelog page has to exist for the sidebar to work - async loadContent() is (apparently) not awaited for by sidebar
8692
if (fs.existsSync(path.join(p, 'changelog.md'))) continue;
@@ -92,12 +98,6 @@ It seems that the changelog is not available.
9298
This either means that your Docusaurus setup is misconfigured, or that your GitHub repository contains no releases yet.
9399
`);
94100
}
95-
96-
if (options.changelogFromRoot) {
97-
copyChangelogFromRoot(pathsToCopyChangelog);
98-
} else {
99-
await copyChangelogFromReleases(pathsToCopyChangelog, `${context.siteConfig.organizationName}/${context.siteConfig.projectName}`);
100-
}
101101
} catch (e) {
102102
// eslint-disable-next-line no-console
103103
console.warn(`Changelog page could not be initialized: ${e.message}`);

0 commit comments

Comments
 (0)