@@ -24,7 +24,7 @@ const REGEXP_MARKDOWN_ANCHOR = /^<a id="markdown-.+" name=".+"><\/a\>/;
24
24
const REGEXP_HEADER = / ^ ( \# { 1 , 6 } ) \s * ( .+ ) / ;
25
25
const REGEXP_CODE_BLOCK1 = / ^ ` ` ` / ;
26
26
const REGEXP_CODE_BLOCK2 = / ^ ~ ~ ~ / ;
27
- const REGEXP_ANCHOR = / \[ . + \] \( # ( .+ ) \) / ;
27
+ const REGEXP_ANCHOR = / ^ \[ ( [ ^ \] ] + ) \] \( # ( .+ ) \) $ / ;
28
28
const REGEXP_IGNORE_TITLE = / < ! - - T O C i g n o r e : t r u e - - > / ;
29
29
30
30
const DEPTH_FROM = "depthFrom" ;
@@ -256,7 +256,7 @@ class MarkdownTocTools {
256
256
private insertAnchor ( editBuilder : TextEditorEdit , headerList : any [ ] ) {
257
257
if ( ! this . options . INSERT_ANCHOR ) return ;
258
258
headerList . forEach ( element => {
259
- let name = element . hash . match ( REGEXP_ANCHOR ) [ 1 ] ;
259
+ let name = element . hash . match ( REGEXP_ANCHOR ) [ 2 ] ;
260
260
let text = [ '<a id="markdown-' , name , '" name="' , name , '"></a>\n' ] ;
261
261
let insertPosition = new Position ( element . line , 0 ) ;
262
262
editBuilder . insert ( insertPosition , text . join ( '' ) ) ;
@@ -380,13 +380,7 @@ class MarkdownTocTools {
380
380
title = title . replace ( / \# / gi, "" ) . trim ( ) ; // replace special char
381
381
title = title . replace ( / \b [ _ * ] | [ * _ ] \b / gi, "" ) ; // replace bold and italic marks
382
382
383
- if ( ! ( title in hashMap ) ) {
384
- hashMap [ title ] = 0 ;
385
- } else {
386
- hashMap [ title ] += 1 ;
387
- }
388
-
389
- let hash = this . getHash ( title , this . options . ANCHOR_MODE , hashMap [ title ] ) ;
383
+ let hash = this . getHash ( title , this . options . ANCHOR_MODE , hashMap ) ;
390
384
headerList . push ( {
391
385
line : index ,
392
386
depth : depth ,
@@ -401,9 +395,39 @@ class MarkdownTocTools {
401
395
return headerList ;
402
396
}
403
397
404
- private getHash ( headername : string , mode : string , repetition : number ) {
405
- let anchor = require ( 'anchor-markdown-header' ) ;
406
- return anchor ( headername , mode , repetition ) ;
398
+ private getHash ( headername : string , mode : string , hashMap : { [ key : string ] : number } ) {
399
+ // Get the link format for headername (force repetition = 0)
400
+ let anchor = require ( 'anchor-markdown-header' ) ( headername , mode , 0 ) ;
401
+
402
+ // Decompose the anchor into its two components
403
+ let match = anchor . match ( REGEXP_ANCHOR ) ;
404
+ if ( ! match || match . length < 3 ) return anchor ;
405
+ let [ title , hash ] = match . slice ( 1 , 3 ) ;
406
+
407
+ // Check if the hash is repeated
408
+ if ( ! ( hash in hashMap ) ) {
409
+ hashMap [ hash ] = 0 ;
410
+ } else {
411
+ hashMap [ hash ] += 1 ;
412
+
413
+ // Add the repetition number to the hash
414
+ switch ( mode ) {
415
+ case "github.com" :
416
+ hash = `${ hash } -${ hashMap [ hash ] } ` ;
417
+ break ;
418
+ case "bitbucket.org" :
419
+ hash = `${ hash } _${ hashMap [ hash ] } ` ;
420
+ break ;
421
+ case "ghost.org" :
422
+ hash = `${ hash } -${ hashMap [ hash ] } ` ;
423
+ break ;
424
+ case "gitlab.com" :
425
+ hash = `${ hash } -${ hashMap [ hash ] } ` ;
426
+ break ;
427
+ }
428
+ }
429
+
430
+ return `[${ title } ](#${ hash } )` ;
407
431
}
408
432
409
433
private parseValidNumber ( value : string ) {
0 commit comments