forked from facebook/react-native-website
-
Notifications
You must be signed in to change notification settings - Fork 328
/
preprocessGeneratedApiDocs.js
85 lines (77 loc) · 2.28 KB
/
preprocessGeneratedApiDocs.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// Preprocess the react-docgen artifact before rendering it to Markdown.
// This file may end up in the React Native repo, as part of the
// `generate-api-docs` script.
const tokenizeComment = require('tokenize-comment');
const {typeOf} = require('tokenize-comment/lib/utils');
function joinDescriptionAndExamples(tokenized) {
let sections = [];
if (tokenized.description) {
sections.push(tokenized.description);
}
for (const {raw} of tokenized.examples) {
sections.push(raw);
}
if (tokenized.footer) {
sections.push(tokenized.footer);
}
return sections.join('\n\n');
}
function preprocessTagsInDescription(obj) {
if (obj && obj.description) {
obj.description = obj.description
.split('\n')
.map(line => {
return line.replace(/ /, '');
})
.join('\n');
const descriptionTokenized = tokenizeComment(obj.description);
obj.description = obj.description.replace(
/@platform .*|@default .*|@type .*/g,
''
);
obj.rnTags = {};
const platformTag = descriptionTokenized.tags.find(
({key}) => key === 'platform'
);
const defaultTag = descriptionTokenized.tags.filter(
tag => tag.key === 'default'
);
const typeTag = descriptionTokenized.tags.filter(tag => tag.key === 'type');
if (platformTag) {
obj.rnTags.platform = platformTag.value.split(',');
}
if (defaultTag.length) {
obj.rnTags.default = [];
defaultTag.forEach(tag => {
obj.rnTags.default.push(tag.value);
});
}
if (typeTag.length) {
obj.rnTags.type = [];
typeTag.forEach(tag => {
obj.rnTags.type.push(tag.value);
});
}
}
}
// NOTE: This function mutates `docs`.
function preprocessGeneratedApiDocs(docs) {
for (const {component} of docs) {
if (component.props && component.description) {
for (const prop of Object.values(component.props)) {
preprocessTagsInDescription(prop);
}
for (const prop of component.methods) {
preprocessTagsInDescription(prop);
}
}
}
}
module.exports = preprocessGeneratedApiDocs;