UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

36 lines (35 loc) 1.55 kB
import slugify from '@sindresorhus/slugify'; import { visit } from 'unist-util-visit'; export function generateParamFieldId(name, count) { const suffix = count > 0 ? `_${count}` : ''; return slugify(`param-${name}${suffix}`, { decamelize: true, separator: '-' }); } export const rehypeParamFieldIds = () => { return (tree) => { const paramCounts = new Map(); visit(tree, 'mdxJsxFlowElement', (element) => { var _a; if (element.name === 'ParamField' || element.name === 'Param' || element.name === 'ResponseField') { const nameAttr = element.attributes.find((attr) => 'name' in attr && (attr.name === 'query' || attr.name === 'path' || attr.name === 'body' || attr.name === 'header' || attr.name === 'name')); if (nameAttr && typeof nameAttr.value === 'string' && nameAttr.value) { const currentCount = (_a = paramCounts.get(nameAttr.value)) !== null && _a !== void 0 ? _a : 0; paramCounts.set(nameAttr.value, currentCount + 1); const id = generateParamFieldId(nameAttr.value, currentCount); element.attributes.push({ type: 'mdxJsxAttribute', name: 'id', value: id, }); } } }); return tree; }; };