Skip to content

Commit 03591f0

Browse files
committed
Refactor headings test to reuse shared vendor-plugin test helpers
1 parent 6a172cf commit 03591f0

1 file changed

Lines changed: 4 additions & 14 deletions

File tree

‎website/src/audiobook/headings.test.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ import { AUDIO_DOC_IDS, readDocSource } from './docs.ts';
44
import { parseMdx } from './extract.ts';
55
import { createHeadingReader } from './headings.ts';
66
import type { MdNode } from './mdast.ts';
7+
import { loadDocusaurusPlugin, nodesOfType } from './testing.ts';
78

89
type HeadingWithProps = MdNode & { data?: { hProperties?: { id?: string } } };
910

10-
const headingNodes = (root: MdNode): MdNode[] => {
11-
const found: MdNode[] = [];
12-
const visit = (node: MdNode): void => {
13-
if (node.type === 'heading') found.push(node);
14-
(node.children ?? []).forEach(visit);
15-
};
16-
visit(root);
17-
return found;
18-
};
11+
const headingNodes = (root: MdNode): MdNode[] => nodesOfType(root, 'heading');
1912

2013
/** Ids as the extractor derives them: one slugger per document, explicit ids win. */
2114
const extractorIds = (mdx: string): string[] => {
@@ -29,16 +22,13 @@ const extractorIds = (mdx: string): string[] => {
2922
* what we narrate against and what the built page actually renders.
3023
*/
3124
const docusaurusIds = async (mdx: string): Promise<string[]> => {
32-
const mod = await import(
25+
const plugin = await loadDocusaurusPlugin(
3326
'@docusaurus/mdx-loader/lib/remark/headings/index.js'
3427
);
35-
// CJS interop: the module sets `exports.default`, so the plugin is one level deeper.
36-
const plugin = ((mod as { default?: { default?: unknown } }).default
37-
?.default ?? mod.default) as never;
3828
const { unified } = await import('unified');
3929
const root = parseMdx(mdx);
4030
await unified()
41-
.use(plugin, { anchorsMaintainCase: false } as never)
31+
.use(plugin as never, { anchorsMaintainCase: false } as never)
4232
.run(root as never);
4333
return headingNodes(root).map(
4434
(node) => (node as HeadingWithProps).data?.hProperties?.id ?? ''

0 commit comments

Comments
 (0)