@@ -2,15 +2,15 @@ const fs = require('fs-extra');
2
2
const inline = require ( 'jsdoc/tag/inline' ) ;
3
3
const inflection = require ( 'inflection' ) ;
4
4
5
+ let typeDefs = [ ] ;
5
6
let knownClassNames = [ ] ;
6
7
8
+
7
9
const anchorName = ( link ) => inflection . dasherize ( inflection . underscore ( link . replace ( / # / g, '-' ) ) ) ;
8
10
9
- const resolveInlineLinks = str => {
10
- return inline . replaceInlineTags ( str , {
11
- link : ( string , { completeTag, text, tag } ) => string . replace ( completeTag , `[${ text } ](#${ anchorName ( text ) } )` )
12
- } ) . newString ;
13
- } ;
11
+ const resolveInlineLinks = str => inline . replaceInlineTags ( str , {
12
+ link : ( string , { completeTag, text } ) => string . replace ( completeTag , `[${ text } ](#${ anchorName ( text ) } )` )
13
+ } ) . newString ;
14
14
15
15
const renderLinks = ( p ) => {
16
16
if ( p . type . names [ 0 ] === '*' ) {
@@ -28,22 +28,62 @@ const renderLinks = (p) => {
28
28
function generateParams ( doclet ) {
29
29
const params = doclet . params . map (
30
30
p => {
31
- const optional = p . optional ? ` **Optional**` : null ;
31
+ const optional = p . optional ? ' **Optional**' : null ;
32
32
const defaultValue = p . defaultvalue ? `**Default:** \`${ p . defaultvalue } \`` : null ;
33
33
const options = [ optional , defaultValue ] . filter ( f => ! ! f ) ;
34
+
35
+ const type = p . type && p . type . parsedType . name ;
36
+ if ( ! p . description && typeDefs . find ( ( td ) => td . name === type ) ) {
37
+ const [ , parent ] = doclet . memberof . split ( '~' ) ;
38
+ p . description = `See {@link ${ parent } #${ type } }` ;
39
+ }
40
+
34
41
return `- \`${ p . name } \`${ options . length ? ` (${ options . join ( ', ' ) } )` : '' } ${ p . description ? ` - ${ resolveInlineLinks ( p . description ) } ` : '' } ` ;
35
42
}
36
43
) ;
37
44
return `**Parameters:**\n\n${ params . join ( '\n' ) } \n` ;
38
45
}
39
46
47
+ function generateTypeDefs ( doclets ) {
48
+ let markDown = '' ;
49
+ let codeBlock = [ ] ;
50
+ let properties = [ ] ;
51
+
52
+ if ( ! doclets . length ) {
53
+ return '' ;
54
+ }
55
+
56
+ doclets . forEach ( ( doclet ) => {
57
+ markDown += [ `**${ doclet . name } **` , doclet . description ] . filter ( ( d ) => ! ! d ) . join ( '\n' ) ;
58
+ doclet . properties . forEach ( ( p ) => {
59
+ if ( p . description ) {
60
+ properties . push ( ` // ${ p . description . replace ( / \n / g, '\n // ' ) } ` ) ;
61
+ }
62
+
63
+ properties . push ( ` ${ p . name } ${ p . optional ? '?' : '' } : ${ p . type . parsedType . typeExpression } ${ p . defaultvalue ? ' = ' . concat ( p . defaultvalue ) : '' } ` ) ;
64
+ } ) ;
65
+
66
+ if ( properties . length ) {
67
+ codeBlock . push ( '```js\n{' ) ;
68
+ codeBlock . push ( ...properties ) ;
69
+ codeBlock . push ( '}\n```\n' ) ;
70
+ }
71
+
72
+ markDown += `\n${ codeBlock . join ( '\n' ) } ` ;
73
+ properties = [ ] ;
74
+ codeBlock = [ ] ;
75
+ } ) ;
76
+
77
+ return `### Type definitions\n ${ markDown } ` ;
78
+ }
79
+
40
80
const generateFunctionDocletSection = ( doclet , isConstructor ) => {
41
81
const title = doclet . name ;
42
82
const header = `##${ doclet . longname . indexOf ( '#' ) !== - 1 || isConstructor ? '#' : '' } ${ title } ${ isConstructor ? ' Constructor' : '' } \n` ;
43
- const args = doclet . params && doclet . params . filter ( p => p . name . indexOf ( '.' ) === - 1 ) . map ( p => p . optional ? `[${ p . name } ]` : p . name ) . join ( ', ' ) || '' ;
83
+ const args = doclet . params && doclet . params . filter ( p => p . name . indexOf ( '.' ) === - 1 ) . map ( p => ( p . optional ? `[${ p . name } ]` : p . name ) ) . join ( ', ' ) || '' ;
44
84
const signature = `\`${ isConstructor ? 'new ' : '' } ${ doclet . meta . code . name || doclet . name } (${ args } )\`\n` ;
45
- const params = doclet . params ? generateParams ( doclet ) : `` ;
46
- const returns = doclet . returns ? `**Returns:** ${ doclet . returns . map ( p => `${ p . type ? renderLinks ( p ) : '' } ${ p . description ? ` ${ resolveInlineLinks ( p . description ) } ` : '' } ` ) } ` : `` ;
85
+ const params = doclet . params ? generateParams ( doclet ) : '' ;
86
+ const returns = doclet . returns ? `**Returns:** ${ doclet . returns . map ( p => `${ p . type ? renderLinks ( p ) : '' } ${ p . description ? ` ${ resolveInlineLinks ( p . description ) } ` : '' } ` ) } ` : '' ;
47
87
return [ header , signature , doclet . description && `${ resolveInlineLinks ( doclet . description ) } \n` , params , returns , '\n' ] . filter ( f => ! ! f ) . join ( '\n' ) ;
48
88
} ;
49
89
@@ -83,20 +123,24 @@ const generateMarkDown = (doclets, parent) => {
83
123
children . sort ( ( a , b ) => order ( a ) - order ( b ) ) ;
84
124
return children . map ( child => {
85
125
if ( child . kind === 'class' ) {
86
- return generateClassSection ( child ) . concat ( generateMarkDown ( doclets , child ) ) ;
126
+ return generateClassSection ( child )
127
+ . concat ( generateMarkDown ( doclets , child ) )
128
+ . concat ( generateTypeDefs ( typeDefs . filter ( ( td ) => td . memberof === child . name ) ) ) ;
87
129
} else if ( child . kind === 'function' || child . kind === 'member' ) {
88
130
return generateFunctionDocletSection ( child ) ;
89
131
}
90
132
return null ;
91
133
} ) . filter ( markdown => ! ! markdown ) . join ( '' ) ;
92
134
} ;
93
135
94
- const classNamesFrom = ( doclets ) => {
95
- return doclets . filter ( d => d . kind === 'class' ) . map ( d => d . name ) ;
96
- } ;
136
+ const classNamesFrom = ( doclets ) => doclets . filter ( d => d . kind === 'class' ) . map ( d => d . name ) ;
97
137
98
138
exports . publish = ( data , { destination } ) => {
99
139
knownClassNames = classNamesFrom ( data ( ) . get ( ) ) ;
100
- const markDown = generateMarkDown ( data ( ) . get ( ) . filter ( d => ! d . undocumented ) ) ;
140
+ typeDefs = data ( ) . get ( ) . filter ( d => d . kind === 'typedef' ) ;
141
+
142
+ const markDown = generateMarkDown ( data ( ) . get ( ) . filter ( d => {
143
+ return ! d . undocumented && d . kind !== 'typedef' ;
144
+ } ) ) ;
101
145
fs . writeFile ( destination , markDown ) ;
102
146
} ;
0 commit comments