@mintlify/common
Version:
Commonly shared code within Mintlify
24 lines (23 loc) • 1.02 kB
JavaScript
import { visit } from 'unist-util-visit';
export const remarkSplitCodeGroup = () => (tree) => {
const codeGroupCompoenents = ['CodeGroup', 'RequestExample', 'ResponseExample'];
const groupsToProcess = [];
visit(tree, 'mdxJsxFlowElement', (node, index, parent) => {
if (node.name &&
codeGroupCompoenents.includes(node.name) &&
typeof index === 'number' &&
parent) {
groupsToProcess.push({ node, index, parent });
}
});
// Split the code group into multiple CodeBlock nodes
// basically remove the CodeGroup component and replace it with the codeblocks
// process in reverse document order so splices don't shift saved sibling indices
for (const { node, index, parent } of groupsToProcess.reverse()) {
const numberOfCodeBlocks = node.children.length;
if (numberOfCodeBlocks <= 1)
continue;
const newNodes = node.children;
parent.children.splice(index, 1, ...newNodes);
}
};