@@ -37,6 +37,30 @@ export const extractPattern = (text, pattern, key, current) => {
37
37
return text . replace ( pattern , '' ) ;
38
38
} ;
39
39
40
+ /**
41
+ * Determines if the input List node is a typed list
42
+ * @param {import('@types/mdast').List } list
43
+ */
44
+ export const isTypedList = list => {
45
+ if ( list . type !== 'list' ) {
46
+ // Exit early if not a list
47
+ return false ;
48
+ }
49
+
50
+ const children = list ?. children ?. [ 0 ] ?. children ?. [ 0 ] ?. children ;
51
+
52
+ return (
53
+ // The first element must be a code block
54
+ children ?. [ 0 ] ?. type === 'inlineCode' &&
55
+ // Followed by a space
56
+ children ?. [ 1 ] ?. value . trim ( ) === '' &&
57
+ // Followed by a link (type)
58
+ children ?. [ 2 ] ?. type === 'link' &&
59
+ // Types start with `<`
60
+ children ?. [ 2 ] ?. children ?. [ 0 ] ?. value ?. [ 0 ] === '<'
61
+ ) ;
62
+ } ;
63
+
40
64
/**
41
65
* Parses an individual list item node to extract its properties
42
66
*
@@ -46,9 +70,11 @@ export const extractPattern = (text, pattern, key, current) => {
46
70
export function parseListItem ( child ) {
47
71
const current = { } ;
48
72
73
+ const subList = child . children . find ( isTypedList ) ;
74
+
49
75
// Extract and clean raw text from the node, excluding nested lists
50
76
current . textRaw = transformTypeReferences (
51
- transformNodesToString ( child . children . filter ( node => node . type !== 'list' ) )
77
+ transformNodesToString ( child . children . filter ( node => node !== subList ) )
52
78
. replace ( / \s + / g, ' ' )
53
79
. replace ( / < ! - - .* ?- - > / gs, '' )
54
80
) ;
@@ -70,9 +96,8 @@ export function parseListItem(child) {
70
96
current . desc = text . replace ( LEADING_HYPHEN , '' ) . trim ( ) || undefined ;
71
97
72
98
// Parse nested lists (options) recursively if present
73
- const optionsNode = child . children . find ( node => node . type === 'list' ) ;
74
- if ( optionsNode ) {
75
- current . options = optionsNode . children . map ( parseListItem ) ;
99
+ if ( subList ) {
100
+ current . options = subList . children . map ( parseListItem ) ;
76
101
}
77
102
78
103
return current ;
0 commit comments