@@ -381,6 +381,7 @@ const DEPRECATION_HEADING_PATTERN = /^DEP\d+:/;
381
381
export function buildToc ( { filename, apilinks } ) {
382
382
return ( tree , file ) => {
383
383
const idCounters = Object . create ( null ) ;
384
+ const legacyIdCounters = Object . create ( null ) ;
384
385
let toc = '' ;
385
386
let depth = 0 ;
386
387
@@ -399,6 +400,8 @@ export function buildToc({ filename, apilinks }) {
399
400
node . children [ 0 ] . position . start . offset ,
400
401
node . position . end . offset ) . trim ( ) ;
401
402
const id = getId ( headingText , idCounters ) ;
403
+ // Use previous ID generator to create alias
404
+ const legacyId = getLegacyId ( `${ realFilename } _${ headingText } ` , legacyIdCounters ) ;
402
405
403
406
const isDeprecationHeading =
404
407
DEPRECATION_HEADING_PATTERN . test ( headingText ) ;
@@ -417,6 +420,9 @@ export function buildToc({ filename, apilinks }) {
417
420
let anchor =
418
421
`<span><a class="mark" href="#${ id } " id="${ id } ">#</a></span>` ;
419
422
423
+ // Add alias anchor to preserve old links
424
+ anchor += `<a class="legacy" id="${ legacyId } "></a>` ;
425
+
420
426
if ( realFilename === 'errors' && headingText . startsWith ( 'ERR_' ) ) {
421
427
anchor +=
422
428
`<span><a class="mark" href="#${ headingText } " id="${ headingText } ">#</a></span>` ;
@@ -446,6 +452,7 @@ export function buildToc({ filename, apilinks }) {
446
452
} ;
447
453
}
448
454
455
+ // ID generator that mirrors Github's heading anchor parser
449
456
const punctuation = / [ ^ \w \- ] / g;
450
457
function getId ( text , idCounters ) {
451
458
text = text . toLowerCase ( )
@@ -458,6 +465,23 @@ function getId(text, idCounters) {
458
465
return text ;
459
466
}
460
467
468
+ // This ID generator is purely to generate aliases
469
+ // so we can preserve old doc links
470
+ const notAlphaNumerics = / [ ^ a - z 0 - 9 ] + / g;
471
+ const edgeUnderscores = / ^ _ + | _ + $ / g;
472
+ const notAlphaStart = / ^ [ ^ a - z ] / ;
473
+ function getLegacyId ( text , idCounters ) {
474
+ text = text . toLowerCase ( )
475
+ . replace ( notAlphaNumerics , '_' )
476
+ . replace ( edgeUnderscores , '' )
477
+ . replace ( notAlphaStart , '_$&' ) ;
478
+ if ( idCounters [ text ] !== undefined ) {
479
+ return `${ text } _${ ++ idCounters [ text ] } ` ;
480
+ }
481
+ idCounters [ text ] = 0 ;
482
+ return text ;
483
+ }
484
+
461
485
function altDocs ( filename , docCreated , versions ) {
462
486
const [ , docCreatedMajor , docCreatedMinor ] = docCreated . map ( Number ) ;
463
487
const host = 'https://nodejs.org' ;
0 commit comments