diff options
author | 2022-12-20 23:08:15 +0100 | |
---|---|---|
committer | 2022-12-20 23:08:15 +0100 | |
commit | 2c65b433bf840a1bb93b0a1947df5949e33512ff (patch) | |
tree | 02314c6ee09b86d08e40367da647458806c932db /packages/integrations | |
parent | a467139e169ad2eb7931e03004f1d658f7362e59 (diff) | |
download | astro-2c65b433bf840a1bb93b0a1947df5949e33512ff.tar.gz astro-2c65b433bf840a1bb93b0a1947df5949e33512ff.tar.zst astro-2c65b433bf840a1bb93b0a1947df5949e33512ff.zip |
MD/MDX collect headings refactor (#5654)
Diffstat (limited to 'packages/integrations')
-rw-r--r-- | packages/integrations/mdx/package.json | 1 | ||||
-rw-r--r-- | packages/integrations/mdx/src/plugins.ts | 14 | ||||
-rw-r--r-- | packages/integrations/mdx/src/rehype-collect-headings.ts | 47 | ||||
-rw-r--r-- | packages/integrations/mdx/test/mdx-get-headings.test.js | 91 |
4 files changed, 106 insertions, 47 deletions
diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json index 01bbeb610..08a681097 100644 --- a/packages/integrations/mdx/package.json +++ b/packages/integrations/mdx/package.json @@ -30,6 +30,7 @@ "test:match": "mocha --timeout 20000 -g" }, "dependencies": { + "@astrojs/markdown-remark": "^1.1.3", "@astrojs/prism": "^1.0.2", "@mdx-js/mdx": "^2.1.2", "@mdx-js/rollup": "^2.1.1", diff --git a/packages/integrations/mdx/src/plugins.ts b/packages/integrations/mdx/src/plugins.ts index 0ae148746..d46ab6cd4 100644 --- a/packages/integrations/mdx/src/plugins.ts +++ b/packages/integrations/mdx/src/plugins.ts @@ -1,3 +1,4 @@ +import { rehypeHeadingIds } from '@astrojs/markdown-remark'; import { nodeTypes } from '@mdx-js/mdx'; import type { PluggableList } from '@mdx-js/mdx/lib/core.js'; import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup'; @@ -10,7 +11,7 @@ import remarkGfm from 'remark-gfm'; import remarkSmartypants from 'remark-smartypants'; import type { Data, VFile } from 'vfile'; import { MdxOptions } from './index.js'; -import rehypeCollectHeadings from './rehype-collect-headings.js'; +import { rehypeInjectHeadingsExport } from './rehype-collect-headings.js'; import rehypeMetaString from './rehype-meta-string.js'; import remarkPrism from './remark-prism.js'; import remarkShiki from './remark-shiki.js'; @@ -153,8 +154,6 @@ export function getRehypePlugins( config: AstroConfig ): MdxRollupPluginOptions['rehypePlugins'] { let rehypePlugins: PluggableList = [ - // getHeadings() is guaranteed by TS, so we can't allow user to override - rehypeCollectHeadings, // ensure `data.meta` is preserved in `properties.metastring` for rehype syntax highlighters rehypeMetaString, // rehypeRaw allows custom syntax highlighters to work without added config @@ -175,7 +174,14 @@ export function getRehypePlugins( break; } - rehypePlugins = [...rehypePlugins, ...(mdxOptions.rehypePlugins ?? [])]; + rehypePlugins = [ + ...rehypePlugins, + ...(mdxOptions.rehypePlugins ?? []), + // getHeadings() is guaranteed by TS, so this must be included. + // We run `rehypeHeadingIds` _last_ to respect any custom IDs set by user plugins. + rehypeHeadingIds, + rehypeInjectHeadingsExport, + ]; return rehypePlugins; } diff --git a/packages/integrations/mdx/src/rehype-collect-headings.ts b/packages/integrations/mdx/src/rehype-collect-headings.ts index 64bd7182b..e6cd20a8d 100644 --- a/packages/integrations/mdx/src/rehype-collect-headings.ts +++ b/packages/integrations/mdx/src/rehype-collect-headings.ts @@ -1,48 +1,9 @@ -import Slugger from 'github-slugger'; -import { visit } from 'unist-util-visit'; +import { MarkdownVFile, MarkdownHeading } from '@astrojs/markdown-remark'; import { jsToTreeNode } from './utils.js'; -export interface MarkdownHeading { - depth: number; - slug: string; - text: string; -} - -export default function rehypeCollectHeadings() { - const slugger = new Slugger(); - return function (tree: any) { - const headings: MarkdownHeading[] = []; - visit(tree, (node) => { - if (node.type !== 'element') return; - const { tagName } = node; - if (tagName[0] !== 'h') return; - const [_, level] = tagName.match(/h([0-6])/) ?? []; - if (!level) return; - const depth = Number.parseInt(level); - - let text = ''; - visit(node, (child, __, parent) => { - if (child.type === 'element' || parent == null) { - return; - } - if (child.type === 'raw' && child.value.match(/^\n?<.*>\n?$/)) { - return; - } - if (new Set(['text', 'raw', 'mdxTextExpression']).has(child.type)) { - text += child.value; - } - }); - - node.properties = node.properties || {}; - if (typeof node.properties.id !== 'string') { - let slug = slugger.slug(text); - if (slug.endsWith('-')) { - slug = slug.slice(0, -1); - } - node.properties.id = slug; - } - headings.push({ depth, slug: node.properties.id, text }); - }); +export function rehypeInjectHeadingsExport() { + return function (tree: any, file: MarkdownVFile) { + const headings: MarkdownHeading[] = file.data.__astroHeadings || []; tree.children.unshift( jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`) ); diff --git a/packages/integrations/mdx/test/mdx-get-headings.test.js b/packages/integrations/mdx/test/mdx-get-headings.test.js index 1ac7283dd..03290abc5 100644 --- a/packages/integrations/mdx/test/mdx-get-headings.test.js +++ b/packages/integrations/mdx/test/mdx-get-headings.test.js @@ -1,4 +1,6 @@ +import { rehypeHeadingIds } from '@astrojs/markdown-remark'; import mdx from '@astrojs/mdx'; +import { visit } from 'unist-util-visit'; import { expect } from 'chai'; import { parseHTML } from 'linkedom'; @@ -58,3 +60,92 @@ describe('MDX getHeadings', () => { ); }); }); + +describe('MDX heading IDs can be customized by user plugins', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/mdx-get-headings/', import.meta.url), + integrations: [mdx()], + markdown: { + rehypePlugins: [ + () => (tree) => { + let count = 0; + visit(tree, 'element', (node, index, parent) => { + if (!/^h\d$/.test(node.tagName)) return; + if (!node.properties?.id) { + node.properties = { ...node.properties, id: String(count++) }; + } + }); + }, + ], + }, + }); + + await fixture.build(); + }); + + it('adds user-specified IDs to HTML output', async () => { + const html = await fixture.readFile('/test/index.html'); + const { document } = parseHTML(html); + + const h1 = document.querySelector('h1'); + expect(h1?.textContent).to.equal('Heading test'); + expect(h1?.getAttribute('id')).to.equal('0'); + + const headingIDs = document.querySelectorAll('h1,h2,h3').map((el) => el.id); + expect(JSON.stringify(headingIDs)).to.equal( + JSON.stringify(Array.from({ length: headingIDs.length }, (_, idx) => String(idx))) + ); + }); + + it('generates correct getHeadings() export', async () => { + const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json')); + expect(JSON.stringify(headingsByPage['./test.mdx'])).to.equal( + JSON.stringify([ + { depth: 1, slug: '0', text: 'Heading test' }, + { depth: 2, slug: '1', text: 'Section 1' }, + { depth: 3, slug: '2', text: 'Subsection 1' }, + { depth: 3, slug: '3', text: 'Subsection 2' }, + { depth: 2, slug: '4', text: 'Section 2' }, + ]) + ); + }); +}); + +describe('MDX heading IDs can be injected before user plugins', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/mdx-get-headings/', import.meta.url), + integrations: [ + mdx({ + rehypePlugins: [ + rehypeHeadingIds, + () => (tree) => { + visit(tree, 'element', (node, index, parent) => { + if (!/^h\d$/.test(node.tagName)) return; + if (node.properties?.id) { + node.children.push({ type: 'text', value: ' ' + node.properties.id }); + } + }); + }, + ], + }), + ], + }); + + await fixture.build(); + }); + + it('adds user-specified IDs to HTML output', async () => { + const html = await fixture.readFile('/test/index.html'); + const { document } = parseHTML(html); + + const h1 = document.querySelector('h1'); + expect(h1?.textContent).to.equal('Heading test heading-test'); + expect(h1?.id).to.equal('heading-test'); + }); +}); |