summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/test
diff options
context:
space:
mode:
authorGravatar Chris Swithinbank <swithinbank@gmail.com> 2022-12-20 23:08:15 +0100
committerGravatar GitHub <noreply@github.com> 2022-12-20 23:08:15 +0100
commit2c65b433bf840a1bb93b0a1947df5949e33512ff (patch)
tree02314c6ee09b86d08e40367da647458806c932db /packages/integrations/mdx/test
parenta467139e169ad2eb7931e03004f1d658f7362e59 (diff)
downloadastro-2c65b433bf840a1bb93b0a1947df5949e33512ff.tar.gz
astro-2c65b433bf840a1bb93b0a1947df5949e33512ff.tar.zst
astro-2c65b433bf840a1bb93b0a1947df5949e33512ff.zip
MD/MDX collect headings refactor (#5654)
Diffstat (limited to 'packages/integrations/mdx/test')
-rw-r--r--packages/integrations/mdx/test/mdx-get-headings.test.js91
1 files changed, 91 insertions, 0 deletions
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');
+ });
+});