1
1
'use strict' ;
2
2
3
- var extend = require ( 'extend' ) ,
4
- mdast = require ( 'mdast' ) ,
5
- formatType = require ( '../markdown_format_type' ) ,
6
- toc = require ( 'mdast-toc' ) ,
7
- u = require ( 'unist-builder' ) ,
8
- inlineLex = require ( 'jsdoc-inline-lex' ) ;
9
-
10
- /**
11
- * Format link & tutorial tags with simple code inline tags.
12
- *
13
- * @param {string } text input - typically a description
14
- * @returns {string } markdown-friendly output
15
- * @private
16
- * @example
17
- * formatInlineTags('{@link Foo}'); // "`Foo`"
18
- */
19
- function formatInlineTags ( text ) {
20
- var output = '' ;
21
- var tokens = inlineLex ( text ) ;
22
-
23
- for ( var i = 0 ; i < tokens . length ; i ++ ) {
24
- if ( tokens [ i ] . type === 'text' ) {
25
- output += tokens [ i ] . capture [ 0 ] ;
26
- } else {
27
- output += '`' + tokens [ i ] . capture [ 1 ] + '`' ;
28
- }
29
- }
30
-
31
- return output ;
32
- }
33
-
34
- function identity ( x ) {
35
- return x ;
36
- }
37
-
38
- function paramSection ( comment ) {
39
- return ! ! comment . params && u ( 'root' , [
40
- u ( 'strong' , [ u ( 'text' , 'Parameters' ) ] ) ,
41
- u ( 'list' , { ordered : false } , comment . params . map ( function ( param ) {
42
- return u ( 'listItem' , [
43
- u ( 'paragraph' , [
44
- u ( 'inlineCode' , param . name ) ,
45
- u ( 'text' , ' ' ) ,
46
- ! ! param . type && u ( 'strong' , [ u ( 'text' , formatType ( param . type ) ) ] ) ,
47
- u ( 'text' , ' ' ) ,
48
- mdast . parse ( formatInlineTags ( param . description ) ) ,
49
- ! ! param . default && u ( 'root' , [
50
- u ( 'text' , ' (optional, default ' ) ,
51
- u ( 'text' , param . default , 'inlineCode' ) ,
52
- u ( 'text' , ')' )
53
- ] )
54
- ] . filter ( identity ) )
55
- ] ) ;
56
- } ) )
57
- ] ) ;
58
- }
59
-
60
- function propertySection ( comment ) {
61
- return ! ! comment . properties && u ( 'root' , [
62
- u ( 'strong' , [ u ( 'text' , 'Properties' ) ] ) ,
63
- u ( 'list' , { ordered : false } ,
64
- comment . properties . map ( function ( property ) {
65
- return u ( 'listItem' , [
66
- u ( 'paragraph' , [
67
- u ( 'inlineCode' , property . title ) ,
68
- u ( 'text' , ' ' ) ,
69
- u ( 'text' , [ u ( 'text' , formatType ( property . type ) ) ] ) ,
70
- u ( 'text' , ' ' ) ,
71
- mdast . parse ( formatInlineTags ( property . description ) )
72
- ] )
73
- ] )
74
- } ) )
75
- ] ) ;
76
- }
77
-
78
- function examplesSection ( comment ) {
79
- return ! ! comment . examples && u ( 'root' , [
80
- u ( 'strong' , [ u ( 'text' , 'Examples' ) ] ) ,
81
- u ( 'root' , comment . examples . map ( function ( example ) {
82
- return u ( 'code' , { lang : 'javascript' } , example ) ;
83
- } ) )
84
- ] ) ;
85
- }
86
-
87
- function returnsSection ( comment ) {
88
- return ! ! comment . returns && u ( 'root' , comment . returns . map ( function ( returns ) {
89
- return u ( 'paragraph' , [
90
- u ( 'text' , 'Returns ' ) ,
91
- u ( 'strong' , [ u ( 'text' , formatType ( returns . type ) ) ] ) ,
92
- u ( 'text' , ' ' ) ,
93
- mdast . parse ( formatInlineTags ( returns . description ) )
94
- ] ) ;
95
- } ) ) ;
96
- }
97
-
98
- function generate ( depth , comment ) {
99
- return u ( 'root' , [
100
- u ( 'heading' , { depth : depth } , [ u ( 'text' , comment . name ) ] ) ,
101
- mdast . parse ( formatInlineTags ( comment . description ) ) ,
102
- paramSection ( comment ) ,
103
- propertySection ( comment ) ,
104
- examplesSection ( comment ) ,
105
- returnsSection ( comment ) ,
106
- ! ! comment . members . instance . length &&
107
- u ( 'root' , comment . members . instance . map ( generate . bind ( null , depth + 1 ) ) ) ,
108
- ! ! comment . members . static . length &&
109
- u ( 'root' , comment . members . static . map ( generate . bind ( null , depth + 1 ) ) ) ,
110
- ] . filter ( identity ) ) ;
111
- }
3
+ var mdast = require ( 'mdast' ) ,
4
+ markdownAST = require ( './markdown_ast' ) ;
112
5
113
6
/**
114
7
* Formats documentation as
@@ -123,16 +16,7 @@ function generate(depth, comment) {
123
16
* @return {undefined } calls callback
124
17
*/
125
18
module . exports = function ( comments , opts , callback ) {
126
-
127
- opts = opts || { } ;
128
-
129
- if ( opts . toc || true ) {
130
- mdast . use ( toc ) ;
131
- }
132
-
133
- var ast = u ( 'root' , comments . map ( function ( comment ) {
134
- return generate ( 1 , comment ) ;
135
- } ) ) ;
136
-
137
- return callback ( null , mdast . stringify ( ast ) ) ;
19
+ markdownAST ( comments , opts , function ( err , ast ) {
20
+ return callback ( null , mdast . stringify ( ast ) ) ;
21
+ } ) ;
138
22
} ;
0 commit comments