@@ -149,17 +149,47 @@ function parseAnchorLinks({
149149      type : string ; 
150150      value : string ; 
151151    } [ ] ; 
152+     data ?: any ; 
152153  } ; 
154+ 
153155  return  ( )  =>  { 
156+     // We need to keep track of how many times that we have encountered a 
157+     // given header ID, as to ensure that we don't run into any conflicts. 
158+     // If there is a conflict, the sidecar will run into issues, and only 
159+     // the first header will be able to be deep-linked to. 
160+     // 
161+     // In the event that we encounter a duplicate Header ID, we'll simply 
162+     // add a suffix to the ID to make it unique. e.g. if there are two headers 
163+     // with the same name "Foo", the first ID will be "foo", while the second 
164+     // will be "foo-2". 
165+     const  encounteredIDs  =  new  Map < string ,  number > ( ) ; 
166+ 
154167    return  function  ( node : Node )  { 
155168      visit ( node ,  "heading" ,  ( node : Node )  =>  { 
156169        if  ( node . type  ===  "heading" )  { 
157170          let  headingNode  =  node  as  HeadingNode ; 
158171          if  ( headingNode . children . length  >  0 )  { 
159172            const  text  =  headingNode . children . map ( ( v )  =>  v . value ) . join ( "" ) ; 
173+             const  baseId  =  slugify ( text . toLowerCase ( ) ) ; 
174+ 
175+             // If this is not the first occurrence, add a data-index attribute 
176+             const  encounteredCount  =  ( encounteredIDs . get ( baseId )  ||  0 )  +  1 ; 
177+             encounteredIDs . set ( baseId ,  encounteredCount ) ; 
178+             if  ( encounteredCount  >=  2 )  { 
179+               if  ( ! headingNode . data )  { 
180+                 headingNode . data  =  { } ; 
181+               } 
182+               headingNode . data . hProperties  =  { 
183+                 ...headingNode . data . hProperties , 
184+                 "data-index" : encounteredCount . toString ( ) , 
185+               } ; 
186+             } 
187+             const  resolvedID  = 
188+               encounteredCount  >=  2  ? `${ baseId }  -${ encounteredCount }  `  : baseId ; 
189+ 
160190            pageHeaders . push ( { 
161191              depth : headingNode . depth , 
162-               id : slugify ( text . toLowerCase ( ) ) , 
192+               id : resolvedID , 
163193              title : text , 
164194            } ) ; 
165195          } 
0 commit comments