-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathgenerate.mjs
More file actions
72 lines (59 loc) · 1.92 KB
/
generate.mjs
File metadata and controls
72 lines (59 loc) · 1.92 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
'use strict';
import { readFile } from 'node:fs/promises';
import { relative, sep } from 'node:path/posix';
import globParent from 'glob-parent';
import { globSync } from 'tinyglobby';
import { STABILITY_INDEX_URL } from './constants.mjs';
import getConfig from '../../utils/configuration/index.mjs';
import { withExt } from '../../utils/file.mjs';
import { QUERIES } from '../../utils/queries/index.mjs';
import { getRemark as remark } from '../../utils/remark.mjs';
/**
* 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, parent] of filePaths) {
const content = await readFile(path, 'utf-8');
const value = content
.replace(
QUERIES.standardYamlFrontmatter,
(_, yaml) => '<!-- YAML\n' + yaml + '\n-->\n'
)
.replace(
QUERIES.stabilityIndexPrefix,
match => `[${match}](${STABILITY_INDEX_URL})`
);
const relativePath = sep + withExt(relative(parent, path));
results.push({
tree: remark().parse(value),
// The path is the relative path minus the extension
path: relativePath,
fullPath: path,
});
}
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 = config.input.flatMap(input => {
const parent = globParent(input);
return globSync(input, { ignore: config.ignore }).map(child => [
child,
parent,
]);
});
// Parse markdown files in parallel using worker threads
for await (const chunkResult of worker.stream(files)) {
yield chunkResult;
}
}