1
1
var u = require ( 'unist-builder' ) ,
2
2
hljs = require ( 'highlight.js' ) ,
3
3
GithubSlugger = require ( 'github-slugger' ) ,
4
+ unnest = require ( '../unnest' ) ,
4
5
createLinkerStack = require ( './util/linker_stack' ) ,
5
6
rerouteLinks = require ( './util/reroute_links' ) ,
6
7
_formatType = require ( './util/format_type' ) ;
7
8
9
+ /**
10
+ * Format a list of left, right items into a remark AST list
11
+ *
12
+ * @param {Array<{ left: Array<Object>, left: Array<Object> }> } items left, right pairs
13
+ * @returns {Object } markdown AST
14
+ */
15
+ function list ( items ) {
16
+ return u ( 'list' , { ordered : false } , items . map ( function ( item ) {
17
+ return u ( 'listItem' , u ( 'paragraph' , item . left . concat ( item . right || [ ] ) ) ) ;
18
+ } ) ) ;
19
+ }
20
+
21
+ /**
22
+ * Format a list of left, right items into a remark AST table
23
+ *
24
+ * @param {Array<{ left: Array<Object>, left: Array<Object> }> } items left, right pairs
25
+ * @param {Array<string> } headers column headers
26
+ * @returns {Object } markdown AST
27
+ */
28
+ function table ( items , headers ) {
29
+ return u ( 'table' , { align : 'left' } ,
30
+ [ u ( 'tableRow' , headers . map ( function ( header ) {
31
+ return u ( 'tableCell' , [ u ( 'text' , header ) ] ) ;
32
+ } ) ) ]
33
+ . concat ( items . map ( function ( item ) {
34
+ return u ( 'tableRow' , [
35
+ u ( 'tableCell' , u ( 'paragraph' , item . left ) ) ,
36
+ u ( 'tableCell' , u ( 'paragraph' , item . right ) )
37
+ ] ) ;
38
+ } ) ) ) ;
39
+ }
40
+
8
41
/**
9
42
* Given a hierarchy-nested set of comments, generate an remark-compatible
10
43
* Abstract Syntax Tree usable for generating Markdown output
@@ -29,6 +62,31 @@ function commentsToAST(comments, options, callback) {
29
62
30
63
var formatType = _formatType . bind ( undefined , linkerStack . link ) ;
31
64
65
+ var listFormatter = list ;
66
+ if ( options . markdownTables ) {
67
+ listFormatter = table ;
68
+ }
69
+
70
+ function genericFormatRight ( param ) {
71
+ return ( param . description ? param . description . children [ 0 ] . children : [ ] )
72
+ . concat ( [
73
+ ! ! param . default && u ( 'paragraph' , [
74
+ u ( 'text' , ' (optional, default ' ) ,
75
+ u ( 'inlineCode' , param . default ) ,
76
+ u ( 'text' , ')' )
77
+ ] )
78
+ ] ) ;
79
+ }
80
+
81
+ function genericFormatLeft ( tag ) {
82
+ return [
83
+ u ( 'inlineCode' , tag . name ) ,
84
+ u ( 'text' , ' ' ) ,
85
+ ! ! tag . type && u ( 'strong' , formatType ( tag . type ) ) ,
86
+ u ( 'text' , ' ' )
87
+ ] . filter ( Boolean ) ;
88
+ }
89
+
32
90
/**
33
91
* Generate an AST chunk for a comment at a given depth: this is
34
92
* split from the main function to handle hierarchially nested comments
@@ -40,24 +98,14 @@ function commentsToAST(comments, options, callback) {
40
98
function generate ( depth , comment ) {
41
99
42
100
function paramList ( params ) {
43
- return u ( 'list' , { ordered : false } , params . map ( function ( param ) {
44
- return u ( 'listItem' , [
45
- u ( 'paragraph' , [
46
- u ( 'inlineCode' , param . name ) ,
47
- u ( 'text' , ' ' ) ,
48
- ! ! param . type && u ( 'strong' , formatType ( param . type ) ) ,
49
- u ( 'text' , ' ' )
50
- ] . concat ( param . description ? param . description . children : [ ] )
51
- . concat ( [
52
- ! ! param . default && u ( 'paragraph' , [
53
- u ( 'text' , ' (optional, default ' ) ,
54
- u ( 'inlineCode' , param . default ) ,
55
- u ( 'text' , ')' )
56
- ] )
57
- ] ) . filter ( Boolean ) )
58
- ] . concat ( param . properties && paramList ( param . properties ) )
59
- . filter ( Boolean ) ) ;
60
- } ) ) ;
101
+ return listFormatter ( params . map ( function ( param ) {
102
+ return {
103
+ left : genericFormatLeft ( param ) ,
104
+ right : genericFormatRight ( param )
105
+ . concat ( param . properties && paramList ( param . properties ) )
106
+ . filter ( Boolean )
107
+ } ;
108
+ } ) , [ 'Parameter' , 'Description' ] ) ;
61
109
}
62
110
63
111
function paramSection ( comment ) {
@@ -75,20 +123,14 @@ function commentsToAST(comments, options, callback) {
75
123
}
76
124
77
125
function propertyList ( properties ) {
78
- return u ( 'list' , { ordered : false } ,
79
- properties . map ( function ( property ) {
80
- return u ( 'listItem' , [
81
- u ( 'paragraph' , [
82
- u ( 'inlineCode' , property . name ) ,
83
- u ( 'text' , ' ' ) ,
84
- u ( 'strong' , formatType ( property . type ) ) ,
85
- u ( 'text' , ' ' )
86
- ]
87
- . concat ( property . description ? property . description . children : [ ] )
88
- . filter ( Boolean ) ) ,
89
- property . properties && propertyList ( property . properties )
90
- ] . filter ( Boolean ) ) ;
91
- } ) ) ;
126
+ return listFormatter ( properties . map ( function ( property ) {
127
+ return {
128
+ left : genericFormatLeft ( property ) ,
129
+ right : genericFormatRight ( property )
130
+ . concat ( property . properties && propertyList ( property . properties ) )
131
+ . filter ( Boolean )
132
+ } ;
133
+ } ) , [ 'Property' , 'Description' ] ) ;
92
134
}
93
135
94
136
function examplesSection ( comment ) {
@@ -190,9 +232,16 @@ function commentsToAST(comments, options, callback) {
190
232
}
191
233
192
234
return callback ( null , rerouteLinks ( linkerStack . link ,
193
- u ( 'root' , comments . reduce ( function ( memo , comment ) {
194
- return memo . concat ( generate ( 1 , comment ) ) ;
195
- } , [ ] ) ) ) ) ;
235
+ u ( 'root' , comments
236
+ . map ( function ( comment ) {
237
+ if ( options . markdownTables ) {
238
+ return unnest ( comment ) ;
239
+ }
240
+ return comment ;
241
+ } )
242
+ . reduce ( function ( memo , comment ) {
243
+ return memo . concat ( generate ( 1 , comment ) ) ;
244
+ } , [ ] ) ) ) ) ;
196
245
}
197
246
198
247
module . exports = commentsToAST ;
0 commit comments