@@ -26,13 +26,14 @@ import {
26
26
MdxMappableXmlNode ,
27
27
} from "./directMapXmlToMdx" ;
28
28
import { BlockContent } from "mdast" ;
29
+ import { BlockList } from "net" ;
29
30
30
31
// Only exported for testing
31
32
export const xmlParser = new XMLParser ( {
32
33
preserveOrder : true ,
33
34
alwaysCreateTextNode : true ,
34
35
trimValues : false ,
35
- attributeNamePrefix : "@" ,
36
+ ignoreAttributes : false ,
36
37
} ) ;
37
38
38
39
// === XML Types ===
@@ -61,29 +62,33 @@ type CompoundDef = XmlArray<
61
62
| { detaileddescription : MdxMappableXmlNode [ ] }
62
63
| { sectiondef : XmlArray < MemberDef > }
63
64
> ;
64
- type MemberDef =
65
- | { "@kind" : "function" }
66
- | { "@static" : "yes" | "no" }
67
- | { "@const" : "yes" | "no" }
68
- | { name : XmlTextNode [ ] }
69
- | { definition : XmlTextNode [ ] }
70
- | { argsstring : XmlTextNode [ ] }
71
- | { detaileddescription : XmlArray < DetailedDescription > } ;
65
+ type MemberDef = {
66
+ memberdef : XmlArray <
67
+ // TODO: Handle tag groups
68
+ // Or even use `isArray`
69
+ | { "@kind" : "function" }
70
+ | { "@static" : "yes" | "no" }
71
+ | { "@const" : "yes" | "no" }
72
+ | { name : XmlTextNode [ ] }
73
+ | { definition : XmlTextNode [ ] }
74
+ | { argsstring : XmlTextNode [ ] }
75
+ | { detaileddescription : XmlArray < DetailedDescription > }
76
+ > ;
77
+ } ;
72
78
type DetailedDescription =
73
79
| { para : MdxMappableXmlNode }
74
80
| { para : XmlArray < { simplesect : XmlArray < FunctionReturn > } > }
75
- | { para : XmlArray < FunctionParameters > } ;
81
+ | { para : XmlArray < FunctionParameters > }
82
+ | { sect1 : XmlArray < MdxMappableXmlNode > } ;
76
83
type FunctionReturn = { "@kind" : "return" } | MdxMappableXmlNode ;
77
84
type FunctionParameters = {
78
- parameterlist : XmlArray <
79
- | { parameterdescription : MdxMappableXmlNode }
80
- | {
81
- parameteritem : XmlArray < {
82
- parameternamelist : XmlArray < { parametername : XmlTextNode [ ] } > ;
83
- } > ;
84
- }
85
- > ;
85
+ parameterlist : XmlArray < {
86
+ parameteritem : XmlArray < FunctionParameterItem > ;
87
+ } > ;
86
88
} ;
89
+ type FunctionParameterItem =
90
+ | { parameterdescription : MdxMappableXmlNode }
91
+ | { parameternamelist : XmlArray < { parametername : XmlTextNode [ ] } > } ;
87
92
88
93
// === PageData Type ===
89
94
/**
@@ -93,6 +98,16 @@ type FunctionParameters = {
93
98
type PageData = {
94
99
title : string ;
95
100
description : BlockContent [ ] ;
101
+ functions : FunctionData [ ] ;
102
+ } ;
103
+
104
+ type FunctionData = {
105
+ name : string ;
106
+ signature : string ;
107
+ description : BlockContent [ ] ;
108
+ parameters : Array < { name : string ; description : BlockContent [ ] } > ;
109
+ returns : BlockContent [ ] ;
110
+ extendedDescription : BlockContent [ ] ;
96
111
} ;
97
112
98
113
/**
@@ -106,6 +121,9 @@ export function readXml(xml: string): PageData {
106
121
description : directMapXmlToMdx (
107
122
getChild ( compoundDef , "detaileddescription" ) as MdxMappableXmlNode [ ] ,
108
123
) ,
124
+ functions : ( getChild ( compoundDef , "sectiondef" ) as XmlArray < MemberDef > )
125
+ . filter ( ( tag ) : tag is MemberDef => "memberdef" in tag )
126
+ . map ( readFunction ) ,
109
127
} ;
110
128
return pageData ;
111
129
}
@@ -147,6 +165,79 @@ function getCompoundDef(document: DocumentRoot): CompoundDef {
147
165
return compoundDef ;
148
166
}
149
167
168
+ function readFunction ( memberDef : MemberDef ) : FunctionData {
169
+ const func = memberDef . memberdef ;
170
+ const detailedDescription = getChild (
171
+ func ,
172
+ "detaileddescription" ,
173
+ ) as XmlArray < DetailedDescription > ;
174
+ return {
175
+ name : extractText ( getChild ( func , "name" ) as XmlTextNode [ ] ) ,
176
+ signature :
177
+ extractText ( getChild ( func , "definition" ) as XmlTextNode [ ] ) +
178
+ extractText ( getChild ( func , "argsstring" ) as XmlTextNode [ ] ) ,
179
+ description : directMapXmlToMdx (
180
+ detailedDescription
181
+ . filter ( ( tag ) : tag is any => "para" in tag )
182
+ . filter (
183
+ ( tag ) : tag is MdxMappableXmlNode =>
184
+ ! (
185
+ getChild ( tag . para , "simplesect" ) ||
186
+ getChild ( tag . para , "parameterlist" )
187
+ ) ,
188
+ ) ,
189
+ ) ,
190
+ parameters : detailedDescription
191
+ . filter ( ( tag ) => "para" in tag )
192
+ . filter ( ( tag : any ) => ! ! getChild ( tag . para , "parameterlist" ) )
193
+ . map (
194
+ ( tag : any ) =>
195
+ getChild (
196
+ tag . para ,
197
+ "parameterlist" ,
198
+ ) as FunctionParameters [ "parameterlist" ] ,
199
+ )
200
+ . map ( ( tag : FunctionParameters [ "parameterlist" ] ) => {
201
+ const parameterItem = getChild (
202
+ tag ,
203
+ "parameteritem" ,
204
+ ) as XmlArray < FunctionParameterItem > ;
205
+ let name : string ;
206
+ try {
207
+ name = extractText (
208
+ getChild (
209
+ getChild ( parameterItem , "parameternamelist" ) as any ,
210
+ "parametername" ,
211
+ ) ,
212
+ ) ;
213
+ } catch {
214
+ throw new Error (
215
+ `Could not get parameter name: ${ JSON . stringify ( tag ) } ` ,
216
+ ) ;
217
+ }
218
+ const description = directMapXmlToMdx (
219
+ getChild (
220
+ parameterItem ,
221
+ "parameterdescription" ,
222
+ ) as MdxMappableXmlNode [ ] ,
223
+ ) ;
224
+ return { name, description } ;
225
+ } ) ,
226
+ returns : detailedDescription
227
+ . filter ( ( tag ) : tag is any => "para" in tag )
228
+ . filter ( ( tag : any ) => ! ! getChild ( tag . para , "simplesect" ) )
229
+ . map (
230
+ ( tag : any ) =>
231
+ getChild ( tag . para , "simplesect" ) as XmlArray < FunctionReturn > ,
232
+ )
233
+ // TODO: Check for @kind ="return"
234
+ // and remove `as MdxMappableXmlNode[]`
235
+ . flatMap ( ( tag ) => {
236
+ return directMapXmlToMdx ( tag as MdxMappableXmlNode [ ] ) ;
237
+ } ) ,
238
+ } ;
239
+ }
240
+
150
241
/**
151
242
* Extra whitespace in the XML can appear in the MDX output.
152
243
* This shouldn't affect how it renders on the site so we trim it to reduce noise.
0 commit comments