@@ -20,6 +20,13 @@ function throttle(fn, ms){
20
20
} ;
21
21
}
22
22
23
+ function makeAnchorHeadingId ( anchorText ) {
24
+ return ( anchorText || "" )
25
+ . replace ( / \s / g, "-" ) // replace spaces with dashes
26
+ . replace ( / [ ^ \w \- ] / g, "" ) // remove punctuation
27
+ . toLowerCase ( ) ;
28
+ }
29
+
23
30
function outerHeight ( el ) {
24
31
var height = el . offsetHeight ;
25
32
var style = getComputedStyle ( el ) ;
@@ -98,9 +105,17 @@ var TableOfContents = Control.extend({
98
105
var titles = selector ? document . querySelectorAll ( selector ) : [ ] ;
99
106
var curScroll = this . scroller . scrollTop ;
100
107
var navHeight = this . navHeight ;
101
- return [ ] . map . call ( titles , function ( title , idx ) {
108
+ var headings = { } ;
109
+
110
+ return [ ] . map . call ( titles , function ( title , idx ) {
102
111
var txt = title . textContent ;
103
- title . id = 'section_' + txt . replace ( / \s / g, "" ) . replace ( / [ ^ \w ] / g, "_" ) ;
112
+ var id = makeAnchorHeadingId ( txt ) ;
113
+ var count = headings [ id ] || 0 ;
114
+
115
+ // add unique id if we get headings with the same text
116
+ title . id = makeAnchorHeadingId ( txt ) + ( count > 0 ? "-" + count : "" ) ;
117
+ headings [ id ] = count + 1 ;
118
+
104
119
return {
105
120
id : title . id ,
106
121
index : idx ,
0 commit comments