-
Notifications
You must be signed in to change notification settings - Fork 10
/
generateMarkdown.js
80 lines (68 loc) · 1.95 KB
/
generateMarkdown.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
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
"use strict";
function stringOfLength(string, length) {
var newString = '';
for (var i = 0; i < length; i++) {
newString += string;
}
return newString;
}
function generateTitle(name) {
var title = '`' + name + '` (component)';
return title + '\n' + stringOfLength('=', title.length) + '\n';
}
function generateDesciption(description) {
return description + '\n';
}
function generatePropType(type) {
var values;
if (Array.isArray(type.value)) {
values = '(' +
type.value.map(function(typeValue) {
return typeValue.name || typeValue.value;
}).join('|') +
')';
} else {
values = type.value;
}
return 'type: `' + type.name + (values ? values: '') + '`\n';
}
function generatePropDefaultValue(value) {
return 'defaultValue: `' + value.value + '`\n';
}
function generateProp(propName, prop) {
return (
'### `' + propName + '`' + (prop.required ? ' (required)' : '') + '\n' +
'\n' +
(prop.description ? prop.description + '\n\n' : '') +
(prop.type ? generatePropType(prop.type) : '') +
(prop.defaultValue ? generatePropDefaultValue(prop.defaultValue) : '') +
'\n'
);
}
function generateProps(props) {
var title = 'Props';
return (
title + '\n' +
stringOfLength('-', title.length) + '\n' +
'\n' +
Object.keys(props).sort().map(function(propName) {
return generateProp(propName, props[propName]);
}).join('\n')
);
}
function generateMarkdown(name, reactAPI) {
var markdownString =
generateTitle(name) + '\n' +
generateDesciption(reactAPI.description) + '\n' +
generateProps(reactAPI.props);
return markdownString;
}
module.exports = generateMarkdown;