Skip to content

Commit

Permalink
docs: add function docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed Dec 11, 2024
1 parent 5323184 commit 31e8bb9
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions apify-docs-theme/src/markdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const { remark } = require('remark');

/**
* Updates the markdown content for better UX and compatibility with Docusaurus v3.
* @param {string} changelog The markdown content.
* @returns {string} The updated markdown content.
*/
function updateChangelog(changelog) {
const tree = remark.parse(changelog);

Expand All @@ -13,6 +18,13 @@ function updateChangelog(changelog) {
return changelog;
}

/**
* Bumps the headings levels in the markdown content. This function increases the depth
* of all headings in the content by 1. This is useful when the content is included in
* another markdown file with a higher-level heading.
* @param {*} tree Remark AST tree.
* @returns {void} Nothing. This function modifies the tree in place.
*/
function bumpHeadingsLevels(tree) {
tree.children?.forEach((child) => {
if (child.type === 'heading') {
Expand All @@ -23,6 +35,12 @@ function bumpHeadingsLevels(tree) {
});
}

/**
* Links user tags in the markdown content. This function replaces the user tags
* (e.g. `@username`) with a link to the user's GitHub profile (just like GitHub's UI).
* @param {*} tree Remark AST tree.
* @returns {void} Nothing. This function modifies the tree in place.
*/
function linkifyUserTags(tree) {
for (let i = 0; i < tree.children?.length; i++) {
const child = tree.children[i];
Expand Down Expand Up @@ -56,7 +74,12 @@ function linkifyUserTags(tree) {
}
}

// If there is a PR URL (https://github.com/**/**/pull/number) in the text body, split the body, and replace the URL with a link to the PR (the text should be the PR number).
/**
* Prettifies PR links in the markdown content. Just like GitHub's UI, this function
* replaces the full PR URL with a link represented by the PR number (prefixed by a hashtag).
* @param {*} tree Remark AST tree.
* @returns {void} Nothing. This function modifies the tree in place.
*/
function prettifyPRLinks(tree) {
for (let i = 0; i < tree.children?.length; i++) {
const child = tree.children[i];
Expand Down Expand Up @@ -90,15 +113,27 @@ function prettifyPRLinks(tree) {
}
}

function addFrontmatter(changelog, header = 'Changelog') {
/**
* Adds frontmatter to the markdown content.
* @param {string} changelog The markdown content.
* @param {string} title The frontmatter title.
* @returns {string} The markdown content with frontmatter.
*/
function addFrontmatter(changelog, title = 'Changelog') {
return `---
title: ${header}
sidebar_label: ${header}
title: ${title}
sidebar_label: ${title}
toc_max_heading_level: 3
---
${changelog}`;
}

/**
* Escapes the MDX-related characters in the markdown content.
* This is required by Docusaurus v3 and its dependencies (see the v3 [migration guide](https://docusaurus.io/docs/migration/v3#common-mdx-problems)).
* @param {string} changelog The markdown content.
* @returns {string} The markdown content with escaped MDX characters.
*/
function escapeMDXCharacters(changelog) {
return changelog.replaceAll(/<|>/g, (match) => {
return match === '<' ? '&lt;' : '&gt;';
Expand Down

0 comments on commit 31e8bb9

Please sign in to comment.