-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathgenerate.mjs
More file actions
90 lines (77 loc) · 2.42 KB
/
generate.mjs
File metadata and controls
90 lines (77 loc) · 2.42 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
import { buildSideBarProps } from './utils/buildBarProps.mjs';
import buildContent from './utils/buildContent.mjs';
import { getSortedHeadNodes } from './utils/getSortedHeadNodes.mjs';
import { groupNodesByModule } from '../../utils/generators.mjs';
import { getRemarkRecma } from '../../utils/remark.mjs';
import { relative } from '../../utils/url.mjs';
const remarkRecma = getRemarkRecma();
/**
* 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.
*
* @type {import('./types').Generator['processChunk']}
*/
export async function processChunk(
slicedInput,
itemIndices,
{ docPages, stabilityOverviewEntries }
) {
const results = [];
for (const idx of itemIndices) {
const { head, entries } = slicedInput[idx];
const sideBarProps = buildSideBarProps(
head,
docPages.map(([heading, path]) => [
heading,
head.path === path
? `${head.basename}.html`
: `${relative(path, head.path)}.html`,
])
);
const content = await buildContent(
entries,
head,
sideBarProps,
remarkRecma,
stabilityOverviewEntries
);
results.push(content);
}
return results;
}
/**
* Generates a JSX AST
*
* @type {import('./types').Generator['generate']}
*/
export async function* generate(input, worker) {
const groupedModules = groupNodesByModule(input);
const headNodes = getSortedHeadNodes(input);
const docPages = headNodes.map(node => [node.heading.data.name, node.path]);
// Pre-compute stability overview data once — avoid serialising full AST nodes to workers
const stabilityOverviewEntries = headNodes
.filter(node => node.stability)
.map(({ api, heading, stability }) => {
return {
api,
name: heading.data.name,
stabilityIndex: parseInt(stability.data.index, 10),
stabilityDescription: stability.data.description.split('. ')[0],
};
});
// 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, {
docPages,
stabilityOverviewEntries,
})) {
yield chunkResult;
}
}