Skip to content

Commit 8375d8c

Browse files
authored
feat(types): support type index (#463)
* feat(types): support type index
1 parent f132fcb commit 8375d8c

File tree

11 files changed

+172
-214
lines changed

11 files changed

+172
-214
lines changed

bin/commands/generate.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ 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 { DEFAULT_TYPE_MAP } from '../../src/utils/parser/constants.mjs';
14+
import { loadFromURL } from '../../src/utils/parser.mjs';
1315
import { loadAndParse } from '../utils.mjs';
1416

1517
const availableGenerators = Object.keys(publicGenerators);
@@ -21,6 +23,7 @@ const availableGenerators = Object.keys(publicGenerators);
2123
* @property {Array<keyof publicGenerators>} target - Specifies the generator target mode.
2224
* @property {string} version - Specifies the target Node.js version.
2325
* @property {string} changelog - Specifies the path to the Node.js CHANGELOG.md file.
26+
* @property {string} typeMap - Specifies the path to the Node.js Type Map.
2427
* @property {string} [gitRef] - Git ref/commit URL.
2528
* @property {number} [threads] - Number of threads to allow.
2629
*/
@@ -112,6 +115,15 @@ export default {
112115
type: 'text',
113116
},
114117
},
118+
typeMap: {
119+
flags: ['--type-map <path>'],
120+
desc: 'The mapping of types to links',
121+
prompt: {
122+
message: 'Path to doc/api/type_map.json',
123+
type: 'text',
124+
initialValue: DEFAULT_TYPE_MAP,
125+
},
126+
},
115127
},
116128
/**
117129
* Handles the action for generating API docs
@@ -121,6 +133,10 @@ export default {
121133
async action(opts) {
122134
const docs = await loadAndParse(opts.input, opts.ignore);
123135
const releases = await parseChangelog(opts.changelog);
136+
137+
const rawTypeMap = await loadFromURL(opts.typeMap);
138+
const typeMap = JSON.parse(rawTypeMap);
139+
124140
const index = opts.index && (await parseIndex(opts.index));
125141

126142
const { runGenerators } = createGenerator(docs);
@@ -134,6 +150,7 @@ export default {
134150
gitRef: opts.gitRef,
135151
threads: parseInt(opts.threads, 10),
136152
index,
153+
typeMap,
137154
});
138155
},
139156
};

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
@@ -16,9 +16,10 @@ import { IGNORE_STABILITY_STEMS } from '../constants.mjs';
1616
* This generator generates a flattened list of metadata entries from a API doc
1717
*
1818
* @param {ParserOutput<import('mdast').Root>} input
19+
* @param {Record<string, string>} typeMap
1920
* @returns {Promise<Array<ApiDocMetadataEntry>>}
2021
*/
21-
export const parseApiDoc = ({ file, tree }) => {
22+
export const parseApiDoc = ({ file, tree }, typeMap) => {
2223
/**
2324
* This holds references to all the Metadata entries for a given file
2425
* this is used so we can traverse the AST tree and keep mutating things
@@ -39,7 +40,7 @@ export const parseApiDoc = ({ file, tree }) => {
3940
updateUnixManualReference,
4041
updateLinkReference,
4142
addStabilityMetadata,
42-
} = createQueries();
43+
} = createQueries(typeMap);
4344

4445
// Creates an instance of the Remark processor with GFM support
4546
// 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.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const loadFromURL = async url => {
1212

1313
if (!parsedUrl || parsedUrl.protocol === 'file:') {
1414
// Load from file system
15-
return readFile(url, 'utf-8');
15+
return readFile(parsedUrl ?? url, 'utf-8');
1616
} else {
1717
// Load from network
1818
const response = await fetch(parsedUrl);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ describe('transformTypeToReferenceLink', () => {
2323
);
2424
});
2525

26-
it('should transform a prefixed type into a Markdown link', () => {
26+
it('should transform a type into a Markdown link', () => {
2727
strictEqual(
28-
transformTypeToReferenceLink('prefix.Type'),
29-
'[`<prefix.Type>`](prefix.html#class-prefixtype)'
28+
transformTypeToReferenceLink('SomeOtherType', {
29+
SomeOtherType: 'fromTypeMap',
30+
}),
31+
'[`<SomeOtherType>`](fromTypeMap)'
3032
);
3133
});
3234
});

src/utils/parser/constants.mjs

Lines changed: 3 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import globals from 'globals';
22

3+
// The default type map path
4+
export const DEFAULT_TYPE_MAP = import.meta.resolve('./typeMap.json');
5+
36
// These are string replacements specific to Node.js API docs for anchor IDs
47
export const DOC_API_SLUGS_REPLACEMENTS = [
58
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
@@ -95,199 +98,6 @@ export const DOC_TYPES_MAPPING_GLOBALS = {
9598
'WebAssembly.Instance': 'WebAssembly/Instance',
9699
};
97100

98-
// This is a mapping for types within the Markdown content and their respective
99-
// Node.js types within the Node.js API docs (refers to a different API doc page)
100-
// Note: These hashes are generated with the GitHub Slugger
101-
export const DOC_TYPES_MAPPING_NODE_MODULES = {
102-
AbortController: 'globals.html#class-abortcontroller',
103-
AbortSignal: 'globals.html#class-abortsignal',
104-
105-
Algorithm: 'webcrypto.html#class-algorithm',
106-
AlgorithmIdentifier: 'webcrypto.html#class-algorithmidentifier',
107-
AsyncHook: 'async_hooks.html#async_hookscreatehookcallbacks',
108-
AsyncLocalStorage: 'async_context.html#class-asynclocalstorage',
109-
AsyncResource: 'async_hooks.html#class-asyncresource',
110-
111-
AesCbcParams: 'webcrypto.html#class-aescbcparams',
112-
AesCtrParams: 'webcrypto.html#class-aesctrparams',
113-
AesGcmParams: 'webcrypto.html#class-aesgcmparams',
114-
AesKeyAlgorithm: 'webcrypto.html#class-aeskeyalgorithm',
115-
AesKeyGenParams: 'webcrypto.html#class-aeskeygenparams',
116-
AesDerivedKeyParams: 'webcrypto.html#class-aesderivedkeyparams',
117-
118-
Blob: 'buffer.html#class-blob',
119-
BroadcastChannel:
120-
'worker_threads.html#class-broadcastchannel-extends-eventtarget',
121-
Buffer: 'buffer.html#class-buffer',
122-
123-
ByteLengthQueuingStrategy: 'webstreams.html#class-bytelengthqueuingstrategy',
124-
125-
Channel: 'diagnostics_channel.html#class-channel',
126-
ChildProcess: 'child_process.html#class-childprocess',
127-
Cipher: 'crypto.html#class-cipher',
128-
Cipheriv: 'crypto.html#class-cipheriv',
129-
Decipheriv: 'crypto.html#class-decipheriv',
130-
ClientHttp2Session: 'http2.html#class-clienthttp2session',
131-
ClientHttp2Stream: 'http2.html#class-clienthttp2stream',
132-
133-
CountQueuingStrategy: 'webstreams.html#class-countqueuingstrategy',
134-
135-
Crypto: 'webcrypto.html#class-crypto',
136-
CryptoKey: 'webcrypto.html#class-cryptokey',
137-
CryptoKeyPair: 'webcrypto.html#class-cryptokeypair',
138-
139-
CustomEvent: 'events.html#class-customevent',
140-
141-
Decipher: 'crypto.html#class-decipher',
142-
DiffieHellman: 'crypto.html#class-diffiehellman',
143-
DiffieHellmanGroup: 'crypto.html#class-diffiehellmangroup',
144-
Domain: 'domain.html#class-domain',
145-
146-
Duplex: 'stream.html#class-streamduplex',
147-
148-
ECDH: 'crypto.html#class-ecdh',
149-
EcdhKeyDeriveParams: 'webcrypto.html#class-ecdhkeyderiveparams',
150-
EcdsaParams: 'webcrypto.html#class-ecdsaparams',
151-
EcKeyAlgorithm: 'webcrypto.html#class-eckeyalgorithm',
152-
EcKeyGenParams: 'webcrypto.html#class-eckeygenparams',
153-
EcKeyImportParams: 'webcrypto.html#class-eckeyimportparams',
154-
Ed448Params: 'webcrypto.html#class-ed448params',
155-
156-
Event: 'events.html#class-event',
157-
EventEmitter: 'events.html#class-eventemitter',
158-
EventListener: 'events.html#event-listener',
159-
EventTarget: 'events.html#class-eventtarget',
160-
161-
File: 'buffer.html#class-file',
162-
FileHandle: 'fs.html#class-filehandle',
163-
164-
Handle: 'net.html#serverlistenhandle-backlog-callback',
165-
Hash: 'crypto.html#class-hash',
166-
Histogram: 'perf_hooks.html#class-histogram',
167-
HkdfParams: 'webcrypto.html#class-hkdfparams',
168-
Hmac: 'crypto.html#class-hmac',
169-
HmacImportParams: 'webcrypto.html#class-hmacimportparams',
170-
HmacKeyAlgorithm: 'webcrypto.html#class-hmackeyalgorithm',
171-
HmacKeyGenParams: 'webcrypto.html#class-hmackeygenparams',
172-
173-
Http2SecureServer: 'http2.html#class-http2secureserver',
174-
Http2Server: 'http2.html#class-http2server',
175-
Http2Session: 'http2.html#class-http2session',
176-
Http2Stream: 'http2.html#class-http2stream',
177-
178-
Immediate: 'timers.html#class-immediate',
179-
180-
IntervalHistogram:
181-
'perf_hooks.html#class-intervalhistogram-extends-histogram',
182-
183-
LockManager: 'worker_threads.html#class-lockmanager',
184-
185-
KeyAlgorithm: 'webcrypto.html#class-keyalgorithm',
186-
KeyObject: 'crypto.html#class-keyobject',
187-
188-
MIMEParams: 'util.html#class-utilmimeparams',
189-
MessagePort: 'worker_threads.html#class-messageport',
190-
191-
MockModuleContext: 'test.html#class-mockmodulecontext',
192-
193-
NodeEventTarget: 'events.html#class-nodeeventtarget',
194-
195-
Pbkdf2Params: 'webcrypto.html#class-pbkdf2params',
196-
PerformanceEntry: 'perf_hooks.html#class-performanceentry',
197-
PerformanceNodeTiming: 'perf_hooks.html#class-performancenodetiming',
198-
PerformanceObserver: 'perf_hooks.html#class-performanceobserver',
199-
PerformanceObserverEntryList:
200-
'perf_hooks.html#class-performanceobserverentrylist',
201-
202-
Readable: 'stream.html#class-streamreadable',
203-
ReadableByteStreamController:
204-
'webstreams.html#class-readablebytestreamcontroller',
205-
ReadableStream: 'webstreams.html#class-readablestream',
206-
ReadableStreamBYOBReader: 'webstreams.html#class-readablestreambyobreader',
207-
ReadableStreamBYOBRequest: 'webstreams.html#class-readablestreambyobrequest',
208-
ReadableStreamDefaultController:
209-
'webstreams.html#class-readablestreamdefaultcontroller',
210-
ReadableStreamDefaultReader:
211-
'webstreams.html#class-readablestreamdefaultreader',
212-
213-
ModuleRequest: 'vm.html#type-modulerequest',
214-
215-
DatabaseSync: 'sqlite.html#class-databasesync',
216-
217-
RecordableHistogram:
218-
'perf_hooks.html#class-recordablehistogram-extends-histogram',
219-
220-
RsaHashedKeyAlgorithm: 'webcrypto.html#class-rsahashedkeyalgorithm',
221-
RsaHashedImportParams: 'webcrypto.html#class-rsahashedimportparams',
222-
RsaHashedKeyGenParams: 'webcrypto.html#class-rsahashedkeygenparams',
223-
RsaOaepParams: 'webcrypto.html#class-rsaoaepparams',
224-
RsaPssParams: 'webcrypto.html#class-rsapssparams',
225-
226-
ServerHttp2Session: 'http2.html#class-serverhttp2session',
227-
ServerHttp2Stream: 'http2.html#class-serverhttp2stream',
228-
229-
Sign: 'crypto.html#class-sign',
230-
231-
Disposable:
232-
'https://tc39.es/proposal-explicit-resource-management/#sec-disposable-interface',
233-
234-
Session: 'sqlite.html#class-session',
235-
StatementSync: 'sqlite.html#class-statementsync',
236-
237-
Stream: 'stream.html#stream',
238-
239-
SubtleCrypto: 'webcrypto.html#class-subtlecrypto',
240-
241-
TestsStream: 'test.html#class-testsstream',
242-
243-
TextDecoderStream: 'webstreams.html#class-textdecoderstream',
244-
TextEncoderStream: 'webstreams.html#class-textencoderstream',
245-
246-
Timeout: 'timers.html#class-timeout',
247-
Timer: 'timers.html#timers',
248-
249-
Tracing: 'tracing.html#tracing-object',
250-
TracingChannel: 'diagnostics_channel.html#class-tracingchannel',
251-
252-
Transform: 'stream.html#class-streamtransform',
253-
TransformStream: 'webstreams.html#class-transformstream',
254-
TransformStreamDefaultController:
255-
'webstreams.html#class-transformstreamdefaultcontroller',
256-
257-
URL: 'url.html#the-whatwg-url-api',
258-
URLSearchParams: 'url.html#class-urlsearchparams',
259-
260-
Verify: 'crypto.html#class-verify',
261-
262-
Writable: 'stream.html#class-streamwritable',
263-
WritableStream: 'webstreams.html#class-writablestream',
264-
WritableStreamDefaultController:
265-
'webstreams.html#class-writablestreamdefaultcontroller',
266-
WritableStreamDefaultWriter:
267-
'webstreams.html#class-writablestreamdefaultwriter',
268-
269-
Worker: 'worker_threads.html#class-worker',
270-
271-
X509Certificate: 'crypto.html#class-x509certificate',
272-
273-
'brotli options': 'zlib.html#class-brotlioptions',
274-
275-
'import.meta': 'esm.html#importmeta',
276-
277-
'os.constants.dlopen': 'os.html#dlopen-constants',
278-
279-
'readlinePromises.Interface': 'readline.html#class-readlinepromisesinterface',
280-
281-
require: 'modules.html#requireid',
282-
module: 'modules.html#the-module-object',
283-
284-
'zlib options': 'zlib.html#class-options',
285-
'zstd options': 'zlib.html#class-zstdoptions',
286-
287-
'HTTP/2 Headers Object': 'http2.html#headers-object',
288-
'HTTP/2 Settings Object': 'http2.html#settings-object',
289-
};
290-
291101
// This is a mapping for miscellaneous types within the Markdown content and their respective
292102
// external reference on appropriate 3rd-party vendors/documentation sites.
293103
export const DOC_TYPES_MAPPING_OTHER = {

src/utils/parser/index.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
DOC_MDN_BASE_URL_JS_GLOBALS,
88
DOC_MDN_BASE_URL_JS_PRIMITIVES,
99
DOC_TYPES_MAPPING_GLOBALS,
10-
DOC_TYPES_MAPPING_NODE_MODULES,
1110
DOC_TYPES_MAPPING_OTHER,
1211
DOC_TYPES_MAPPING_PRIMITIVES,
1312
DOC_MAN_BASE_URL,
@@ -66,9 +65,10 @@ export const transformUnixManualToLink = (
6665
* that link to the actual relevant reference for such type (either internal or external link)
6766
*
6867
* @param {string} type The plain type to be transformed into a Markdown link
68+
* @param {Record<string, string>} record The mapping of types to links
6969
* @returns {string} The Markdown link as a string (formatted in Markdown)
7070
*/
71-
export const transformTypeToReferenceLink = type => {
71+
export const transformTypeToReferenceLink = (type, record) => {
7272
// Removes the wrapping tags that wrap the type references such as `<>` and `{}`
7373
const typeInput = type.replace(/[{}<>]/g, '');
7474

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

101101
// Transform Node.js type/module references into Markdown links
102102
// 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];
103+
if (lookupPiece in record) {
104+
return record[lookupPiece];
105105
}
106106

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

0 commit comments

Comments
 (0)