Skip to content

Commit f75eaa3

Browse files
committed
feat(types): support type index
1 parent 3291812 commit f75eaa3

File tree

7 files changed

+44
-9
lines changed

7 files changed

+44
-9
lines changed

bin/commands/generate.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { publicGenerators } from '../../src/generators/index.mjs';
1111
import createGenerator from '../../src/generators.mjs';
1212
import { parseChangelog, parseIndex } from '../../src/parsers/markdown.mjs';
13+
import { loadFromURL } from '../../src/utils/parser.mjs';
1314
import { loadAndParse } from '../utils.mjs';
1415

1516
const availableGenerators = Object.keys(publicGenerators);
@@ -21,6 +22,7 @@ const availableGenerators = Object.keys(publicGenerators);
2122
* @property {Array<keyof publicGenerators>} target - Specifies the generator target mode.
2223
* @property {string} version - Specifies the target Node.js version.
2324
* @property {string} changelog - Specifies the path to the Node.js CHANGELOG.md file.
25+
* @property {string} typeMap - Specifies the path to the Node.js Type Map.
2426
* @property {string} [gitRef] - Git ref/commit URL.
2527
* @property {number} [threads] - Number of threads to allow.
2628
*/
@@ -112,6 +114,14 @@ export default {
112114
type: 'text',
113115
},
114116
},
117+
typeMap: {
118+
flags: ['--type-map <path>'],
119+
desc: 'The mapping of types to links',
120+
prompt: {
121+
message: 'Path to doc/api/type_map.json',
122+
type: 'text',
123+
},
124+
},
115125
},
116126
/**
117127
* Handles the action for generating API docs
@@ -121,6 +131,11 @@ export default {
121131
async action(opts) {
122132
const docs = await loadAndParse(opts.input, opts.ignore);
123133
const releases = await parseChangelog(opts.changelog);
134+
135+
const typeMap = opts.typeMap
136+
? JSON.parse(await loadFromURL(opts.typeMap))
137+
: undefined;
138+
124139
const index = opts.index && (await parseIndex(opts.index));
125140

126141
const { runGenerators } = createGenerator(docs);
@@ -134,6 +149,7 @@ export default {
134149
gitRef: opts.gitRef,
135150
threads: parseInt(opts.threads, 10),
136151
index,
152+
typeMap,
137153
});
138154
},
139155
};

src/generators/metadata/index.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export default {
2020

2121
/**
2222
* @param {Input} inputs
23+
* @param {GeneratorOptions} options
2324
* @returns {Promise<Array<ApiDocMetadataEntry>>}
2425
*/
25-
async generate(inputs) {
26-
return inputs.flatMap(input => parseApiDoc(input));
26+
async generate(inputs, { typeMap }) {
27+
return inputs.flatMap(input => parseApiDoc(input, typeMap));
2728
},
2829
};

src/generators/metadata/utils/parse.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import { getRemark } from '../../../utils/remark.mjs';
1515
* This generator generates a flattened list of metadata entries from a API doc
1616
*
1717
* @param {ParserOutput<import('mdast').Root>} input
18+
* @param {Record<string, string>} typeMap
1819
* @returns {Promise<Array<ApiDocMetadataEntry>>}
1920
*/
20-
export const parseApiDoc = ({ file, tree }) => {
21+
export const parseApiDoc = ({ file, tree }, typeMap) => {
2122
/**
2223
* This holds references to all the Metadata entries for a given file
2324
* this is used so we can traverse the AST tree and keep mutating things
@@ -38,7 +39,7 @@ export const parseApiDoc = ({ file, tree }) => {
3839
updateUnixManualReference,
3940
updateLinkReference,
4041
addStabilityMetadata,
41-
} = createQueries();
42+
} = createQueries(typeMap);
4243

4344
// Creates an instance of the Remark processor with GFM support
4445
// which is used for stringifying the AST tree back to Markdown

src/generators/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ declare global {
4242

4343
// The number of threads the process is allowed to use
4444
threads: number;
45+
46+
// The type map
47+
typeMap: Record<string, string>;
4548
}
4649

4750
export interface GeneratorMetadata<I extends any, O extends any> {

src/utils/parser/__tests__/index.test.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ describe('transformTypeToReferenceLink', () => {
2929
'[`<prefix.Type>`](prefix.html#class-prefixtype)'
3030
);
3131
});
32+
33+
it('should accept a type map', () => {
34+
strictEqual(
35+
transformTypeToReferenceLink('SomeOtherType', {
36+
SomeOtherType: 'fromTypeMap',
37+
}),
38+
'[`<SomeOtherType>`](fromTypeMap)'
39+
);
40+
});
3241
});
3342

3443
describe('normalizeYamlSyntax', () => {

src/utils/parser/index.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ export const transformUnixManualToLink = (
6666
* that link to the actual relevant reference for such type (either internal or external link)
6767
*
6868
* @param {string} type The plain type to be transformed into a Markdown link
69+
* @param {Record<string, string>} record The mapping of types to links
6970
* @returns {string} The Markdown link as a string (formatted in Markdown)
7071
*/
71-
export const transformTypeToReferenceLink = type => {
72+
export const transformTypeToReferenceLink = (
73+
type,
74+
record = DOC_TYPES_MAPPING_NODE_MODULES
75+
) => {
7276
// Removes the wrapping tags that wrap the type references such as `<>` and `{}`
7377
const typeInput = type.replace(/[{}<>]/g, '');
7478

@@ -100,8 +104,8 @@ export const transformTypeToReferenceLink = type => {
100104

101105
// Transform Node.js type/module references into Markdown links
102106
// that refer to other API docs pages within the Node.js API docs
103-
if (lookupPiece in DOC_TYPES_MAPPING_NODE_MODULES) {
104-
return DOC_TYPES_MAPPING_NODE_MODULES[lookupPiece];
107+
if (lookupPiece in record) {
108+
return record[lookupPiece];
105109
}
106110

107111
// Transform Node.js types like 'vm.Something'.

src/utils/queries/index.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import { transformNodesToString } from '../unist.mjs';
2020
/**
2121
* Creates an instance of the Query Manager, which allows to do multiple sort
2222
* of metadata and content metadata manipulation within an API Doc
23+
* @param {Record<string, string>} typeMap The mapping to types to links
2324
*/
24-
const createQueries = () => {
25+
const createQueries = typeMap => {
2526
const remark = getRemark();
2627
/**
2728
* Sanitizes the YAML source by returning the inner YAML content
@@ -179,7 +180,7 @@ const createQueries = () => {
179180
updateTypeReference: (...args) =>
180181
updateReferences(
181182
createQueries.QUERIES.normalizeTypes,
182-
transformTypeToReferenceLink,
183+
type => transformTypeToReferenceLink(type, typeMap),
183184
...args
184185
),
185186
/** @param {Array<import('@types/mdast').Node>} args */

0 commit comments

Comments
 (0)