Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions apify-docs-theme/src/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand All @@ -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)
Expand All @@ -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;
}
Expand Down
22 changes: 15 additions & 7 deletions apify-docs-theme/src/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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 }));
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -95,17 +99,21 @@ 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.
`);
hasDefaultChangelog.set(p, true);
}

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}`);
Expand Down