@@ -71,26 +71,24 @@ function createSection(entry, head) {
7171 } ;
7272}
7373
74+ /**
75+ *
76+ * @param {String } string
77+ * @returns {String }
78+ */
79+ function transformTypeReferences ( string ) {
80+ // console.log(string)
81+ return string . replaceAll ( / ` < ( [ ^ > ] + ) > ` / g, '{$1}' ) . replaceAll ( '} | {' , '|' ) ;
82+ }
83+
7484/**
7585 * Parses a list item to extract properties.
7686 * @param {import('mdast').ListItem } child - The list item node.
77- * @param {import('../types.d.ts').HierarchizedEntry } entry - The entry containing raw content.
7887 * @returns {import('../types.d.ts').List } The parsed list.
7988 */
80- function parseListItem ( child , entry ) {
89+ function parseListItem ( child ) {
8190 const current = { } ;
8291
83- /**
84- * Extracts raw content from a node based on its position.
85- * @param {import('mdast').BlockContent } node
86- * @returns {string }
87- */
88- const getRawContent = node =>
89- entry . rawContent . slice (
90- node . position . start . offset ,
91- node . position . end . offset
92- ) ;
93-
9492 /**
9593 * Extracts a pattern from text and assigns it to the current object.
9694 * @param {string } text
@@ -108,12 +106,14 @@ function parseListItem(child, entry) {
108106 } ;
109107
110108 // Combine and clean text from child nodes, excluding nested lists
111- current . textRaw = child . children
112- . filter ( node => node . type !== 'list' )
113- . map ( getRawContent )
114- . join ( '' )
115- . replace ( / \s + / g, ' ' )
116- . replace ( / < ! - - .* ?- - > / gs, '' ) ;
109+ current . textRaw = transformTypeReferences (
110+ transformNodesToString (
111+ child . children . filter ( node => node . type !== 'list' ) ,
112+ true
113+ )
114+ . replace ( / \s + / g, ' ' )
115+ . replace ( / < ! - - .* ?- - > / gs, '' )
116+ ) ;
117117
118118 let text = current . textRaw ;
119119
@@ -136,9 +136,7 @@ function parseListItem(child, entry) {
136136 const optionsNode = child . children . find ( child => child . type === 'list' ) ;
137137
138138 if ( optionsNode ) {
139- current . options = optionsNode . children . map ( child =>
140- parseListItem ( child , entry )
141- ) ;
139+ current . options = optionsNode . children . map ( parseListItem ) ;
142140 }
143141
144142 return current ;
@@ -148,38 +146,26 @@ function parseListItem(child, entry) {
148146 * Parses stability metadata and adds it to the section.
149147 * @param {import('../types.d.ts').Section } section - The section to add stability to.
150148 * @param {Array } nodes - The AST nodes.
149+ * @param {import('../types.d.ts').HierarchizedEntry } entry - The entry to handle.
151150 */
152- function parseStability ( section , nodes ) {
153- nodes . forEach ( ( node , i ) => {
154- if (
155- node . type === 'blockquote' &&
156- node . children . length === 1 &&
157- node . children [ 0 ] . type === 'paragraph' &&
158- nodes . slice ( 0 , i ) . every ( n => n . type === 'list' )
159- ) {
160- const text = transformNodesToString ( node . children [ 0 ] . children ) ;
161- const stabilityMatch = / ^ S t a b i l i t y : ( [ 0 - 5 ] ) (?: \s * - \s * ) ? ( .* ) $ / s. exec ( text ) ;
162- if ( stabilityMatch ) {
163- section . stability = Number ( stabilityMatch [ 1 ] ) ;
164- section . stabilityText = stabilityMatch [ 2 ] . replace ( / \n / g, ' ' ) . trim ( ) ;
165- nodes . splice ( i , 1 ) ; // Remove the matched stability node to prevent further processing
166- }
167- }
168- } ) ;
151+ function parseStability ( section , nodes , entry ) {
152+ const json = entry . stability . toJSON ( ) [ 0 ] ;
153+ if ( json ) {
154+ section . stability = json . index ;
155+ section . stabilityText = json . description ;
156+ nodes . splice ( 0 , 1 ) ;
157+ }
169158}
170159
171160/**
172161 * Parses a list and updates the section accordingly.
173162 * @param {import('../types.d.ts').Section } section - The section to update.
174163 * @param {Array } nodes - The AST nodes.
175- * @param {import('../types.d.ts').HierarchizedEntry } entry - The associated entry.
176164 */
177- function parseList ( section , nodes , entry ) {
165+ function parseList ( section , nodes ) {
178166 const list = nodes [ 0 ] ?. type === 'list' ? nodes . shift ( ) : null ;
179167
180- const values = list
181- ? list . children . map ( child => parseListItem ( child , entry ) )
182- : [ ] ;
168+ const values = list ? list . children . map ( parseListItem ) : [ ] ;
183169
184170 switch ( section . type ) {
185171 case 'ctor' :
@@ -296,8 +282,8 @@ function handleEntry(entry, parentSection) {
296282 const [ headingNode , ...nodes ] = structuredClone ( entry . content . children ) ;
297283 const section = createSection ( entry , headingNode ) ;
298284
299- parseStability ( section , nodes ) ;
300- parseList ( section , nodes , entry ) ;
285+ parseStability ( section , nodes , entry ) ;
286+ parseList ( section , nodes ) ;
301287 addDescription ( section , nodes ) ;
302288 handleChildren ( entry , section ) ;
303289 addAdditionalMetadata ( section , parentSection , headingNode ) ;
0 commit comments