-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.js
executable file
·115 lines (101 loc) · 2.66 KB
/
docs.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* 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.
*/
var fs = require('fs');
var path = require('path');
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;
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 = {}) {
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;
}
const json = require('./readme.json');
function buildDocs(api) {
// api is an object keyed by filepath. We use the file name as component name.
for (var filepath in api) {
var name = getComponentName(filepath);
var markdown = generateMarkdown(name, api[filepath]);
fs.writeFileSync('./docs/' + name + '.md', markdown);
process.stdout.write(filepath + ' -> ' + name + '.md\n');
}
}
function getComponentName(filepath) {
var name = path.basename(filepath);
var ext;
while ((ext = path.extname(name))) {
name = name.substring(0, name.length - ext.length);
}
return name;
}
buildDocs(json);