UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

18 lines (17 loc) 793 B
import { visit } from 'unist-util-visit'; import { getUnicodeId, getTOCTitle } from '../../lib/remark-utils.js'; const DEEPLINKABLE_COMPONENTS = ['Heading', 'Update', 'Accordion']; export function rehypeUnicodeIds() { return (tree) => { visit(tree, 'mdxJsxFlowElement', (node) => { if (node && node.name && DEEPLINKABLE_COMPONENTS.includes(node.name)) { const title = getTOCTitle(node); const encodedId = getUnicodeId(title); const existingIdIndex = node.attributes.findIndex((attr) => attr.name === 'id'); if (existingIdIndex !== -1 && !node.attributes[existingIdIndex].value) { node.attributes[existingIdIndex].value = encodedId; } } }); }; }