@@ -23,25 +23,43 @@ import {
23
23
headingsAndContent ,
24
24
findConstructorHeader ,
25
25
consumeTypedKeysList ,
26
- findProcess ,
27
26
} from './markdown-helpers' ;
28
- import { WEBSITE_BASE_DOCS_URL , REPO_BASE_DOCS_URL } from './constants' ;
29
27
import { extendError } from './helpers' ;
30
28
import {
31
29
parseMethodBlocks ,
32
30
_headingToMethodBlock ,
33
31
parsePropertyBlocks ,
34
32
parseEventBlocks ,
35
33
} from './block-parsers' ;
34
+ import { DocsParserPlugin } from './DocsParserPlugin' ;
36
35
37
36
export class DocsParser {
38
37
constructor (
39
- private baseElectronDir : string ,
38
+ private baseDir : string ,
40
39
private moduleVersion : string ,
41
40
private apiFiles : string [ ] ,
42
41
private structureFiles : string [ ] ,
42
+ private plugins : DocsParserPlugin < any > [ ] = [ ] ,
43
43
) { }
44
44
45
+ private getRelatveDocsPath = ( filePath : string ) =>
46
+ path . relative ( this . baseDir , filePath ) . split ( '.' ) [ 0 ] ;
47
+
48
+ private extendAPI = <
49
+ T extends
50
+ | ModuleDocumentationContainer
51
+ | ClassDocumentationContainer
52
+ | ElementDocumentationContainer
53
+ > (
54
+ api : T ,
55
+ tokens : Token [ ] ,
56
+ ) : T => {
57
+ for ( const plugin of this . plugins ) {
58
+ if ( plugin . extendAPI ) Object . assign ( api , plugin . extendAPI ( api , tokens ) || { } ) ;
59
+ }
60
+ return api ;
61
+ } ;
62
+
45
63
private async parseBaseContainers (
46
64
filePath : string ,
47
65
fileContents : string ,
@@ -53,7 +71,7 @@ export class DocsParser {
53
71
isClass : boolean ;
54
72
} [ ]
55
73
> {
56
- const relativeDocsPath = path . relative ( this . baseElectronDir , filePath ) . split ( '.' ) [ 0 ] ;
74
+ const relativeDocsPath = this . getRelatveDocsPath ( filePath ) ;
57
75
const isStructure = relativeDocsPath . includes ( 'structures' ) ;
58
76
const headings = headingsAndContent ( tokens ) ;
59
77
expect ( headings ) . to . not . have . lengthOf (
@@ -105,11 +123,19 @@ export class DocsParser {
105
123
extends : extendsMatch ? extendsMatch [ 1 ] : undefined ,
106
124
description,
107
125
slug : path . basename ( filePath , '.md' ) ,
108
- websiteUrl : `${ WEBSITE_BASE_DOCS_URL } /${ relativeDocsPath } ` ,
109
- repoUrl : `${ REPO_BASE_DOCS_URL ( this . moduleVersion ) } /${ relativeDocsPath } .md` ,
110
126
version : this . moduleVersion ,
111
127
} ,
112
128
} ) ;
129
+ const added = parsedContainers [ parsedContainers . length - 1 ] ;
130
+ for ( const plugin of this . plugins )
131
+ Object . assign (
132
+ added . container ,
133
+ plugin . extendContainer
134
+ ? plugin . extendContainer ( added . container , {
135
+ relativeDocsPath,
136
+ } )
137
+ : { } ,
138
+ ) ;
113
139
}
114
140
}
115
141
@@ -147,7 +173,6 @@ export class DocsParser {
147
173
'HTMLElement documentation should not be considered a class' ,
148
174
) ;
149
175
}
150
- const electronProcess = findProcess ( tokens ) ;
151
176
if ( isClass ) {
152
177
// Instance name will be taken either from an example in a method declaration or the camel
153
178
// case version of the class name
@@ -161,60 +186,78 @@ export class DocsParser {
161
186
const constructorMethod = _headingToMethodBlock ( findConstructorHeader ( tokens ) ) ;
162
187
163
188
// This is a class
164
- parsed . push ( {
165
- ...container ,
166
- type : 'Class' ,
167
- process : electronProcess ,
168
- constructorMethod : constructorMethod
169
- ? {
170
- signature : constructorMethod . signature ,
171
- parameters : constructorMethod . parameters ,
172
- }
173
- : null ,
174
- // ### Static Methods
175
- staticMethods : parseMethodBlocks ( findContentInsideHeader ( tokens , 'Static Methods' , 3 ) ) ,
176
- // ### Static Properties
177
- staticProperties : parsePropertyBlocks (
178
- findContentInsideHeader ( tokens , 'Static Properties' , 3 ) ,
179
- ) ,
180
- // ### Instance Methods
181
- instanceMethods : parseMethodBlocks (
182
- findContentInsideHeader ( tokens , 'Instance Methods' , 3 ) ,
189
+ parsed . push (
190
+ this . extendAPI (
191
+ {
192
+ ...container ,
193
+ type : 'Class' ,
194
+ constructorMethod : constructorMethod
195
+ ? {
196
+ signature : constructorMethod . signature ,
197
+ parameters : constructorMethod . parameters ,
198
+ }
199
+ : null ,
200
+ // ### Static Methods
201
+ staticMethods : parseMethodBlocks (
202
+ findContentInsideHeader ( tokens , 'Static Methods' , 3 ) ,
203
+ ) ,
204
+ // ### Static Properties
205
+ staticProperties : parsePropertyBlocks (
206
+ findContentInsideHeader ( tokens , 'Static Properties' , 3 ) ,
207
+ ) ,
208
+ // ### Instance Methods
209
+ instanceMethods : parseMethodBlocks (
210
+ findContentInsideHeader ( tokens , 'Instance Methods' , 3 ) ,
211
+ ) ,
212
+ // ### Instance Properties
213
+ instanceProperties : parsePropertyBlocks (
214
+ findContentInsideHeader ( tokens , 'Instance Properties' , 3 ) ,
215
+ ) ,
216
+ // ### Instance Events
217
+ instanceEvents : parseEventBlocks (
218
+ findContentInsideHeader ( tokens , 'Instance Events' , 3 ) ,
219
+ ) ,
220
+ instanceName,
221
+ } ,
222
+ tokens ,
183
223
) ,
184
- // ### Instance Properties
185
- instanceProperties : parsePropertyBlocks (
186
- findContentInsideHeader ( tokens , 'Instance Properties' , 3 ) ,
187
- ) ,
188
- // ### Instance Events
189
- instanceEvents : parseEventBlocks ( findContentInsideHeader ( tokens , 'Instance Events' , 3 ) ) ,
190
- instanceName,
191
- } ) ;
224
+ ) ;
192
225
} else {
193
226
// This is a module
194
227
if ( isElement ) {
195
- parsed . push ( {
196
- ...container ,
197
- type : 'Element' ,
198
- process : electronProcess ,
199
- // ## Methods
200
- methods : parseMethodBlocks ( findContentInsideHeader ( tokens , 'Methods' , 2 ) ) ,
201
- // ## Properties
202
- properties : parsePropertyBlocks ( findContentInsideHeader ( tokens , 'Tag Attributes' , 2 ) ) ,
203
- // ## Events
204
- events : parseEventBlocks ( findContentInsideHeader ( tokens , 'DOM Events' , 2 ) ) ,
205
- } ) ;
228
+ parsed . push (
229
+ this . extendAPI (
230
+ {
231
+ ...container ,
232
+ type : 'Element' ,
233
+ // ## Methods
234
+ methods : parseMethodBlocks ( findContentInsideHeader ( tokens , 'Methods' , 2 ) ) ,
235
+ // ## Properties
236
+ properties : parsePropertyBlocks (
237
+ findContentInsideHeader ( tokens , 'Tag Attributes' , 2 ) ,
238
+ ) ,
239
+ // ## Events
240
+ events : parseEventBlocks ( findContentInsideHeader ( tokens , 'DOM Events' , 2 ) ) ,
241
+ } ,
242
+ tokens ,
243
+ ) ,
244
+ ) ;
206
245
} else {
207
- parsed . push ( {
208
- ...container ,
209
- type : 'Module' ,
210
- process : electronProcess ,
211
- // ## Methods
212
- methods : parseMethodBlocks ( findContentInsideHeader ( tokens , 'Methods' , 2 ) ) ,
213
- // ## Properties
214
- properties : parsePropertyBlocks ( findContentInsideHeader ( tokens , 'Properties' , 2 ) ) ,
215
- // ## Events
216
- events : parseEventBlocks ( findContentInsideHeader ( tokens , 'Events' , 2 ) ) ,
217
- } ) ;
246
+ parsed . push (
247
+ this . extendAPI (
248
+ {
249
+ ...container ,
250
+ type : 'Module' ,
251
+ // ## Methods
252
+ methods : parseMethodBlocks ( findContentInsideHeader ( tokens , 'Methods' , 2 ) ) ,
253
+ // ## Properties
254
+ properties : parsePropertyBlocks ( findContentInsideHeader ( tokens , 'Properties' , 2 ) ) ,
255
+ // ## Events
256
+ events : parseEventBlocks ( findContentInsideHeader ( tokens , 'Events' , 2 ) ) ,
257
+ } ,
258
+ tokens ,
259
+ ) ,
260
+ ) ;
218
261
}
219
262
}
220
263
}
0 commit comments