Skip to content

Commit e517efa

Browse files
claudiorodriguezsilverwind
authored andcommitted
tools: parse types into links in doc html gen
Changes the parsing of parameter types in the doc html gen Links to either MDN or nodejs docs depending on type See nodejs#4350 PR-URL: nodejs#4741 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Chris Dickinson <chris@neversaw.us> Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent 8830797 commit e517efa

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

tools/doc/html.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var fs = require('fs');
44
var marked = require('marked');
55
var path = require('path');
66
var preprocess = require('./preprocess.js');
7+
var typeParser = require('./type-parser.js');
78

89
module.exports = toHTML;
910

@@ -118,7 +119,8 @@ function parseLists(input) {
118119
output.push({ type: 'html', text: tok.text });
119120
return;
120121
}
121-
if (state === null) {
122+
if (state === null ||
123+
(state === 'AFTERHEADING' && tok.type === 'heading')) {
122124
if (tok.type === 'heading') {
123125
state = 'AFTERHEADING';
124126
}
@@ -168,9 +170,15 @@ function parseLists(input) {
168170
function parseListItem(text) {
169171
var parts = text.split('`');
170172
var i;
173+
var typeMatches;
171174

172175
for (i = 0; i < parts.length; i += 2) {
173-
parts[i] = parts[i].replace(/\{([^\}]+)\}/, '<span class="type">$1</span>');
176+
typeMatches = parts[i].match(/\{([^\}]+)\}/g);
177+
if (typeMatches) {
178+
typeMatches.forEach(function(typeMatch) {
179+
parts[i] = parts[i].replace(typeMatch, typeParser.toLink(typeMatch));
180+
});
181+
}
174182
}
175183

176184
//XXX maybe put more stuff here?
@@ -229,4 +237,3 @@ function getId(text) {
229237
}
230238
return text;
231239
}
232-

tools/doc/type-parser.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
const nodeDocUrl = '';
3+
const jsDocUrl = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/' +
4+
'Reference/Global_Objects/';
5+
const jsPrimitiveUrl = 'https://developer.mozilla.org/en-US/docs/Web/' +
6+
'JavaScript/Data_structures';
7+
const jsPrimitives = [
8+
'Number', 'String', 'Boolean', 'Null', 'Symbol'
9+
]
10+
const jsGlobalTypes = [
11+
'Error', 'Object', 'Function', 'Array', 'Uint8Array',
12+
'Uint16Array', 'Uint32Array', 'Int8Array', 'Int16Array', 'Int32Array',
13+
'Uint8ClampedArray', 'Float32Array', 'Float64Array', 'Date', 'RegExp',
14+
'ArrayBuffer', 'DataView', 'Promise'
15+
];
16+
const typeMap = {
17+
'Buffer': 'buffer.html#buffer_class_buffer',
18+
'Handle': 'net.html#net_server_listen_handle_backlog_callback',
19+
'Stream': 'stream.html#stream_stream',
20+
'stream.Writable': 'stream.html#stream_class_stream_writable',
21+
'stream.Readable': 'stream.html#stream_class_stream_readable',
22+
'ChildProcess': 'child_process.html#child_process_class_childprocess',
23+
'cluster.Worker': 'cluster.html#cluster_class_worker',
24+
'dgram.Socket': 'dgram.html#dgram_class_dgram_socket',
25+
'net.Socket': 'net.html#net_class_net_socket',
26+
'EventEmitter': 'events.html#events_class_events_eventemitter',
27+
'Timer': 'timers.html#timers_timers'
28+
};
29+
30+
module.exports = {
31+
toLink: function (typeInput) {
32+
let typeLinks = [];
33+
typeInput = typeInput.replace('{', '').replace('}', '');
34+
let typeTexts = typeInput.split('|');
35+
36+
typeTexts.forEach(function (typeText) {
37+
typeText = typeText.trim();
38+
if (typeText) {
39+
let typeUrl = null;
40+
if (jsPrimitives.indexOf(typeText) !== -1) {
41+
typeUrl = jsPrimitiveUrl + '#' + typeText + '_type';
42+
} else if (jsGlobalTypes.indexOf(typeText) !== -1) {
43+
typeUrl = jsDocUrl + typeText;
44+
} else if (typeMap[typeText]) {
45+
typeUrl = nodeDocUrl + typeMap[typeText];
46+
}
47+
48+
if (typeUrl) {
49+
typeLinks.push('<a href="' + typeUrl + '" class="type">&lt;' +
50+
typeText + '&gt;</a>');
51+
} else {
52+
typeLinks.push('<span class="type">&lt;' + typeText + '&gt;</span>');
53+
}
54+
}
55+
});
56+
57+
return typeLinks.length ? typeLinks.join(' | ') : typeInput;
58+
}
59+
}

0 commit comments

Comments
 (0)