-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathindex.mjs
More file actions
83 lines (66 loc) · 2.27 KB
/
index.mjs
File metadata and controls
83 lines (66 loc) · 2.27 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
import { buildSideBarProps } from './utils/buildBarProps.mjs';
import buildContent from './utils/buildContent.mjs';
import { getSortedHeadNodes } from './utils/getSortedHeadNodes.mjs';
import getConfig from '../../utils/configuration/index.mjs';
import { groupNodesByModule } from '../../utils/generators.mjs';
import { getRemarkRecma } from '../../utils/remark.mjs';
const remarkRecma = getRemarkRecma();
/**
* Generator for converting MDAST to JSX AST.
*
* @type {import('./types').Generator}
*/
export default {
name: 'jsx-ast',
version: '1.0.0',
description: 'Generates JSX AST from the input MDAST',
dependsOn: 'metadata',
defaultConfiguration: {
ref: 'main',
remoteConfig: 'https://nodejs.org/site.json',
},
/**
* Process a chunk of items in a worker thread.
* Transforms metadata entries into JSX AST nodes.
*
* Each item is a SlicedModuleInput containing the head node
* and all entries for that module - no need to recompute grouping.
*/
async processChunk(slicedInput, itemIndices, docPages) {
const results = [];
for (const idx of itemIndices) {
const { head, entries } = slicedInput[idx];
const sideBarProps = buildSideBarProps(head, docPages);
const content = await buildContent(
entries,
head,
sideBarProps,
remarkRecma
);
results.push(content);
}
return results;
},
/**
* Generates a JSX AST
*/
async *generate(input, worker) {
const config = getConfig('jsx-ast');
const groupedModules = groupNodesByModule(input);
const headNodes = getSortedHeadNodes(input);
// Pre-compute docPages once in main thread
// TODO(@avivkeller): Load the index file here instead of during configuration
const docPages = config.index
? config.index.map(({ section, api }) => [section, `${api}.html`])
: headNodes.map(node => [node.heading.data.name, `${node.api}.html`]);
// Create sliced input: each item contains head + its module's entries
// This avoids sending all 4700+ entries to every worker
const entries = headNodes.map(head => ({
head,
entries: groupedModules.get(head.api),
}));
for await (const chunkResult of worker.stream(entries, entries, docPages)) {
yield chunkResult;
}
},
};