UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

42 lines (41 loc) 1.48 kB
import * as acorn from 'acorn'; import { fromMarkdown } from 'mdast-util-from-markdown'; import { mdxJsxFromMarkdown } from 'mdast-util-mdx-jsx'; import { mdxJsx } from 'micromark-extension-mdx-jsx'; export const createMdxJsxAttribute = (key, value) => { return { type: 'mdxJsxAttribute', name: key, value, }; }; export const toMdxJsxFlowElement = (input) => { const tree = fromMarkdown(input, { extensions: [mdxJsx({ acorn: acorn, addResult: true })], mdastExtensions: [mdxJsxFromMarkdown()], }); return tree.children[0]; }; export const getUnicodeId = (title) => { return encodeURIComponent(title.toLowerCase().trim().replace(/\s+/g, '-')); }; export const getTOCTitle = (node, i = 1, a = []) => { var _a, _b, _c; if (node.name === 'Update') { const labelAttr = node.attributes.find((attr) => attr.name === 'label'); return labelAttr.value; } if ((node.type === 'text' && (((_a = a[i - 1]) === null || _a === void 0 ? void 0 : _a.type) !== 'mdxJsxFlowElement' || !((_c = (_b = a[i - 1]) === null || _b === void 0 ? void 0 : _b.value) === null || _c === void 0 ? void 0 : _c.startsWith('<small')))) || node.type === 'inlineCode') { return node.value; } if (node.children) { let title = ''; node.children.forEach((node, i, a) => { title += getTOCTitle(node, i, a); }); return title; } return ''; };