4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
-
8
7
// @ts -expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
9
- import type { Transformer } from 'unified' ;
10
- import type { Heading , Parent } from 'mdast' ;
8
+ import type { Transformer , Plugin } from 'unified' ;
9
+ import type { Heading , Parent , Root } from 'mdast' ;
11
10
12
11
// @ts -expect-error: ES support...
13
12
import type { MdxJsxFlowElement } from 'mdast-util-mdx' ;
14
13
15
- // TODO as of April 2023, no way to import/re-export this ESM type easily :/
16
- // TODO upgrade to TS 5.3
17
- // See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
18
- // import type {Plugin} from 'unified';
19
- type Plugin = any ; // TODO fix this asap
20
-
21
14
interface PluginOptions {
22
15
removeContentTitle ?: boolean ;
23
16
}
@@ -41,35 +34,36 @@ function wrapHeadingInJsxHeader(
41
34
* This is exposed as "data.contentTitle" to the processed vfile
42
35
* Also gives the ability to strip that content title (used for the blog plugin)
43
36
*/
44
- const plugin : Plugin = function plugin (
45
- options : PluginOptions = { } ,
46
- ) : Transformer {
37
+ const plugin : Plugin < PluginOptions [ ] , Root > = function plugin (
38
+ options = { } ,
39
+ ) : Transformer < Root > {
47
40
// content title is
48
41
const removeContentTitle = options . removeContentTitle ?? false ;
49
42
50
43
return async ( root , vfile ) => {
51
44
const { toString} = await import ( 'mdast-util-to-string' ) ;
52
45
const { visit, EXIT } = await import ( 'unist-util-visit' ) ;
53
46
visit ( root , [ 'heading' , 'thematicBreak' ] , ( node , index , parent ) => {
47
+ if ( ! parent || index === undefined ) {
48
+ return undefined ;
49
+ }
54
50
if ( node . type === 'heading' ) {
55
- const headingNode = node as Heading ;
56
51
// console.log('headingNode:', headingNode);
57
52
58
- if ( headingNode . depth === 1 ) {
59
- vfile . data . contentTitle = toString ( headingNode ) ;
53
+ if ( node . depth === 1 ) {
54
+ vfile . data . contentTitle = toString ( node ) ;
60
55
if ( removeContentTitle ) {
61
- // @ts -expect-error: TODO how to fix?
62
- parent ! . children . splice ( index , 1 ) ;
56
+ parent . children . splice ( index , 1 ) ;
63
57
} else {
64
58
// TODO in the future it might be better to export contentTitle as
65
59
// as JSX node to keep this logic a theme concern?
66
60
// See https://github.com/facebook/docusaurus/pull/10335#issuecomment-2250187371
67
- wrapHeadingInJsxHeader ( headingNode , parent , index ! ) ;
61
+ wrapHeadingInJsxHeader ( node , parent , index ) ;
68
62
}
69
63
return EXIT ; // We only handle the very first heading
70
64
}
71
65
// We only handle contentTitle if it's the very first heading found
72
- if ( headingNode . depth >= 1 ) {
66
+ if ( node . depth >= 1 ) {
73
67
return EXIT ;
74
68
}
75
69
}
0 commit comments