-
Notifications
You must be signed in to change notification settings - Fork 16
/
generateMarkdown.js
97 lines (85 loc) · 2.2 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
function stringOfLength(string, length) {
let newString = '';
for (let i = 0; i < length; i++) {
newString += string;
}
return newString;
}
function generateTitle(name) {
const title = '`' + name + '` (component)';
return title + '\n' + stringOfLength('=', title.length) + '\n';
}
function generateDesciption(description) {
return description + '\n';
}
function generatePropType(type) {
let values;
let name;
name = type.name;
if(type.name==='signature'){
name = type.raw;
}
if (Array.isArray(type.value)) {
values =
'(' +
type.value
.map(function(typeValue) {
return typeValue.name || typeValue.value;
})
.join('|') +
')';
} else {
values = type.value;
}
return '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.flowType ? generatePropType(prop.flowType) : '') +
(prop.defaultValue ? generatePropDefaultValue(prop.defaultValue) : '') +
'\n'
);
}
function generateProps(props) {
if (!props) return '\n';
const 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) {
const markdownString =
generateTitle(name) +
'\n' +
generateDesciption(reactAPI.description) +
'\n' +
generateProps(reactAPI.props);
return markdownString;
}
module.exports = generateMarkdown;