-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathgenerate.mjs
More file actions
58 lines (44 loc) · 1.49 KB
/
generate.mjs
File metadata and controls
58 lines (44 loc) · 1.49 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
'use strict';
import { readFile } from 'node:fs/promises';
import { extname } from 'node:path';
import { globSync } from 'tinyglobby';
import { VFile } from 'vfile';
import getConfig from '../../utils/configuration/index.mjs';
import createQueries from '../../utils/queries/index.mjs';
import { getRemark } from '../../utils/remark.mjs';
const { updateStabilityPrefixToLink } = createQueries();
const remarkProcessor = getRemark();
/**
* Process a chunk of markdown files in a worker thread.
* Loads and parses markdown files into AST representations.
*
* @type {import('./types').Generator['processChunk']}
*/
export async function processChunk(inputSlice, itemIndices) {
const filePaths = itemIndices.map(idx => inputSlice[idx]);
const results = [];
for (const path of filePaths) {
const vfile = new VFile({ path, value: await readFile(path, 'utf-8') });
updateStabilityPrefixToLink(vfile);
results.push({
tree: remarkProcessor.parse(vfile),
file: { stem: vfile.stem, basename: vfile.basename },
});
}
return results;
}
/**
* Generates AST trees from markdown input files.
*
* @type {import('./types').Generator['generate']}
*/
export async function* generate(_, worker) {
const { ast: config } = getConfig();
const files = globSync(config.input, { ignore: config.ignore }).filter(
p => extname(p) === '.md'
);
// Parse markdown files in parallel using worker threads
for await (const chunkResult of worker.stream(files)) {
yield chunkResult;
}
}