forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateMarkdown.sh
executable file
·77 lines (65 loc) · 1.95 KB
/
generateMarkdown.sh
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
/**
* 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) {
if (description) return description + '\n';
return '';
}
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.name + (values ? values: '') + '`';
}
function generatePropDefaultValue(value) {
return '`' + value.value + '`';
}
function generateProp(propName, prop) {
return (
'|`' + propName + '`' + (prop.required ? ' (required)' : '') +
'|' + (prop.type ? generatePropType(prop.type) : '') +
'|' + (prop.defaultValue ? generatePropDefaultValue(prop.defaultValue) : '') +
'|' + (prop.description ? prop.description + '\n\n' : '') + '|'
);
}
function generateProps(props) {
return (
'| name | type | default value | description |' + '\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;