@@ -13,16 +13,24 @@ const createNodeSlugger = () => {
1313 const slugger = new GitHubSlugger ( ) ;
1414 const slugFn = slugger . slug . bind ( slugger ) ;
1515
16+ // Legacy slug counter tracking (mirrors old Node.js deduplication)
17+ const legacyIdCounters = { __proto__ : null } ;
18+
19+ /** Deduplicates legacy slugs by appending an incremented counter */
20+ const legacyDeduplicateFn = id => {
21+ if ( legacyIdCounters [ id ] !== undefined ) {
22+ return `${ id } _${ ++ legacyIdCounters [ id ] } ` ;
23+ }
24+ legacyIdCounters [ id ] = 0 ;
25+ return id ;
26+ } ;
27+
1628 return {
1729 ...slugger ,
18- /**
19- * Creates a new slug based on the provided string
20- *
21- * @param {string } title The title to be parsed into a slug
22- */
23- // Applies certain string replacements that are specific
24- // to the way how Node.js generates slugs/anchor IDs
30+ /** Creates a new slug with Node.js-specific replacements applied */
2531 slug : title => slug ( title , slugFn ) ,
32+ /** Creates a legacy-style slug to preserve old anchor links */
33+ legacySlug : ( title , api ) => getLegacySlug ( title , api , legacyDeduplicateFn ) ,
2634 } ;
2735} ;
2836
@@ -36,4 +44,29 @@ export const slug = (title, slugFn = defaultSlugFn) =>
3644 slugFn ( title )
3745 ) ;
3846
47+ // Legacy slug algorithm regex patterns (from old Node.js doc generator)
48+ const notAlphaNumerics = / [ ^ a - z 0 - 9 ] + / g;
49+ const edgeUnderscores = / ^ _ + | _ + $ / g;
50+ const notAlphaStart = / ^ [ ^ a - z ] / ;
51+
52+ /**
53+ * Generates a legacy-style slug to preserve old anchor links.
54+ * The old Node.js doc generator used this format: filename_headingtext
55+ * with non-alphanumerics replaced by underscores.
56+ *
57+ * @param {string } title The heading text
58+ * @param {string } api The API file identifier (e.g. 'fs', 'http')
59+ * @param {(id: string) => string } [deduplicateFn] Optional function for counter-based deduplication
60+ * @returns {string } The legacy slug
61+ */
62+ export const getLegacySlug = ( title , api , deduplicateFn ) => {
63+ let id = `${ api } _${ title } `
64+ . toLowerCase ( )
65+ . replace ( notAlphaNumerics , '_' )
66+ . replace ( edgeUnderscores , '' )
67+ . replace ( notAlphaStart , '_$&' ) ;
68+
69+ return deduplicateFn ? deduplicateFn ( id ) : id ;
70+ } ;
71+
3972export default createNodeSlugger ;
0 commit comments