@@ -23,18 +23,19 @@ export function toc(this: Processor, opts?: Options): Transformer {
23
23
24
24
let mainNode : Element | undefined ;
25
25
let mainParent : Parent | undefined ;
26
+ let placeholderNode : Element | undefined ;
27
+ let placeholderParent : Parent | undefined ;
26
28
27
29
if ( options . placeholder ) {
28
30
// Find the <target> element if option is given
29
- [ mainNode , mainParent ] = findPlaceholderNode ( root , options . placeholder ) ;
31
+ [ placeholderNode , placeholderParent ] = findPlaceholderNode ( root , options . placeholder ) ;
30
32
31
- if ( ! mainNode || ! mainParent ) { return root ; }
32
- }
33
- else {
34
- // Find the <main> or <body> element
35
- [ mainNode , mainParent ] = findMainNode ( root ) ;
33
+ if ( ! placeholderNode || ! placeholderParent ) { return root ; }
36
34
}
37
35
36
+ // Find the <main> or <body> element
37
+ [ mainNode , mainParent ] = findMainNode ( root ) ;
38
+
38
39
// Find all heading elements
39
40
let headings = findHeadings ( mainNode , options ) ;
40
41
@@ -46,7 +47,7 @@ export function toc(this: Processor, opts?: Options): Transformer {
46
47
47
48
if ( node ) {
48
49
// Add the table of contents to the <main> element
49
- insertTOC ( node , mainNode , mainParent , { ...options , replace : ! ! options . placeholder } ) ;
50
+ insertTOC ( node , placeholderNode || mainNode , placeholderParent || mainParent , { ...options , replace : ! ! options . placeholder } ) ;
50
51
}
51
52
52
53
return root ;
0 commit comments