forked from yuristrelets/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateMarkdown.js
More file actions
65 lines (53 loc) · 1.62 KB
/
generateMarkdown.js
File metadata and controls
65 lines (53 loc) · 1.62 KB
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
function generateTitle (name) {
return `## ${name} \`<${name}/>\``;
}
function generateDesciption (description) {
return description + '\n';
}
function generatePropType (type) {
let 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 generateProp (propName, prop) {
if (!prop.description) {
if (propName === 'className') {
prop.description = 'Additional class(es) for custom styling.';
} else if (propName === 'children') {
prop.description = 'Children to pass through the component.';
}
}
return (
`| \`${propName}\` ${prop.required ? '(required)' : ''}`
+ `| ${(prop.type ? generatePropType(prop.type) : '')} `
+ `| ${(prop.defaultValue ? `\`${prop.defaultValue}\`` : '')} `
+ `| ${(prop.description ? prop.description : '')} `
+ '|'
);
}
function generateProps (props) {
const title = '### Properties';
return (
`${title}\n`
+ '| Name | Type | Default | Description |\n'
+ '|:-----|:-----|:-----|:-----|\n'
+ Object.keys(props).sort().map(propName => {
return generateProp(propName, props[propName]);
}).join('\n')
);
}
function generateMarkdown (name, reactAPI) {
const markdownString = generateTitle(name) + '\n'
+ (reactAPI.description ? generateDesciption(reactAPI.description) + '\n' : '\n')
+ generateProps(reactAPI.props);
return markdownString;
}
module.exports = generateMarkdown;