-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmarkdown-docs.js
executable file
·190 lines (181 loc) · 5.39 KB
/
markdown-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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
const path = require('path');
const escape = require('escape-html');
let json = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
var chunk = process.stdin.read();
if (chunk !== null) {
json += chunk;
}
});
process.stdin.on('end', () => {
buildDocs(JSON.parse(json));
});
function buildDocs(components) {
for (const filename in components) {
const component = components[filename];
const componentName = path.basename(filename, '.js');
console.log(`\n### ${componentName}\n`);
if (component.description) {
console.log(`${component.description}\n`);
}
const propNames = Object.keys(component.props || {});
if (propNames.length) {
console.log(`#### Props\n`);
console.log('<table>');
console.log('<thead>');
console.log('<tr>');
console.log('<th>Name</th>');
console.log('<th colspan="2">Type</th>');
console.log('<th>Default</th>');
console.log('<th>Description</th>');
console.log('</tr>');
console.log('</thead>');
console.log('<tbody>');
propNames.forEach(name => {
const prop = component.props[name];
const required = s =>
prop.required ? `<strong title="Required">${s}</strong>` : s;
const extraRows = [];
let type = renderType(prop.type, extraRows, 0, true);
if (type.includes('\n')) {
type = `\n${type}\n`;
}
console.log(`<tr>`);
console.log(
`<td valign="top" rowspan="${extraRows.length + 1}">${required(
name
)}</td>`
);
console.log(`<td valign="top" colspan="2">${type}</td>`);
console.log(
`<td valign="top" align="right" rowspan="${extraRows.length +
1}">${renderValue(prop.defaultValue)}</td>`
);
if (prop.description) {
console.log(
`<td valign="top" valign="top" rowspan="${extraRows.length + 1}">`
);
console.log(`\n${prop.description}\n`);
console.log(`</td>`);
} else {
console.log(
`<td valign="top" valign="top" rowspan="${extraRows.length +
1}"></td>`
);
}
console.log('</tr>');
extraRows.forEach(row => {
console.log(`<tr>${row}</tr>`);
});
});
console.log('</tbody>');
console.log('</table>');
}
}
}
const TYPES = {
string: 'String',
number: 'Number',
func: 'Function',
bool: 'Boolean',
element: 'React Element',
object: 'Object',
node: 'Node'
};
function renderShape(value, extraRows, topLevel) {
const ref = topLevel ? null : ++REF_COUNTER;
const nextPos = topLevel
? extraRows.length
: extraRows.push(
`<td colspan="2"><a name="shape-${ref}"><sup>${ref}</sup> Object</a></td>`
);
extraRows.splice(
nextPos,
0,
...Object.keys(value).map(key => {
const name = value[key].required
? `<strong title="Required">${key}</strong>`
: key;
const type = renderType(value[key], extraRows);
return `<td valign="top">${name}</td><td valign="top">${type}</td>`;
})
);
if (topLevel) {
return 'Object';
}
const tooltip = `{ ${Object.keys(value).join(', ')} }`;
return `<a href="#shape-${ref}" title="${tooltip}">Object <sup>${ref}</sup></a>`;
}
let REF_COUNTER = 0;
function renderType(value, extraRows = [], depth = 0, topLevel = false) {
if (!value) {
return '';
}
const indent = Array(2 * depth + 1).join(' ');
if (value.name in TYPES) {
return TYPES[value.name];
}
if (value.name === 'enum') {
return `One of… <br>\n${indent} ${value.value
.map(v => renderValue(v))
.join(` <br>\n${indent} `)}`;
}
if (value.name === 'union') {
return `One of… <br>\n${indent} ${value.value
.map(t => renderType(t, extraRows, depth + 1))
.join(` <br>\n${indent} `)}`;
}
if (value.name === 'arrayOf' || value.name === 'objectOf') {
return `${
value.name === 'arrayOf' ? 'Array' : 'Object'
} of… <br>\n${indent} ${renderType(
value.value,
extraRows,
depth + 1
)}`;
}
if (value.name === 'shape') {
return renderShape(value.value, extraRows, topLevel);
}
throw new Error(`Unsupported type: ${value.name}`);
}
const CODE_CHARS = {
'\0': '<code title="null character">\\0</code>',
'\t': '<code title="tab">\\t</code>',
'\n': '<code title="line feed">\\n</code>',
'\r': '<code title="carriage return">\\r</code>',
'\u00A0': '<code title="non-breaking space">\\u00A0</code>'
};
function renderValue(value) {
if (!value) {
return '';
}
if (value.value === 'undefined') {
return '';
}
if (value.value.match(/^\/.*\/[ig]*$/)) {
return `<pre>${value.value}</pre>`;
}
if (value.value.match(/^true|false$/)) {
return value.value;
}
if (value.value.match(/^[ \d./*+-]*$/)) {
// eslint-disable-next-line no-eval
return escape('' + eval(value.value));
}
if (value.value.match(/^"([^"]|[\\"])+"$/)) {
// eslint-disable-next-line no-eval
return escape(eval(value.value)).replace(/./g, match => {
return CODE_CHARS[match] || match;
});
}
if (value.value === '[]') {
return `<code title="empty array">[]</code>`;
}
if (value.value === '{}') {
return `<code title="empty object">{}</code>`;
}
return value.value;
}