Skip to content

Commit a04ad2d

Browse files
authored
fix(types,css): fix type parsing, and css headers (#379)
* fix(types,css): fix type parsing, and css headers * fixup!
1 parent a470b27 commit a04ad2d

File tree

10 files changed

+88
-133
lines changed

10 files changed

+88
-133
lines changed

src/generators/metadata/__tests__/slugger.test.mjs

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

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/__tests__/index.test.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ describe('transformTypeToReferenceLink', () => {
2222
'[`<Array>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)'
2323
);
2424
});
25+
26+
it('should transform a prefixed type into a Markdown link', () => {
27+
strictEqual(
28+
transformTypeToReferenceLink('prefix.Type'),
29+
'[`<prefix.Type>`](prefix.html#class-prefixtype)'
30+
);
31+
});
2532
});
2633

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

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

@@ -104,6 +114,7 @@ export const DOC_TYPES_MAPPING_GLOBALS = {
104114
'WeakSet',
105115

106116
'TypedArray',
117+
'Float16Array',
107118
'Float32Array',
108119
'Float64Array',
109120
'Int8Array',
@@ -137,6 +148,7 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
137148
AesGcmParams: 'webcrypto.html#class-aesgcmparams',
138149
AesKeyAlgorithm: 'webcrypto.html#class-aeskeyalgorithm',
139150
AesKeyGenParams: 'webcrypto.html#class-aeskeygenparams',
151+
AesDerivedKeyParams: 'webcrypto.html#class-aesderivedkeyparams',
140152

141153
Blob: 'buffer.html#class-blob',
142154
BroadcastChannel:
@@ -148,6 +160,8 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
148160
Channel: 'diagnostics_channel.html#class-channel',
149161
ChildProcess: 'child_process.html#class-childprocess',
150162
Cipher: 'crypto.html#class-cipher',
163+
Cipheriv: 'crypto.html#class-cipheriv',
164+
Decipheriv: 'crypto.html#class-decipheriv',
151165
ClientHttp2Session: 'http2.html#class-clienthttp2session',
152166
ClientHttp2Stream: 'http2.html#class-clienthttp2stream',
153167

@@ -201,6 +215,8 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
201215
IntervalHistogram:
202216
'perf_hooks.html#class-intervalhistogram-extends-histogram',
203217

218+
LockManager: 'worker_threads.html#class-lockmanager',
219+
204220
KeyAlgorithm: 'webcrypto.html#class-keyalgorithm',
205221
KeyObject: 'crypto.html#class-keyobject',
206222

@@ -229,6 +245,10 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
229245
ReadableStreamDefaultReader:
230246
'webstreams.html#class-readablestreamdefaultreader',
231247

248+
ModuleRequest: 'vm.html#type-modulerequest',
249+
250+
DatabaseSync: 'sqlite.html#class-databasesync',
251+
232252
RecordableHistogram:
233253
'perf_hooks.html#class-recordablehistogram-extends-histogram',
234254

@@ -243,6 +263,10 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
243263

244264
Sign: 'crypto.html#class-sign',
245265

266+
Disposable:
267+
'https://tc39.es/proposal-explicit-resource-management/#sec-disposable-interface',
268+
269+
Session: 'sqlite.html#class-session',
246270
StatementSync: 'sqlite.html#class-statementsync',
247271

248272
Stream: 'stream.html#stream',
@@ -283,71 +307,20 @@ export const DOC_TYPES_MAPPING_NODE_MODULES = {
283307

284308
'brotli options': 'zlib.html#class-brotlioptions',
285309

286-
'cluster.Worker': 'cluster.html#class-worker',
287-
288-
'crypto.constants': 'crypto.html#cryptoconstants',
289-
290-
'dgram.Socket': 'dgram.html#class-dgramsocket',
291-
292-
'errors.Error': 'errors.html#class-error',
293-
294-
'fs.Dir': 'fs.html#class-fsdir',
295-
'fs.Dirent': 'fs.html#class-fsdirent',
296-
'fs.FSWatcher': 'fs.html#class-fsfswatcher',
297-
'fs.ReadStream': 'fs.html#class-fsreadstream',
298-
'fs.StatFs': 'fs.html#class-fsstatfs',
299-
'fs.Stats': 'fs.html#class-fsstats',
300-
'fs.StatWatcher': 'fs.html#class-fsstatwatcher',
301-
'fs.WriteStream': 'fs.html#class-fswritestream',
302-
303-
'http.Agent': 'http.html#class-httpagent',
304-
'http.ClientRequest': 'http.html#class-httpclientrequest',
305-
'http.IncomingMessage': 'http.html#class-httpincomingmessage',
306-
'http.OutgoingMessage': 'http.html#class-httpoutgoingmessage',
307-
'http.Server': 'http.html#class-httpserver',
308-
'http.ServerResponse': 'http.html#class-httpserverresponse',
309-
310-
'http2.Http2ServerRequest': 'http2.html#class-http2http2serverrequest',
311-
'http2.Http2ServerResponse': 'http2.html#class-http2http2serverresponse',
312-
313310
'import.meta': 'esm.html#importmeta',
314311

315-
'module.SourceMap': 'module.html#class-modulesourcemap',
316-
317-
'net.BlockList': 'net.html#class-netblocklist',
318-
'net.Server': 'net.html#class-netserver',
319-
'net.Socket': 'net.html#class-netsocket',
320-
'net.SocketAddress': 'net.html#class-netsocketaddress',
321-
322312
'os.constants.dlopen': 'os.html#dlopen-constants',
323313

324-
'readline.Interface': 'readline.html#class-readlineinterface',
325-
'readline.InterfaceConstructor': 'readline.html#class-interfaceconstructor',
326314
'readlinePromises.Interface': 'readline.html#class-readlinepromisesinterface',
327315

328-
'repl.REPLServer': 'repl.html#class-replserver',
329-
330316
require: 'modules.html#requireid',
331-
332-
'stream.Duplex': 'stream.html#class-streamduplex',
333-
'stream.Readable': 'stream.html#class-streamreadable',
334-
'stream.Transform': 'stream.html#class-streamtransform',
335-
'stream.Writable': 'stream.html#class-streamwritable',
336-
337-
'tls.SecureContext': 'tls.html#tlscreatesecurecontextoptions',
338-
'tls.Server': 'tls.html#class-tlsserver',
339-
'tls.TLSSocket': 'tls.html#class-tlstlssocket',
340-
341-
'tty.ReadStream': 'tty.html#class-ttyreadstream',
342-
'tty.WriteStream': 'tty.html#class-ttywritestream',
343-
344-
'vm.Module': 'vm.html#class-vmmodule',
345-
'vm.Script': 'vm.html#class-vmscript',
346-
'vm.SourceTextModule': 'vm.html#class-vmsourcetextmodule',
347-
'vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER':
348-
'vm.html#vmconstantsuse_main_context_default_loader',
317+
module: 'modules.html#the-module-object',
349318

350319
'zlib options': 'zlib.html#class-options',
320+
'zstd options': 'zlib.html#class-zstdoptions',
321+
322+
'HTTP/2 Headers Object': 'http2.html#headers-object',
323+
'HTTP/2 Settings Object': 'http2.html#settings-object',
351324
};
352325

353326
// 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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,10 @@ 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+
normalizeTypes: /(\{|<)(?! )[^<({})>]+(?! )(\}|>)/g,
202201
// ReGex to match the type API type references that got already parsed
203202
// so that they can be transformed into HTML links
204-
linksWithTypes: /\[`<([a-zA-Z0-9.| \\[\]]+)>`\]\((\S+)\)/g,
203+
linksWithTypes: /\[`<[^<({})>]+>`\]\((\S+)\)/g,
205204
// ReGeX for handling Stability Indexes Metadata
206205
stabilityIndex: /^Stability: ([0-5](?:\.[0-3])?)(?:\s*-\s*)?(.*)$/s,
207206
// ReGeX for handling the Stability Index Prefix

0 commit comments

Comments
 (0)