File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -4,17 +4,26 @@ require("./styles/styles.less");
4
4
5
5
$ ( function ( ) {
6
6
var headings = { } ;
7
+ var collected = collectHeadings ( ) ;
7
8
8
- collectHeadings ( ) . each ( function ( ) {
9
+ collected . each ( function ( ) {
10
+ var $el = $ ( this ) ;
11
+ var id = $el . attr ( "id" ) ;
12
+
13
+ if ( id ) headings [ id ] = true ;
14
+ } ) ;
15
+
16
+
17
+ collected . each ( function ( ) {
9
18
var $el = $ ( this ) ;
10
19
var id = $el . attr ( "id" ) ;
11
20
12
21
if ( ! id ) {
13
22
id = makeAnchorHeadingId ( $el . text ( ) ) ;
14
- var count = headings [ id ] || 0 ;
23
+ var token = getUniqueToken ( id , headings ) ;
15
24
16
- id += ( count > 0 ) ? "-" + count : "" ;
17
- headings [ id ] = count + 1 ;
25
+ id += ( token > 0 ) ? "-" + token : "" ;
26
+ headings [ id ] = true ;
18
27
19
28
$el . attr ( "id" , id ) ;
20
29
}
@@ -31,6 +40,18 @@ $(function() {
31
40
}
32
41
}
33
42
43
+ function getUniqueToken ( id , headings ) {
44
+ var token = 0 ;
45
+ var uniq = id ;
46
+
47
+ while ( headings [ uniq ] ) {
48
+ token += 1 ;
49
+ uniq = id + "-" + token ;
50
+ }
51
+
52
+ return token ;
53
+ }
54
+
34
55
function collectHeadings ( ) {
35
56
return $ ( ".content .comment h2, .content .comment h3" ) ;
36
57
}
You can’t perform that action at this time.
0 commit comments