-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathgenerate-jsdoc.js
128 lines (104 loc) · 3.2 KB
/
generate-jsdoc.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
const jsdoc2md = require('jsdoc-to-markdown');
const fs = require('fs');
const path = require('path');
const prettier = require('prettier');
const publicClassFiles = ['amplitude-client.js', 'amplitude.js', 'identify.js', 'revenue.js'];
const homeFile = 'amplitude-client.js';
const publicTypedefFiles = ['options.js'];
const srcDir = path.join(__dirname, '../', 'src');
const outputDir = path.join(__dirname, 'docs');
function generateTypedefMarkdown(inputFile) {
const inputFilePath = path.join(srcDir, inputFile);
const data = jsdoc2md.getTemplateDataSync({ files: inputFilePath });
const name = data.find((e) => e.kind === 'typedef').name;
const filteredData = data.filter((e) => e.kind === 'typedef');
const markdownOutput = filteredData.map((item) => documentOptionsFile(item)).join('\n');
fs.writeFileSync(path.join(outputDir, `${name}.md`), prettier.format(markdownOutput, { parser: 'mdx' }));
}
function generateClassMarkdown(inputFile) {
const inputFilePath = path.join(srcDir, inputFile);
const data = jsdoc2md.getTemplateDataSync({ files: inputFilePath });
const className = data.find((e) => e.kind === 'class').name;
const filteredData = data.filter(
(e) => e.kind === 'constructor' || (e.access === 'public' && (e.kind === 'function' || e.kind === 'member')),
);
let markdownOutput = filteredData.map((item) => documentClassFile(item)).join('\n');
if (inputFile === homeFile) {
markdownOutput =
`\
---
slug: /
---
` + markdownOutput;
}
fs.writeFileSync(path.join(outputDir, `${className}.md`), prettier.format(markdownOutput, { parser: 'mdx' }));
}
function documentOptionsFile(data) {
return `${documentHeader(data)}
${data.description}
Option | Type | Default | Description
-------|------|-------------|---------
${documentOptionsProperties(data)}
`;
}
function documentOptionsProperties(data) {
return data.properties
.map(
(prop) =>
`${prop.name || ''} | ${data.properties[0].type.names.join('|')} | ${prop.defaultvalue || ''} | ${
prop.description || ''
}`,
)
.join('\n');
}
function documentClassFile(data) {
return `${documentHeader(data)}
${data.examples ? documentExamples(data) : ''}
${data.description || ''}
${data.deprecated ? documentDeprecated(data) : ''}
${data.params ? documentParams(data) : ''}
${data.returns ? documentReturn(data) : ''}
`;
}
function documentHeader(data) {
if (data.deprecated) return `## ~~\`${data.id}\`~~`;
return `## \`${data.id}\``;
}
function documentExamples(data) {
return `\`\`\`
${data.examples}
\`\`\`
`;
}
function documentDeprecated(data) {
return `:::danger Deprecated
${data.deprecated}
:::
`;
}
function documentParams(data) {
const params = data.params.map(
(param) => `- \`${param.name}\` (\`${param.type.names.join('|')}\`)
${param.description}
`,
);
return `### Parameters
${params.join('\n')}
`;
}
function documentReturn(data) {
return `### Return Value
- (\`${data.returns[0].type.names.join('|')}\`)
${data.returns[0].description}
`;
}
// Main Script
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
for (const file of publicClassFiles) {
generateClassMarkdown(file);
}
for (const file of publicTypedefFiles) {
generateTypedefMarkdown(file);
}