1- import {
2- DOC_MAN_BASE_URL ,
3- DOC_API_HEADING_TYPES ,
4- TYPE_GENERIC_REGEX ,
5- } from '../constants.mjs' ;
1+ import { DOC_MAN_BASE_URL , DOC_API_HEADING_TYPES } from '../constants.mjs' ;
62import { slug } from './slugger.mjs' ;
3+ import { parseType } from './typeParser.mjs' ;
74import { transformNodesToString } from '../../../utils/unist.mjs' ;
85import BUILTIN_TYPE_MAP from '../maps/builtin.json' with { type : 'json' } ;
96import MDN_TYPE_MAP from '../maps/mdn.json' with { type : 'json' } ;
@@ -22,84 +19,7 @@ export const transformUnixManualToLink = (
2219) => {
2320 return `[\`${ text } \`](${ DOC_MAN_BASE_URL } ${ sectionNumber } /${ command } .${ sectionNumber } ${ sectionLetter } .html)` ;
2421} ;
25- /**
26- * Safely splits the string by `|` or `&` at the top level (ignoring those
27- * inside `< >`), and returns both the pieces and the separator used.
28- *
29- * @param {string } str The type string to split
30- * @returns {{ pieces: string[], separator: string } } The split pieces and the separator string used to join them (` | ` or ` & `)
31- */
32- const splitByOuterSeparator = str => {
33- const pieces = [ ] ;
34- let current = '' ;
35- let depth = 0 ;
36- let separator ;
37-
38- for ( const char of str ) {
39- if ( char === '<' ) {
40- depth ++ ;
41- } else if ( char === '>' ) {
42- depth -- ;
43- } else if ( ( char === '|' || char === '&' ) && depth === 0 ) {
44- pieces . push ( current ) ;
45- current = '' ;
46- separator ??= ` ${ char } ` ;
47- continue ;
48- }
49- current += char ;
50- }
51-
52- pieces . push ( current ) ;
53- return { pieces, separator } ;
54- } ;
55-
56- /**
57- * Attempts to parse and format a basic Generic type (e.g., Promise<string>).
58- * It also supports union and multi-parameter types within the generic brackets.
59- *
60- * @param {string } typePiece The plain type piece to be evaluated
61- * @param {Function } transformType The function used to resolve individual types into links
62- * @returns {string|null } The formatted Markdown link, or null if no match is found
63- */
64- const formatBasicGeneric = ( typePiece , transformType ) => {
65- const genericMatch = typePiece . match ( TYPE_GENERIC_REGEX ) ;
66-
67- if ( genericMatch ) {
68- const baseType = genericMatch [ 1 ] . trim ( ) ;
69- const innerType = genericMatch [ 2 ] . trim ( ) ;
70-
71- const baseResult = transformType ( baseType . replace ( / \[ \] $ / , '' ) ) ;
72- const baseFormatted = baseResult
73- ? `[\`<${ baseType } >\`](${ baseResult } )`
74- : `\`<${ baseType } >\`` ;
7522
76- // Split while capturing delimiters (| or ,) to preserve original syntax
77- const parts = innerType . split ( / ( [ | , ] ) / ) ;
78-
79- const innerFormatted = parts
80- . map ( part => {
81- const trimmed = part . trim ( ) ;
82- // If it is a delimiter, return it as is
83- if ( trimmed === '|' ) {
84- return ' | ' ;
85- }
86-
87- if ( trimmed === ',' ) {
88- return ', ' ;
89- }
90-
91- const innerRes = transformType ( trimmed . replace ( / \[ \] $ / , '' ) ) ;
92- return innerRes
93- ? `[\`<${ trimmed } >\`](${ innerRes } )`
94- : `\`<${ trimmed } >\`` ;
95- } )
96- . join ( '' ) ;
97-
98- return `${ baseFormatted } <${ innerFormatted } >` ;
99- }
100-
101- return null ;
102- } ;
10323/**
10424 * This method replaces plain text Types within the Markdown content into Markdown links
10525 * that link to the actual relevant reference for such type (either internal or external link)
@@ -111,7 +31,10 @@ const formatBasicGeneric = (typePiece, transformType) => {
11131export const transformTypeToReferenceLink = ( type , record ) => {
11232 // Removes the wrapping curly braces that wrap the type references
11333 // We keep the angle brackets `<>` intact here to parse Generics later
114- const typeInput = type . replace ( / [ { } ] / g, '' ) ;
34+ const typeInput = type
35+ . trim ( )
36+ . replace ( / ^ \{ ( .* ) \} $ / , '$1' )
37+ . trim ( ) ;
11538
11639 /**
11740 * Handles the mapping (if there's a match) of the input text
@@ -150,32 +73,7 @@ export const transformTypeToReferenceLink = (type, record) => {
15073 return '' ;
15174 } ;
15275
153- const { pieces : outerPieces , separator } = splitByOuterSeparator ( typeInput ) ;
154-
155- const typePieces = outerPieces . map ( piece => {
156- // This is the content to render as the text of the Markdown link
157- const trimmedPiece = piece . trim ( ) ;
158-
159- // 1. Attempt to format as a basic Generic type first
160- const genericMarkdown = formatBasicGeneric ( trimmedPiece , transformType ) ;
161- if ( genericMarkdown ) {
162- return genericMarkdown ;
163- }
164-
165- // 2. Fallback to the logic for plain types
166- // This is what we will compare against the API types mappings
167- // The ReGeX below is used to remove `[]` from the end of the type
168- const result = transformType ( trimmedPiece . replace ( / \[ \] $ / , '' ) ) ;
169-
170- // If we have a valid result and the piece is not empty, we return the Markdown link
171- if ( trimmedPiece . length && result . length ) {
172- return `[\`<${ trimmedPiece } >\`](${ result } )` ;
173- }
174- } ) ;
175-
176- // Filter out pieces that we failed to map and then join the valid ones
177- // using the same separator that appeared in the original type string
178- const markdownLinks = typePieces . filter ( Boolean ) . join ( separator ) ;
76+ const markdownLinks = parseType ( typeInput , transformType ) ;
17977
18078 // Return the replaced links or the original content if they all failed to be replaced
18179 // Note that if some failed to get replaced, only the valid ones will be returned
0 commit comments