-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgenerate.mjs
More file actions
43 lines (34 loc) · 1.35 KB
/
generate.mjs
File metadata and controls
43 lines (34 loc) · 1.35 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
'use strict';
import { join } from 'node:path';
import { remove } from 'unist-util-remove';
import getConfig from '../../utils/configuration/index.mjs';
import { writeFile } from '../../utils/file.mjs';
import { UNIST } from '../../utils/queries/index.mjs';
/**
* Generates the simplified JSON version of the API docs
*
* @type {import('./types').Generator['generate']}
*/
export async function generate(input) {
const config = getConfig('json-simple');
// Iterates the input (MetadataEntry) and performs a few changes
const mappedInput = input.map(node => {
// Deep clones the content nodes to avoid affecting upstream nodes
const content = JSON.parse(JSON.stringify(node.content));
// Removes numerous nodes from the content that should not be on the "body"
// of the JSON version of the API docs as they are already represented in the metadata
remove(content, [UNIST.isStabilityNode, UNIST.isHeading]);
return { ...node, content };
});
if (config.output) {
// Writes all the API docs stringified content into one file
// Note: The full JSON generator in the future will create one JSON file per top-level API doc file
await writeFile(
join(config.output, 'api-docs.json'),
config.minify
? JSON.stringify(mappedInput)
: JSON.stringify(mappedInput, null, 2)
);
}
return mappedInput;
}