UNPKG

uniorg-slug

Version:

uniorg plugin to add `id` attributes to headlines

25 lines 1.02 kB
import { visit } from 'unist-util-visit'; import GithubSlugger from 'github-slugger'; import { toString } from 'orgast-util-to-string'; export const uniorgSlug = (options = {}) => { return transformer; function transformer(tree, _file) { const slugger = new GithubSlugger(); visit(tree, 'section', (section) => { const headline = section.children[0]; const data = (headline.data = headline.data || {}); const props = (data.hProperties = data.hProperties || {}); if (!props.id) { const id = customId(section) ?? slugger.slug(toString(headline)); props.id = id; } }); } }; function customId(section) { const drawer = section.children.find((node) => node.type === 'property-drawer'); const property = drawer?.children?.find((node) => node.type === 'node-property' && node.key === 'CUSTOM_ID'); return property?.value; } export default uniorgSlug; //# sourceMappingURL=index.js.map