diff --git a/apify-docs-theme/src/markdown.js b/apify-docs-theme/src/markdown.js index 9af65e34a1..430d477e8c 100644 --- a/apify-docs-theme/src/markdown.js +++ b/apify-docs-theme/src/markdown.js @@ -102,14 +102,20 @@ const prettifyPRLinks = () => (tree) => { /** * Adds frontmatter to the markdown content. * @param {string} changelog The markdown content. - * @param {string} title The frontmatter title. + * @param {object} [opts] + * @param {string} [opts.title] The frontmatter title. + * @param {string} [opts.displayedSidebar] The sidebar to display on the changelog page. Omitted from frontmatter if undefined. * @returns {string} The markdown content with frontmatter. */ -function addFrontmatter(changelog, title = 'Changelog') { +function addFrontmatter(changelog, { title = 'Changelog', displayedSidebar } = {}) { + const fields = [ + `title: ${title}`, + `sidebar_label: ${title}`, + ...(displayedSidebar !== undefined ? [`displayed_sidebar: ${displayedSidebar}`] : []), + `toc_max_heading_level: 3`, + ]; return `--- -title: ${title} -sidebar_label: ${title} -toc_max_heading_level: 3 +${fields.join('\n')} --- ${changelog}`; } @@ -131,9 +137,11 @@ function escapeMDXCharacters(changelog) { /** * Updates the markdown content for better UX and compatibility with Docusaurus v3. * @param {string} changelog The markdown content. + * @param {object} [opts] + * @param {string} [opts.displayedSidebar] The sidebar to display on the changelog page. * @returns {string} The updated markdown content. */ -function updateChangelog(changelog) { +function updateChangelog(changelog, { displayedSidebar } = {}) { const pipeline = unified() .use(remarkParse) .use(removeGitCliffMarkers) @@ -143,7 +151,7 @@ function updateChangelog(changelog) { .use(remarkStringify); changelog = pipeline.processSync(changelog).toString(); - changelog = addFrontmatter(changelog); + changelog = addFrontmatter(changelog, { displayedSidebar }); changelog = escapeMDXCharacters(changelog); return changelog; } diff --git a/apify-docs-theme/src/theme.js b/apify-docs-theme/src/theme.js index a8fd4b9977..a2f64054cb 100644 --- a/apify-docs-theme/src/theme.js +++ b/apify-docs-theme/src/theme.js @@ -25,7 +25,7 @@ function findPathInParentOrThrow(endPath) { return filePath; } -async function generateChangelogFromGitHubReleases(paths, repo) { +async function generateChangelogFromGitHubReleases(paths, repo, { displayedSidebar } = {}) { const response = await axios.get(`https://api.github.com/repos/${repo}/releases`); const releases = response.data; @@ -40,11 +40,11 @@ async function generateChangelogFromGitHubReleases(paths, repo) { }); paths.forEach((p) => { - fs.writeFileSync(path.join(p, 'changelog.md'), updateChangelog(markdown)); + fs.writeFileSync(path.join(p, 'changelog.md'), updateChangelog(markdown, { displayedSidebar })); }); } -function copyChangelogFromRoot(paths, hasDefaultChangelog) { +function copyChangelogFromRoot(paths, hasDefaultChangelog, { displayedSidebar } = {}) { const sourceChangelogPath = findPathInParentOrThrow('CHANGELOG.md'); for (const docsPath of paths) { @@ -57,7 +57,7 @@ function copyChangelogFromRoot(paths, hasDefaultChangelog) { } const changelog = fs.readFileSync(sourceChangelogPath, 'utf-8'); - fs.writeFileSync(targetChangelogPath, updateChangelog(changelog)); + fs.writeFileSync(targetChangelogPath, updateChangelog(changelog, { displayedSidebar })); } } @@ -87,6 +87,10 @@ function theme( ), ]; + const { changelogDisplayedSidebar: displayedSidebar } = options; + const displayedSidebarLine = displayedSidebar !== undefined + ? `displayed_sidebar: ${displayedSidebar}\n` + : ''; const hasDefaultChangelog = new Map(); for (const p of pathsToCopyChangelog) { @@ -95,7 +99,7 @@ function theme( fs.writeFileSync(`${p}/changelog.md`, `--- title: Changelog sidebar_label: Changelog ---- +${displayedSidebarLine}--- It seems that the changelog is not available. This either means that your Docusaurus setup is misconfigured, or that your GitHub repository contains no releases yet. `); @@ -103,9 +107,13 @@ This either means that your Docusaurus setup is misconfigured, or that your GitH } if (options.changelogFromRoot) { - copyChangelogFromRoot(pathsToCopyChangelog, hasDefaultChangelog); + copyChangelogFromRoot(pathsToCopyChangelog, hasDefaultChangelog, { displayedSidebar }); } else { - await generateChangelogFromGitHubReleases(pathsToCopyChangelog, `${context.siteConfig.organizationName}/${context.siteConfig.projectName}`); + await generateChangelogFromGitHubReleases( + pathsToCopyChangelog, + `${context.siteConfig.organizationName}/${context.siteConfig.projectName}`, + { displayedSidebar }, + ); } } catch (e) { console.warn(`Changelog page could not be initialized: ${e.message}`);