Skip to content

Commit 5c1da3a

Browse files
committed
fix: ignore and json gen improvements
1 parent d724766 commit 5c1da3a

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

bin/commands/generate.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export default {
148148
await runGenerators({
149149
generators: opts.target,
150150
input: opts.input,
151+
ignore: opts.ignore,
151152
output: opts.output && resolve(opts.output),
152153
version: coerce(opts.version),
153154
releases: await parseChangelog(opts.changelog),

src/generators/ast-js/index.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ export default {
5656
* @param {Partial<GeneratorOptions>} options
5757
* @returns {AsyncGenerator<Output>}
5858
*/
59-
async *generate(_, { input = [], worker }) {
60-
const source = globSync(input).filter(path => extname(path) === '.js');
59+
async *generate(_, { input = [], ignore, worker }) {
60+
const toIgnore = globSync(ignore);
61+
62+
const files = globSync(input)
63+
.filter(path => extname(path) === '.js')
64+
.filter(path => !toIgnore.includes(path));
6165

6266
// Parse the Javascript sources into ASTs in parallel using worker threads
6367
// source is both the items list and the fullInput since we use sliceInput
64-
for await (const chunkResult of worker.stream(source, source)) {
68+
for await (const chunkResult of worker.stream(files, files)) {
6569
yield chunkResult;
6670
}
6771
},

src/generators/ast/index.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ export default {
5555
},
5656

5757
/**
58-
* Generates AST trees from markdown input files.
58+
* Generates AST trees from markdown input fileAs.
5959
*
6060
* @param {Input} _ - Unused (top-level generator)
6161
* @param {Partial<GeneratorOptions>} options
6262
* @returns {AsyncGenerator<Output>}
6363
*/
64-
async *generate(_, { input = [], worker }) {
65-
const files = globSync(input).filter(path => extname(path) === '.md');
64+
async *generate(_, { input = [], ignore = [], worker }) {
65+
const toIgnore = globSync(ignore);
66+
67+
const files = globSync(input)
68+
.filter(path => extname(path) === '.md')
69+
.filter(path => !toIgnore.includes(path));
6670

6771
// Parse markdown files in parallel using worker threads
6872
for await (const chunkResult of worker.stream(files, files)) {

src/generators/legacy-json-all/index.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default {
2929
* @param {Partial<GeneratorOptions>} options
3030
* @returns {Promise<Output>}
3131
*/
32-
async generate(input, { output }) {
32+
async generate(input, { output, index }) {
3333
/**
3434
* The consolidated output object that will contain
3535
* combined data from all sections in the input.
@@ -49,8 +49,26 @@ export default {
4949
*/
5050
const propertiesToCopy = Object.keys(generatedValue);
5151

52+
// Create a map of api name to index position for sorting
53+
const indexOrder = new Map(
54+
index?.map(({ api }, position) => [`doc/api/${api}.md`, position]) ?? []
55+
);
56+
57+
// Sort input by index order (documents not in index go to the end)
58+
const sortedInput = [...input].sort((a, b) => {
59+
const aOrder = indexOrder.get(a.source) ?? Infinity;
60+
const bOrder = indexOrder.get(b.source) ?? Infinity;
61+
62+
return aOrder - bOrder;
63+
});
64+
5265
// Aggregate all sections into the output
53-
for (const section of input) {
66+
for (const section of sortedInput) {
67+
// Skip index.json - it has no useful content, just navigation
68+
if (section.api === 'index') {
69+
continue;
70+
}
71+
5472
for (const property of propertiesToCopy) {
5573
const items = section[property];
5674

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ export const createSectionBuilder = () => {
4545
removed_in = [],
4646
changes,
4747
}) => {
48-
const meta = { changes };
48+
const meta = {};
4949

5050
if (added_in?.length) {
5151
meta.added = enforceArray(added_in);
5252
}
5353

54+
meta.changes = changes;
55+
5456
if (n_api_version?.length) {
5557
meta.napiVersion = enforceArray(n_api_version);
5658
}
@@ -81,8 +83,8 @@ export const createSectionBuilder = () => {
8183
const section = {
8284
textRaw: transformNodesToString(head.children),
8385
name: head.data.name,
84-
type: head.data.type,
8586
introduced_in: entry.introduced_in,
87+
type: head.data.type,
8688
};
8789

8890
const meta = createMeta(entry);
@@ -135,8 +137,11 @@ export const createSectionBuilder = () => {
135137
* @param {import('../../types.d.ts').NodeWithData} heading - The heading node of the section.
136138
*/
137139
const addAdditionalMetadata = (section, parent, heading) => {
138-
if (!section.type) {
140+
if (!section.type || section.type === 'module') {
139141
section.name = section.textRaw.toLowerCase().trim().replace(/\s+/g, '_');
142+
}
143+
144+
if (!section.type) {
140145
section.displayName = heading.data.name;
141146
section.type = parent.type === 'misc' ? 'misc' : 'module';
142147
}

src/generators/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ declare global {
3737
// be a glob when passed to a generator.
3838
input: string | string[];
3939

40+
// The path or glob patterns used to ignore files from the input source files.
41+
ignore?: string | string[];
42+
4043
// The path used to output generated files, this is to be considered
4144
// the base path that any generator will use for generating files
4245
// This parameter accepts globs but when passed to generators will contain

0 commit comments

Comments
 (0)