Skip to content

Commit 7f4ee2a

Browse files
committed
fix(types,css): fix type parsing, and css headers
1 parent 7149528 commit 7f4ee2a

File tree

8 files changed

+91
-115
lines changed

8 files changed

+91
-115
lines changed

src/generators/metadata/constants.mjs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/generators/metadata/utils/parse.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { remove } from 'unist-util-remove';
66
import { selectAll } from 'unist-util-select';
77
import { SKIP, visit } from 'unist-util-visit';
88

9-
import { createNodeSlugger } from './slugger.mjs';
109
import createMetadata from '../../../metadata.mjs';
10+
import createNodeSlugger from '../../../utils/parser/slugger.mjs';
1111
import createQueries from '../../../utils/queries/index.mjs';
1212
import { getRemark } from '../../../utils/remark.mjs';
1313

src/generators/metadata/utils/slugger.mjs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/generators/web/ui/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ main {
2929
}
3030

3131
/* Change history positioning */
32-
div:has(> h1, > h2, > h3, > h4) {
32+
div:has(> h1, > h2, > h3, > h4, > h5, > h6) {
3333
display: flex;
3434
align-items: center;
3535
gap: 8px;

src/utils/parser/constants.mjs

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

3+
// These are string replacements specific to Node.js API docs for anchor IDs
4+
export const DOC_API_SLUGS_REPLACEMENTS = [
5+
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
6+
{ from: /&/, to: '-and-' }, // Replace &
7+
{ from: /[/_,:;\\ ]/g, to: '-' }, // Replace /_,:;\. and whitespace
8+
{ from: /--+/g, to: '-' }, // Replace multiple hyphens with single
9+
{ from: /^-/, to: '' }, // Remove any leading hyphen
10+
{ from: /-$/, to: '' }, // Remove any trailing hyphen
11+
];
12+
313
// This is the base URL of the MDN Web documentation
414
export const DOC_MDN_BASE_URL = 'https://developer.mozilla.org/en-US/docs/Web/';
515

@@ -94,6 +104,7 @@ export const DOC_TYPES_MAPPING_GLOBALS = {
94104
'WeakSet',
95105

96106
'TypedArray',
107+
'Float16Array',
97108
'Float32Array',
98109
'Float64Array',
99110
'Int8Array',
@@ -127,6 +138,7 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
127138
AesGcmParams: 'webcrypto.html#class-aesgcmparams',
128139
AesKeyAlgorithm: 'webcrypto.html#class-aeskeyalgorithm',
129140
AesKeyGenParams: 'webcrypto.html#class-aeskeygenparams',
141+
AesDerivedKeyParams: 'webcrypto.html#class-aesderivedkeyparams',
130142

131143
Blob: 'buffer.html#class-blob',
132144
BroadcastChannel:
@@ -138,6 +150,8 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
138150
Channel: 'diagnostics_channel.html#class-channel',
139151
ChildProcess: 'child_process.html#class-childprocess',
140152
Cipher: 'crypto.html#class-cipher',
153+
Cipheriv: 'crypto.html#class-cipheriv',
154+
Decipheriv: 'crypto.html#class-decipheriv',
141155
ClientHttp2Session: 'http2.html#class-clienthttp2session',
142156
ClientHttp2Stream: 'http2.html#class-clienthttp2stream',
143157

@@ -191,6 +205,8 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
191205
IntervalHistogram:
192206
'perf_hooks.html#class-intervalhistogram-extends-histogram',
193207

208+
LockManager: 'worker_threads.html#class-lockmanager',
209+
194210
KeyAlgorithm: 'webcrypto.html#class-keyalgorithm',
195211
KeyObject: 'crypto.html#class-keyobject',
196212

@@ -219,6 +235,10 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
219235
ReadableStreamDefaultReader:
220236
'webstreams.html#class-readablestreamdefaultreader',
221237

238+
ModuleRequest: 'vm.html#type-modulerequest',
239+
240+
DatabaseSync: 'sqlite.html#class-databasesync',
241+
222242
RecordableHistogram:
223243
'perf_hooks.html#class-recordablehistogram-extends-histogram',
224244

@@ -233,6 +253,10 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
233253

234254
Sign: 'crypto.html#class-sign',
235255

256+
Disposable:
257+
'https://tc39.es/proposal-explicit-resource-management/#sec-disposable-interface',
258+
259+
Session: 'sqlite.html#class-session',
236260
StatementSync: 'sqlite.html#class-statementsync',
237261

238262
Stream: 'stream.html#stream',
@@ -273,71 +297,20 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
273297

274298
'brotli options': 'zlib.html#class-brotlioptions',
275299

276-
'cluster.Worker': 'cluster.html#class-worker',
277-
278-
'crypto.constants': 'crypto.html#cryptoconstants',
279-
280-
'dgram.Socket': 'dgram.html#class-dgramsocket',
281-
282-
'errors.Error': 'errors.html#class-error',
283-
284-
'fs.Dir': 'fs.html#class-fsdir',
285-
'fs.Dirent': 'fs.html#class-fsdirent',
286-
'fs.FSWatcher': 'fs.html#class-fsfswatcher',
287-
'fs.ReadStream': 'fs.html#class-fsreadstream',
288-
'fs.StatFs': 'fs.html#class-fsstatfs',
289-
'fs.Stats': 'fs.html#class-fsstats',
290-
'fs.StatWatcher': 'fs.html#class-fsstatwatcher',
291-
'fs.WriteStream': 'fs.html#class-fswritestream',
292-
293-
'http.Agent': 'http.html#class-httpagent',
294-
'http.ClientRequest': 'http.html#class-httpclientrequest',
295-
'http.IncomingMessage': 'http.html#class-httpincomingmessage',
296-
'http.OutgoingMessage': 'http.html#class-httpoutgoingmessage',
297-
'http.Server': 'http.html#class-httpserver',
298-
'http.ServerResponse': 'http.html#class-httpserverresponse',
299-
300-
'http2.Http2ServerRequest': 'http2.html#class-http2http2serverrequest',
301-
'http2.Http2ServerResponse': 'http2.html#class-http2http2serverresponse',
302-
303300
'import.meta': 'esm.html#importmeta',
304301

305-
'module.SourceMap': 'module.html#class-modulesourcemap',
306-
307-
'net.BlockList': 'net.html#class-netblocklist',
308-
'net.Server': 'net.html#class-netserver',
309-
'net.Socket': 'net.html#class-netsocket',
310-
'net.SocketAddress': 'net.html#class-netsocketaddress',
311-
312302
'os.constants.dlopen': 'os.html#dlopen-constants',
313303

314-
'readline.Interface': 'readline.html#class-readlineinterface',
315-
'readline.InterfaceConstructor': 'readline.html#class-interfaceconstructor',
316304
'readlinePromises.Interface': 'readline.html#class-readlinepromisesinterface',
317305

318-
'repl.REPLServer': 'repl.html#class-replserver',
319-
320306
require: 'modules.html#requireid',
321-
322-
'stream.Duplex': 'stream.html#class-streamduplex',
323-
'stream.Readable': 'stream.html#class-streamreadable',
324-
'stream.Transform': 'stream.html#class-streamtransform',
325-
'stream.Writable': 'stream.html#class-streamwritable',
326-
327-
'tls.SecureContext': 'tls.html#tlscreatesecurecontextoptions',
328-
'tls.Server': 'tls.html#class-tlsserver',
329-
'tls.TLSSocket': 'tls.html#class-tlstlssocket',
330-
331-
'tty.ReadStream': 'tty.html#class-ttyreadstream',
332-
'tty.WriteStream': 'tty.html#class-ttywritestream',
333-
334-
'vm.Module': 'vm.html#class-vmmodule',
335-
'vm.Script': 'vm.html#class-vmscript',
336-
'vm.SourceTextModule': 'vm.html#class-vmsourcetextmodule',
337-
'vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER':
338-
'vm.html#vmconstantsuse_main_context_default_loader',
307+
module: 'modules.html#the-module-object',
339308

340309
'zlib options': 'zlib.html#class-options',
310+
'zstd options': 'zlib.html#class-zstdoptions',
311+
312+
'HTTP/2 Headers Object': 'http2.html#headers-object',
313+
'HTTP/2 Settings Object': 'http2.html#settings-object',
341314
};
342315

343316
// This is a mapping for miscellaneous types within the Markdown content and their respective

src/utils/parser/index.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
DOC_TYPES_MAPPING_PRIMITIVES,
1313
DOC_MAN_BASE_URL,
1414
} from './constants.mjs';
15+
import { slug } from './slugger.mjs';
1516
import createQueries from '../queries/index.mjs';
1617

1718
/**
@@ -103,6 +104,14 @@ export const transformTypeToReferenceLink = type => {
103104
return DOC_TYPES_MAPPING_NODE_MODULES[lookupPiece];
104105
}
105106

107+
// Transform Node.js types like 'vm.Something'.
108+
if (lookupPiece.indexOf('.') >= 0) {
109+
const [mod, ...pieces] = lookupPiece.split('.');
110+
const isClass = pieces.at(-1).match(/^[A-Z][a-z]/);
111+
112+
return `${mod}.html#${isClass ? 'class-' : ''}${slug(lookupPiece)}`;
113+
}
114+
106115
return '';
107116
};
108117

src/utils/parser/slugger.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
import GitHubSlugger, { slug as defaultSlugFn } from 'github-slugger';
4+
5+
import { DOC_API_SLUGS_REPLACEMENTS } from './constants.mjs';
6+
7+
/**
8+
* Creates a modified version of the GitHub Slugger
9+
*
10+
* @returns {InstanceType<typeof import('github-slugger').default>} The modified GitHub Slugger
11+
*/
12+
const createNodeSlugger = () => {
13+
const slugger = new GitHubSlugger();
14+
const slugFn = slugger.slug.bind(slugger);
15+
16+
return {
17+
...slugger,
18+
/**
19+
* Creates a new slug based on the provided string
20+
*
21+
* @param {string} title The title to be parsed into a slug
22+
*/
23+
// Applies certain string replacements that are specific
24+
// to the way how Node.js generates slugs/anchor IDs
25+
slug: title => slug(title, slugFn),
26+
};
27+
};
28+
29+
/**
30+
* @param {string} title
31+
* @param {typeof defaultSlugFn} slugFn
32+
*/
33+
export const slug = (title, slugFn = defaultSlugFn) =>
34+
DOC_API_SLUGS_REPLACEMENTS.reduce(
35+
(piece, { from, to }) => piece.replace(from, to),
36+
slugFn(title)
37+
);
38+
39+
export default createNodeSlugger;

src/utils/queries/index.mjs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ createQueries.QUERIES = {
197197
// Fixes the references to Markdown pages into the API documentation
198198
markdownUrl: /^(?![+a-zA-Z]+:)([^#?]+)\.md(#.+)?$/,
199199
// ReGeX to match the {Type}<Type> (API type references)
200-
// eslint-disable-next-line no-useless-escape
201-
normalizeTypes: /(\{|<)(?! )[a-zA-Z0-9.| \[\]\\]+(?! )(\}|>)/g,
200+
201+
normalizeTypes: /(\{|<)(?! )[^<({})>]+(?! )(\}|>)/g,
202202
// ReGex to match the type API type references that got already parsed
203203
// so that they can be transformed into HTML links
204-
linksWithTypes: /\[`<([a-zA-Z0-9.| \\[\]]+)>`\]\((\S+)\)/g,
204+
linksWithTypes: /\[`<[^<({})>]>`\]\((\S+)\)/g,
205205
// ReGeX for handling Stability Indexes Metadata
206206
stabilityIndex: /^Stability: ([0-5](?:\.[0-3])?)(?:\s*-\s*)?(.*)$/s,
207207
// ReGeX for handling the Stability Index Prefix
@@ -291,17 +291,22 @@ createQueries.UNIST = {
291291
return true;
292292
}
293293

294-
// Check for direct type link pattern (starts with '<')
295-
if (node.type === 'link' && node.children?.[0]?.value?.[0] === '<') {
294+
// Check for direct type link pattern (starts with '<'), followed by a space
295+
if (
296+
node.type === 'link' &&
297+
node.children?.[0]?.value?.[0] === '<' &&
298+
node.children?.[1]?.value?.[0] === ''
299+
) {
296300
return true;
297301
}
298302

299-
// Check for inline code + space + type link pattern
303+
// Check for inline code + space + type link + space pattern
300304
if (
301305
node.type === 'inlineCode' &&
302306
contentNodes[0]?.value.trim() === '' &&
303307
contentNodes[1]?.type === 'link' &&
304-
contentNodes[1]?.children?.[0]?.value?.[0] === '<'
308+
contentNodes[1]?.children?.[0]?.value?.[0] === '<' &&
309+
contentNodes[2]?.value?.[0] === ' '
305310
) {
306311
return true;
307312
}

0 commit comments

Comments
 (0)