@@ -4,18 +4,11 @@ import { AUDIO_DOC_IDS, readDocSource } from './docs.ts';
44import { parseMdx } from './extract.ts' ;
55import { createHeadingReader } from './headings.ts' ;
66import type { MdNode } from './mdast.ts' ;
7+ import { loadDocusaurusPlugin , nodesOfType } from './testing.ts' ;
78
89type 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. */
2114const 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 */
3124const 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