@mintlify/common
Version:
Commonly shared code within Mintlify
19 lines (18 loc) • 884 B
JavaScript
import { visit } from 'unist-util-visit';
import { ALL_HEADING_NAMES } from '../../preprocessCustomHeadingIds.js';
/**
* Unwraps paragraph children inside JSX heading elements (h1–h6).
*
* The custom-ID preprocessor emits headings like `<h2 id="x">\nText\n</h2>`,
* and MDX parses the text on its own line as a paragraph block. This plugin
* flattens those paragraphs so the heading receives inline children directly.
*/
export const remarkUnwrapJsxHeadings = () => (tree) => {
visit(tree, 'mdxJsxFlowElement', (node) => {
var _a;
if (!ALL_HEADING_NAMES.includes((_a = node.name) !== null && _a !== void 0 ? _a : ''))
return;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
node.children = node.children.flatMap((child) => child.type === 'paragraph' && 'children' in child ? child.children : [child]);
});
};