11import { visit } from 'unist-util-visit'
22import { toString } from 'hast-util-to-string'
3+ import type { Element , Root } from 'hast'
4+ import type { VFile } from 'vfile'
35
46import { isHeading } from './helpers'
57
@@ -9,19 +11,44 @@ export type MarkdownHeading = {
911 level : number
1012}
1113
14+ const isTabsAncestor = ( ancestor : Element ) => {
15+ if ( ancestor . type !== 'element' ) {
16+ console . log ( 'skip' )
17+ return false
18+ }
19+
20+ if ( ancestor . tagName !== 'md-comment-component' ) {
21+ console . log ( 'skip' )
22+ return false
23+ }
24+
25+ const component = ancestor . properties ?. [ 'data-component' ]
26+ console . log ( 'dont skip' , component )
27+ return typeof component === 'string' && component . toLowerCase ( ) === 'tabs'
28+ }
29+
1230export function rehypeCollectHeadings (
13- tree ,
14- file ,
31+ _tree : Root ,
32+ _file : VFile ,
1533 initialHeadings ?: MarkdownHeading [ ] ,
1634) {
1735 const headings = initialHeadings ?? [ ]
1836
19- return function collectHeadings ( tree , file : any ) {
20- visit ( tree , 'element' , ( node ) => {
37+ return function collectHeadings ( tree : Root , file ?: VFile ) {
38+ visit ( tree , 'element' , ( node : Element , _index , ancestors ) => {
2139 if ( ! isHeading ( node ) ) {
2240 return
2341 }
2442
43+ if ( Array . isArray ( ancestors ) ) {
44+ const insideTabs = ancestors . some ( ( ancestor ) =>
45+ isTabsAncestor ( ancestor as Element ) ,
46+ )
47+ if ( insideTabs ) {
48+ return
49+ }
50+ }
51+
2552 const id =
2653 typeof node . properties ?. id === 'string' ? node . properties . id : ''
2754 if ( ! id ) {
0 commit comments