@@ -7,6 +7,13 @@ import { typescriptVersion, version } from '../../version';
7
7
import { getBuildTimestamp } from '../build/build-ctx' ;
8
8
import { AUTO_GENERATE_COMMENT } from './constants' ;
9
9
10
+ /**
11
+ * Generate metadata that will be used to generate any given documentation-related output target(s)
12
+ * @param config the configuration associated with the current Stencil task run
13
+ * @param compilerCtx the current compiler context
14
+ * @param buildCtx the build context for the current Stencil task run
15
+ * @returns the generated metadata
16
+ */
10
17
export const generateDocData = async (
11
18
config : d . ValidatedConfig ,
12
19
compilerCtx : d . CompilerCtx ,
@@ -23,6 +30,13 @@ export const generateDocData = async (
23
30
} ;
24
31
} ;
25
32
33
+ /**
34
+ * Derive the metadata for each Stencil component
35
+ * @param config the configuration associated with the current Stencil task run
36
+ * @param compilerCtx the current compiler context
37
+ * @param buildCtx the build context for the current Stencil task run
38
+ * @returns the derived metadata
39
+ */
26
40
const getDocsComponents = async (
27
41
config : d . ValidatedConfig ,
28
42
compilerCtx : d . CompilerCtx ,
@@ -37,8 +51,8 @@ const getDocsComponents = async (
37
51
const readme = await getUserReadmeContent ( compilerCtx , readmePath ) ;
38
52
const usage = await generateUsages ( compilerCtx , usagesDir ) ;
39
53
return moduleFile . cmps
40
- . filter ( ( cmp ) => ! cmp . internal && ! cmp . isCollectionDependency )
41
- . map ( ( cmp ) => ( {
54
+ . filter ( ( cmp : d . ComponentCompilerMeta ) => ! cmp . internal && ! cmp . isCollectionDependency )
55
+ . map ( ( cmp : d . ComponentCompilerMeta ) => ( {
42
56
dirPath,
43
57
filePath : relative ( config . rootDir , filePath ) ,
44
58
fileName : basename ( filePath ) ,
@@ -69,9 +83,12 @@ const getDocsComponents = async (
69
83
return sortBy ( flatOne ( results ) , ( cmp ) => cmp . tag ) ;
70
84
} ;
71
85
72
- const buildDocsDepGraph = ( cmp : d . ComponentCompilerMeta , cmps : d . ComponentCompilerMeta [ ] ) => {
86
+ const buildDocsDepGraph = (
87
+ cmp : d . ComponentCompilerMeta ,
88
+ cmps : d . ComponentCompilerMeta [ ]
89
+ ) : d . JsonDocsDependencyGraph => {
73
90
const dependencies : d . JsonDocsDependencyGraph = { } ;
74
- function walk ( tagName : string ) {
91
+ function walk ( tagName : string ) : void {
75
92
if ( ! dependencies [ tagName ] ) {
76
93
const cmp = cmps . find ( ( c ) => c . tagName === tagName ) ;
77
94
const deps = cmp . directDependencies ;
@@ -94,6 +111,11 @@ const buildDocsDepGraph = (cmp: d.ComponentCompilerMeta, cmps: d.ComponentCompil
94
111
return dependencies ;
95
112
} ;
96
113
114
+ /**
115
+ * Determines the encapsulation string to use, based on the provided compiler metadata
116
+ * @param cmp the metadata for a single component
117
+ * @returns the encapsulation level, expressed as a string
118
+ */
97
119
const getDocsEncapsulation = ( cmp : d . ComponentCompilerMeta ) : 'shadow' | 'scoped' | 'none' => {
98
120
if ( cmp . encapsulation === 'shadow' ) {
99
121
return 'shadow' ;
@@ -251,7 +273,13 @@ const getDocsListeners = (listeners: d.ComponentCompilerListener[]): d.JsonDocsL
251
273
} ) ) ;
252
274
} ;
253
275
254
- const getDocsDeprecationText = ( tags : d . JsonDocsTag [ ] ) => {
276
+ /**
277
+ * Get the text associated with a `@deprecated` tag, if one exists
278
+ * @param tags the tags associated with a JSDoc block on a node in the AST
279
+ * @returns the text associated with the first found `@deprecated` tag. If a `@deprecated` tag exists but does not
280
+ * have associated text, an empty string is returned. If no such tag is found, return `undefined`
281
+ */
282
+ const getDocsDeprecationText = ( tags : d . JsonDocsTag [ ] ) : string | undefined => {
255
283
const deprecation = tags . find ( ( t ) => t . name === 'deprecated' ) ;
256
284
if ( deprecation ) {
257
285
return deprecation . text || '' ;
@@ -284,9 +312,20 @@ export const getNameText = (name: string, tags: d.JsonDocsTag[]) => {
284
312
} ) ;
285
313
} ;
286
314
287
- const getUserReadmeContent = async ( compilerCtx : d . CompilerCtx , readmePath : string ) => {
315
+ /**
316
+ * Attempts to read a pre-existing README.md file from disk, returning any content generated by the user.
317
+ *
318
+ * For simplicity's sake, it is assumed that all user-generated content will fall before {@link AUTO_GENERATE_COMMENT}
319
+ *
320
+ * @param compilerCtx the current compiler context
321
+ * @param readmePath the path to the README file to read
322
+ * @returns the user generated content that occurs before {@link AUTO_GENERATE_COMMENT}. If no user generated content
323
+ * exists, or if there was an issue reading the file, return `undefined`
324
+ */
325
+ const getUserReadmeContent = async ( compilerCtx : d . CompilerCtx , readmePath : string ) : Promise < string | undefined > => {
288
326
try {
289
327
const existingContent = await compilerCtx . fs . readFile ( readmePath ) ;
328
+ // subtract one to get everything up to, but not including the auto generated comment
290
329
const userContentIndex = existingContent . indexOf ( AUTO_GENERATE_COMMENT ) - 1 ;
291
330
if ( userContentIndex >= 0 ) {
292
331
return existingContent . substring ( 0 , userContentIndex ) ;
@@ -295,31 +334,63 @@ const getUserReadmeContent = async (compilerCtx: d.CompilerCtx, readmePath: stri
295
334
return undefined ;
296
335
} ;
297
336
298
- const generateDocs = ( readme : string , jsdoc : d . CompilerJsDoc ) => {
337
+ /**
338
+ * Generate documentation for a given component based on the provided JSDoc and README contents
339
+ * @param readme the contents of a component's README file, without any autogenerated contents
340
+ * @param jsdoc the JSDoc associated with the component's declaration
341
+ * @returns the generated documentation
342
+ */
343
+ const generateDocs = ( readme : string , jsdoc : d . CompilerJsDoc ) : string => {
299
344
const docs = jsdoc . text ;
300
345
if ( docs !== '' || ! readme ) {
346
+ // just return the existing docs if they exist. these would have been captured earlier in the compilation process.
347
+ // if they don't exist, and there's no README to process, return an empty string.
301
348
return docs ;
302
349
}
303
350
351
+ /**
352
+ * Parse the README, storing the first section of content.
353
+ * Content is defined as the area between two non-consecutive lines that start with a '#':
354
+ * ```
355
+ * # Header 1
356
+ * This is some content
357
+ * # Header 2
358
+ * This is more content
359
+ * # Header 3
360
+ * Again, content
361
+ * ```
362
+ * In the example above, this chunk of code is designed to capture "This is some content"
363
+ */
304
364
let isContent = false ;
305
365
const lines = readme . split ( '\n' ) ;
306
366
const contentLines = [ ] ;
307
367
for ( const line of lines ) {
308
368
const isHeader = line . startsWith ( '#' ) ;
309
369
if ( isHeader && isContent ) {
370
+ // we were actively parsing content, but found a new header, break out
310
371
break ;
311
372
}
312
373
if ( ! isHeader && ! isContent ) {
374
+ // we've found content for the first time, set this sentinel to `true`
313
375
isContent = true ;
314
376
}
315
377
if ( isContent ) {
378
+ // we're actively parsing the first found block of content, add it to our list for later
316
379
contentLines . push ( line ) ;
317
380
}
318
381
}
319
382
return contentLines . join ( '\n' ) . trim ( ) ;
320
383
} ;
321
384
322
- const generateUsages = async ( compilerCtx : d . CompilerCtx , usagesDir : string ) => {
385
+ /**
386
+ * This function is responsible for reading the contents of all markdown files in a provided `usage` directory and
387
+ * returning their contents
388
+ * @param compilerCtx the current compiler context
389
+ * @param usagesDir the directory to read usage markdown files from
390
+ * @returns an object that maps the filename containing the usage example, to the file's contents. If an error occurs,
391
+ * an empty object is returned.
392
+ */
393
+ const generateUsages = async ( compilerCtx : d . CompilerCtx , usagesDir : string ) : Promise < d . JsonDocsUsage > => {
323
394
const rtn : d . JsonDocsUsage = { } ;
324
395
325
396
try {
0 commit comments