Skip to content

Commit 3a70577

Browse files
committed
chore: minor improvements
1 parent 9828ed2 commit 3a70577

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

src/generators/ast-js/index.mjs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import createJsLoader from '../../loaders/javascript.mjs';
66
import createJsParser from '../../parsers/javascript.mjs';
77

88
const { loadFiles } = createJsLoader();
9+
910
const { parseJsSource } = createJsParser();
1011

1112
/**
@@ -38,17 +39,11 @@ export default {
3839
* @returns {Promise<object[]>} Parsed JS AST objects for each file
3940
*/
4041
async processChunk(inputSlice, itemIndices) {
41-
const results = [];
42-
43-
for (const idx of itemIndices) {
44-
const [file] = loadFiles(inputSlice[idx]);
42+
const filePaths = itemIndices.map(idx => inputSlice[idx]);
4543

46-
const parsedFile = await parseJsSource(file);
47-
48-
results.push(parsedFile);
49-
}
44+
const vfiles = await Promise.all(loadFiles(filePaths));
5045

51-
return results;
46+
return Promise.all(vfiles.map(parseJsSource));
5247
},
5348

5449
/**

src/generators/ast/index.mjs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,15 @@
11
'use strict';
22

3-
import { readFile } from 'node:fs/promises';
43
import { extname } from 'node:path';
54

65
import { globSync } from 'glob';
7-
import { VFile } from 'vfile';
86

9-
import createQueries from '../../utils/queries/index.mjs';
7+
import createLoader from '../../loaders/markdown.mjs';
108
import { getRemark } from '../../utils/remark.mjs';
119

12-
const remarkProcessor = getRemark();
13-
14-
const { updateStabilityPrefixToLink } = createQueries();
15-
16-
/**
17-
* Parses a single markdown file into an AST.
18-
*
19-
* @param {string} filePath - Path to the markdown file
20-
* @returns {Promise<ParserOutput<import('mdast').Root>>}
21-
*/
22-
const parseMarkdownFile = async filePath => {
23-
const fileContents = await readFile(filePath, 'utf-8');
24-
25-
const vfile = new VFile({ path: filePath, value: fileContents });
10+
const { loadFiles } = createLoader();
2611

27-
// Normalizes all the Stability Index prefixes with Markdown links
28-
updateStabilityPrefixToLink(vfile);
29-
30-
// Parses the API doc into an AST tree using `unified` and `remark`
31-
const tree = remarkProcessor.parse(vfile);
32-
33-
return { file: { stem: vfile.stem, basename: vfile.basename }, tree };
34-
};
12+
const remarkProcessor = getRemark();
3513

3614
/**
3715
* This generator parses Markdown API doc files into AST trees.
@@ -57,15 +35,17 @@ export default {
5735
* @returns {Promise<Array<ParserOutput<import('mdast').Root>>>}
5836
*/
5937
async processChunk(inputSlice, itemIndices) {
60-
const results = [];
38+
const filePaths = itemIndices.map(idx => inputSlice[idx]);
6139

62-
for (const idx of itemIndices) {
63-
const parsed = await parseMarkdownFile(inputSlice[idx]);
40+
const vfiles = await Promise.all(loadFiles(filePaths));
6441

65-
results.push(parsed);
66-
}
42+
return vfiles.map(vfile => {
43+
const tree = remarkProcessor.parse(vfile);
44+
45+
const minimalVfile = { stem: vfile.stem, basename: vfile.basename };
6746

68-
return results;
47+
return { file: minimalVfile, tree };
48+
});
6949
},
7050

7151
/**

src/loaders/markdown.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
import { readFile } from 'node:fs/promises';
4+
import { extname } from 'node:path';
5+
6+
import { globSync } from 'glob';
7+
import { VFile } from 'vfile';
8+
9+
import createQueries from '../utils/queries/index.mjs';
10+
11+
const { updateStabilityPrefixToLink } = createQueries();
12+
13+
/**
14+
* This creates a "loader" for loading Markdown API doc files into VFiles.
15+
*/
16+
const createLoader = () => {
17+
/**
18+
* Loads Markdown source files and transforms them into VFiles.
19+
* Applies stability index normalization during load.
20+
*
21+
* @param {string | string[]} searchPath - Glob pattern(s) or file paths
22+
* @returns {Promise<VFile>[]} Array of promises resolving to VFiles
23+
*/
24+
const loadFiles = searchPath => {
25+
const resolvedFiles = globSync(searchPath).filter(
26+
filePath => extname(filePath) === '.md'
27+
);
28+
29+
return resolvedFiles.map(async filePath => {
30+
const fileContents = await readFile(filePath, 'utf-8');
31+
32+
const vfile = new VFile({ path: filePath, value: fileContents });
33+
34+
// Normalizes all the Stability Index prefixes with Markdown links
35+
updateStabilityPrefixToLink(vfile);
36+
37+
return vfile;
38+
});
39+
};
40+
41+
return { loadFiles };
42+
};
43+
44+
export default createLoader;

0 commit comments

Comments
 (0)