-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathmarkdown_ast.js
254 lines (225 loc) · 8.65 KB
/
markdown_ast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* @flow */
'use strict';
var u = require('unist-builder'),
remark = require('remark'),
mergeConfig = require('../merge_config'),
toc = require('remark-toc'),
hljs = require('highlight.js'),
GithubSlugger = require('github-slugger'),
LinkerStack = require('./util/linker_stack'),
rerouteLinks = require('./util/reroute_links'),
_formatType = require('./util/format_type');
var DEFAULT_LANGUAGE = 'javascript';
/**
* Given a hierarchy-nested set of comments, generate an remark-compatible
* Abstract Syntax Tree usable for generating Markdown output
*
* @param comments nested comment
* @param {Object} args currently none accepted
* @param {boolean} [args.markdownToc=true] whether to include a table of contents
* in markdown output.
* @param {Object} [args.hljs={}] config to be passed to highlightjs for code highlighting:
* consult hljs.configure for the full list.
* @returns {Promise<Object>} returns an eventual Markdown value
*/
function markdownAST(comments/*: Array<Comment> */, args/*: Object */) {
return mergeConfig(args)
.then(config => buildMarkdownAST(comments, config));
}
function buildMarkdownAST(comments/*: Array<Comment> */, config/*: DocumentationConfig*/) {
// Configure code highlighting
var hljsOptions = config.hljs || {};
hljs.configure(hljsOptions);
var linkerStack = new LinkerStack(config)
.namespaceResolver(comments, namespace => {
var slugger = new GithubSlugger();
return '#' + slugger.slug(namespace);
});
var formatType = _formatType.bind(undefined, linkerStack.link);
var generatorComment = [
u('html', '<!-- Generated by documentation.js. Update this documentation by updating the source code. -->')
];
var tableOfContentsHeading = [
u('heading', { depth: 3 }, [u('text', 'Table of Contents')])
];
/**
* Generate an AST chunk for a comment at a given depth: this is
* split from the main function to handle hierarchially nested comments
*
* @param {number} depth nesting of the comment, starting at 1
* @param {Object} comment a single comment
* @returns {Object} remark-compatible AST
*/
function generate(depth/*: number */, comment/*: Comment */) {
function typeSection(comment/*: Comment */) {
return comment.type && u('paragraph', [
u('text', 'Type: ')
].concat(
formatType(comment.type)
));
}
function paramList(params/*: Array<CommentTag> */) {
return u('list', { ordered: false }, params.map(param =>
u('listItem', [
u('paragraph', [
u('inlineCode', param.name),
u('text', ' '),
!!param.type && u('strong', formatType(param.type)),
u('text', ' ')
].concat(param.description ? param.description.children : [])
.concat([
!!param.default && u('paragraph', [
u('text', ' (optional, default '),
u('inlineCode', param.default),
u('text', ')')
])
]).filter(Boolean))
].concat(param.properties && paramList(param.properties))
.filter(Boolean))));
}
function paramSection(comment/*: Comment */) {
return comment.params.length > 0 && [
u('strong', [u('text', 'Parameters')]),
paramList(comment.params)
];
}
function propertySection(comment/*: Comment */) {
return comment.properties.length > 0 && [
u('strong', [u('text', 'Properties')]),
propertyList(comment.properties)
];
}
function propertyList(properties/*: Array<CommentTag> */) {
return u('list', { ordered: false },
properties.map(property =>
u('listItem', [
u('paragraph', [
u('inlineCode', property.name),
u('text', ' '),
u('strong', formatType(property.type)),
u('text', ' ')
]
.concat(property.description ? property.description.children: [])
.filter(Boolean)),
property.properties && propertyList(property.properties)
].filter(Boolean))));
}
function examplesSection(comment/*: Comment */) {
return comment.examples.length > 0 && [u('strong', [u('text', 'Examples')])]
.concat(comment.examples.reduce(function (memo, example) {
var language = hljsOptions.highlightAuto ?
hljs.highlightAuto(example.description).language : DEFAULT_LANGUAGE;
return memo.concat(example.caption ?
[u('paragraph', [u('emphasis', example.caption)])] :
[]).concat([u('code', { lang: language }, example.description)]);
}, []));
}
function returnsSection(comment/*: Comment */) {
return comment.returns.length > 0 && comment.returns.map(returns =>
u('paragraph', [
u('text', 'Returns '),
u('strong', formatType(returns.type)),
u('text', ' ')
].concat(returns.description ? returns.description.children : [])));
}
function throwsSection(comment/*: Comment */) {
return comment.throws.length > 0 &&
u('list', { ordered: false },
comment.throws.map(returns => u('listItem', [
u('paragraph', [
u('text', 'Throws '),
u('strong', formatType(returns.type)),
u('text', ' ')
].concat(returns.description ? returns.description.children : []))
])));
}
function augmentsLink(comment/*: Comment */) {
return comment.augments.length > 0 && u('paragraph', [
u('strong', [
u('text', 'Extends '),
u('text', comment.augments.map(tag => tag.name).join(', '))
])
]);
}
function seeLink(comment/*: Comment */) {
return comment.sees.length > 0 && u('list', { ordered: false }, comment.sees.map(see =>
u('listItem', [
u('strong', [
u('text', 'See: ')
].concat(see.children))
])
));
}
function githubLink(comment/*: Comment */) {
return comment.context && comment.context.github && u('paragraph', [
u('link', {
title: 'Source code on GitHub',
url: comment.context.github.url
}, [u('text', comment.context.github.path + ':' +
comment.context.loc.start.line + '-' +
comment.context.loc.end.line)])
]);
}
function metaSection(comment/*: Comment */) {
let meta = ['version', 'since', 'copyright', 'author', 'license', 'deprecated']
.filter(tag => comment[tag]);
return !!meta.length && [u('strong', [u('text', 'Meta')])].concat(
u('list', { ordered: false },
meta
.map(tag => {
let metaContent;
if (tag === 'copyright' || tag === 'deprecated') {
metaContent = comment[tag];
} else {
metaContent = u('text', comment[tag]);
}
return u('listItem', [
u('paragraph', [
u('strong', [u('text', tag)]),
u('text', ': '),
metaContent
])
]);
})));
}
if (comment.kind === 'note') {
return [u('heading', { depth }, [u('text', comment.name || '')])]
.concat(comment.description);
}
return [u('heading', { depth }, [u('text', comment.name || '')])]
.concat(githubLink(comment))
.concat(augmentsLink(comment))
.concat(seeLink(comment))
.concat(comment.description ? comment.description.children : [])
.concat(typeSection(comment))
.concat(paramSection(comment))
.concat(propertySection(comment))
.concat(examplesSection(comment))
.concat(throwsSection(comment))
.concat(returnsSection(comment))
.concat(metaSection(comment))
.concat(!!comment.members.global.length &&
comment.members.global.reduce((memo, child) =>
memo.concat(generate(depth + 1, child)), []))
.concat(!!comment.members.instance.length &&
comment.members.instance.reduce((memo, child) =>
memo.concat(generate(depth + 1, child)), []))
.concat(!!comment.members.static.length &&
comment.members.static.reduce((memo, child) =>
memo.concat(generate(depth + 1, child)), []))
.concat(!!comment.members.inner.length &&
comment.members.inner.reduce((memo, child) =>
memo.concat(generate(depth + 1, child)), []))
.filter(Boolean);
}
var root = rerouteLinks(linkerStack.link,
u('root', generatorComment
.concat(config.markdownToc ? tableOfContentsHeading : [])
.concat(comments.reduce((memo, comment) =>
memo.concat(generate(2, comment)), []))));
if (config.markdownToc) {
root = remark().use(toc, { tight: true }).run(root);
}
return Promise.resolve(root);
}
module.exports = markdownAST;