forked from facebook/react-native-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateMarkdown.js
More file actions
269 lines (247 loc) · 6.77 KB
/
generateMarkdown.js
File metadata and controls
269 lines (247 loc) · 6.77 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const tokenizeComment = require('tokenize-comment');
const {
formatMultiplePlatform,
maybeLinkifyType,
maybeLinkifyTypeName,
formatTypeColumn,
formatDefaultColumn,
} = require('./propFormatter');
// Formats an array of rows as a Markdown table
function generateTable(rows) {
const colWidths = new Map();
for (const row of rows) {
for (const col of Object.keys(row)) {
colWidths.set(
col,
Math.max(colWidths.get(col) || col.length, String(row[col]).length)
);
}
}
if (!colWidths.size) {
return '';
}
let header = '|',
divider = '|';
for (const [col, width] of colWidths) {
header += ' ' + col.padEnd(width + 1) + '|';
divider += ' ' + '-'.repeat(width) + ' ' + '|';
}
let result = header + '\n' + divider + '\n';
for (const row of rows) {
result += '|';
for (const [col, width] of colWidths) {
result += ' ' + String(row[col] || '').padEnd(width + 1) + '|';
}
result += '\n';
}
return result;
}
// Formats information about a prop
function generateProp(propName, prop) {
const infoTable = generateTable([
{
Type: formatTypeColumn(prop),
...formatDefaultColumn(prop),
},
]);
return (
'### ' +
(prop.required ? '<div class="label required basic">Required</div>' : '') +
'`' +
propName +
'`' +
(prop.rnTags && prop.rnTags.platform
? formatMultiplePlatform(prop.rnTags.platform)
: '') +
'\n' +
'\n' +
(prop.description ? prop.description + '\n\n' : '') +
infoTable
);
}
// Formats information about a prop
function generateMethod(method, component) {
const infoTable = generateTable([
{
...(method.rnTags && method.rnTags.platform
? {Platform: formatPlatformName(method.rnTags.platform)}
: {}),
},
]);
return (
'### `' +
method.name +
'()`' +
'\n' +
'\n' +
generateMethodSignatureBlock(method, component) +
(method.description ? method.description + '\n\n' : '') +
generateMethodSignatureTable(method, component) +
infoTable
).trim();
}
function lowerFirst(s) {
return s[0].toLowerCase() + s.slice(1);
}
function generateMethodSignatureBlock(method, component) {
return (
'```jsx\n' +
(method.modifiers.includes('static')
? component.displayName + '.'
: lowerFirst(component.displayName + '.')) +
method.name +
'(' +
method.params
.map(param => (param.optional ? `[${param.name}]` : param.name))
.join(', ') +
');' +
'\n' +
'```\n\n'
);
}
function generateMethodSignatureTable(method, component) {
if (!method.params.length) {
return '';
}
return (
'**Parameters:**\n\n' +
generateTable(
method.params.map(param => ({
Name: param.name,
Type: param.type ? maybeLinkifyType(param.type) : '',
Required: param.optional ? 'No' : 'Yes',
Description: param.description,
}))
)
);
}
// Formats information about props
function generateProps({props, composes}) {
if (!props || !Object.keys(props).length) {
return '';
}
return (
'## Props' +
'\n' +
'\n' +
(composes && composes.length
? composes
.map(parent => 'Inherits ' + maybeLinkifyTypeName(parent) + '.')
.join('\n\n') + '\n\n'
: '') +
Object.keys(props)
.sort((a, b) => a.localeCompare(b))
.sort((a, b) => props[b].required - props[a].required)
.map(function(propName) {
return generateProp(propName, props[propName]);
})
.join('\n\n---\n\n')
);
}
function generateMethods(component) {
const {methods} = component;
if (!methods || !methods.length) {
return '';
}
return (
'## Methods' +
'\n' +
'\n' +
[...methods]
.sort((a, b) =>
a.name.localeCompare(
b.name /* TODO @nocommit what's a neutral locale */
)
)
.map(function(method) {
return generateMethod(method, component);
})
.join('\n\n---\n\n')
);
}
// Generates a Docusaurus header for a component page
function generateHeader({id, title}) {
return (
'---' + '\n' + 'id: ' + id + '\n' + 'title: ' + title + '\n' + '---' + '\n'
);
}
// Function to process example contained description
function preprocessDescription(desc) {
// Playground tabs for the class and functional components
const playgroundTab = `<div class="toggler">
<ul role="tablist" class="toggle-syntax">
<li id="functional" class="button-functional" aria-selected="false" role="tab" tabindex="0" aria-controls="functionaltab" onclick="displayTabs('syntax', 'functional')">
Function Component Example
</li>
<li id="classical" class="button-classical" aria-selected="false" role="tab" tabindex="0" aria-controls="classicaltab" onclick="displayTabs('syntax', 'classical')">
Class Component Example
</li>
</ul>
</div>`;
//Blocks for different syntax sections
const functionalBlock = `<block class='functional syntax' />`;
const classBlock = `<block class='classical syntax' />`;
const endBlock = `<block class='endBlock syntax' />`;
desc = desc
.split('\n')
.map(line => {
return line.replace(/ /, '');
})
.join('\n');
const descriptionTokenized = tokenizeComment(desc);
// Tabs counter for examples
let tabs = 0;
descriptionTokenized.examples.map(item => {
const matchSnackPlayer = item.language.match(/(SnackPlayer name=).*/g);
if (matchSnackPlayer) {
const matchClassComp = matchSnackPlayer[0].match(
/Class%20Component%20Example/
);
const matchFuncComp = matchSnackPlayer[0].match(
/Function%20Component%20Example/
);
if (matchClassComp || matchFuncComp) tabs++;
}
});
if (tabs === 2) {
const wrapper = `${playgroundTab}\n\n${functionalBlock}\n\n${
descriptionTokenized.examples[0].raw
}\n\n${classBlock}\n\n${
descriptionTokenized.examples[1].raw
}\n\n${endBlock}`;
return (
descriptionTokenized.description +
`\n## Example\n` +
wrapper +
'\n' +
descriptionTokenized?.footer
);
} else {
return (
desc.substr(0, desc.search('```SnackPlayer')) +
'\n' +
'\n## Example\n' +
'\n' +
desc.substr(desc.search('```SnackPlayer'))
);
}
}
function generateMarkdown({id, title}, component) {
const markdownString =
generateHeader({id, title}) +
'\n' +
preprocessDescription(component.description) +
'\n\n' +
'---\n\n' +
'# Reference\n\n' +
generateProps(component) +
generateMethods(component);
return markdownString.replace(/\n{3,}/g, '\n\n');
}
module.exports = generateMarkdown;