forked from HospitalRun/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateMarkdown.js
69 lines (56 loc) · 1.58 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
function generateTitle(name) {
const title = '`' + name + '` (component)';
return '# ' + title + '\n';
}
function generateDesciption(description) {
return description;
}
function generatePropType(type) {
let value;
if(type.raw) {
value = type.raw;
} else {
value = type.name;
}
return '`' + value + '`';
}
function generatePropDefaultValue(value) {
return value.value;
}
function generateProp(propName, prop) {
const required = prop.required ? 'yes' : '-';
const description = prop.description ? prop.description.replace('\n', ' ') : '';
const defaultValue = prop.defaultValue ? generatePropDefaultValue(prop.defaultValue) : '';
const type = prop.tsType ? generatePropType(prop.tsType) : '';
return '|' + propName + '|' + type + '|' + required + '|' + defaultValue + '|' + description + '|';
}
function generateProps(props) {
if (!props) return '\n';
const title = '## Props';
const tableHeader = '| property | propType | required | default | description |';
const tableSeparator = '|----------|----------|----------|---------|-------------|';
return (
title +
'\n' +
tableHeader +
'\n' +
tableSeparator +
'\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;