diff options
author | 2023-05-17 09:13:10 -0400 | |
---|---|---|
committer | 2023-05-17 09:13:10 -0400 | |
commit | fb84622af04f795de8d17f24192de105f70fe910 (patch) | |
tree | 11a99efdb90c17207d3adc1095e88fa8daddd7e4 /packages/integrations/markdoc/src/nodes/heading.ts | |
parent | c91e837e961043e92253148f0f4291856653b993 (diff) | |
download | astro-fb84622af04f795de8d17f24192de105f70fe910.tar.gz astro-fb84622af04f795de8d17f24192de105f70fe910.tar.zst astro-fb84622af04f795de8d17f24192de105f70fe910.zip |
[Markdoc] `headings` and heading IDs (#7095)
* deps: markdown-remark
* wip: heading-ids function
* chore: add `@astrojs/markdoc` to external
* feat: `headings` support
* fix: allow `render` config on headings
* fix: nonexistent `userConfig`
* test: headings, toc, astro component render
* docs: README
* chore: changeset
* refactor: expose Markdoc helpers from runtime
* fix: bad named exports (commonjsssss)
* refactor: defaultNodes -> nodes
* deps: github-slugger
* fix: reset slugger cache on each render
* fix: bad astroNodes import
* docs: explain headingSlugger export
* docs: add back double stringify comment
* chore: bump to minor for internal exports change
Diffstat (limited to 'packages/integrations/markdoc/src/nodes/heading.ts')
-rw-r--r-- | packages/integrations/markdoc/src/nodes/heading.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/integrations/markdoc/src/nodes/heading.ts b/packages/integrations/markdoc/src/nodes/heading.ts new file mode 100644 index 000000000..81a9181c7 --- /dev/null +++ b/packages/integrations/markdoc/src/nodes/heading.ts @@ -0,0 +1,42 @@ +import Markdoc, { type RenderableTreeNode, type Schema } from '@markdoc/markdoc'; +import { getTextContent } from '../runtime.js'; +import Slugger from 'github-slugger'; + +export const headingSlugger = new Slugger(); + +function getSlug(attributes: Record<string, any>, children: RenderableTreeNode[]): string { + if (attributes.id && typeof attributes.id === 'string') { + return attributes.id; + } + const textContent = attributes.content ?? getTextContent(children); + let slug = headingSlugger.slug(textContent); + + if (slug.endsWith('-')) slug = slug.slice(0, -1); + return slug; +} + +export const heading: Schema = { + children: ['inline'], + attributes: { + id: { type: String }, + level: { type: Number, required: true, default: 1 }, + }, + transform(node, config) { + const { level, ...attributes } = node.transformAttributes(config); + const children = node.transformChildren(config); + + + const slug = getSlug(attributes, children); + + const render = config.nodes?.heading?.render ?? `h${level}`; + const tagProps = + // For components, pass down `level` as a prop, + // alongside `__collectHeading` for our `headings` collector. + // Avoid accidentally rendering `level` as an HTML attribute otherwise! + typeof render === 'function' + ? { ...attributes, id: slug, __collectHeading: true, level } + : { ...attributes, id: slug }; + + return new Markdoc.Tag(render, tagProps, children); + }, +}; |