tools: remove redundant code in doc/html.js

This PR reduces code by 40 lines and docs size by ~7.5 KB. Only
<div class="signature">...</div> wrappers are removed from docs,
no other changes are found in results.

PR-URL: https://github.com/nodejs/node/pull/20514
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Vse Mozhet Byt 2018-05-04 12:16:38 +03:00 committed by Anatoli Papirovski
parent 2a96ee284c
commit 974df9c2be
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 15 additions and 55 deletions

View File

@ -29,11 +29,11 @@ const testData = [
file: fixtures.path('order_of_end_tags_5873.md'), file: fixtures.path('order_of_end_tags_5873.md'),
html: '<h3>ClassMethod: Buffer.from(array) <span> ' + html: '<h3>ClassMethod: Buffer.from(array) <span> ' +
'<a class="mark" href="#foo_class_method_buffer_from_array" ' + '<a class="mark" href="#foo_class_method_buffer_from_array" ' +
'id="foo_class_method_buffer_from_array">#</a> </span> </h3><div' + 'id="foo_class_method_buffer_from_array">#</a> </span> </h3>' +
'class="signature"><ul><li><code>array</code><a ' + '<ul><li><code>array</code><a ' +
'href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/' + 'href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/' +
'Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a></li>' + 'Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a></li>' +
'</ul></div>' '</ul>'
}, },
{ {
file: fixtures.path('doc_with_yaml.md'), file: fixtures.path('doc_with_yaml.md'),

View File

@ -93,7 +93,7 @@ function render(opts, cb) {
filename = path.basename(filename, '.md'); filename = path.basename(filename, '.md');
parseText(lexed); parseText(lexed);
lexed = parseLists(lexed); lexed = preprocessElements(lexed);
// Generate the table of contents. // Generate the table of contents.
// This mutates the lexed contents in-place. // This mutates the lexed contents in-place.
@ -231,25 +231,28 @@ function parseText(lexed) {
}); });
} }
// Just update the list item text in-place. // Preprocess stability blockquotes and YAML blocks.
// Lists that come right after a heading are what we're after. function preprocessElements(input) {
function parseLists(input) {
var state = null; var state = null;
const savedState = [];
var depth = 0;
const output = []; const output = [];
let headingIndex = -1; let headingIndex = -1;
let heading = null; let heading = null;
output.links = input.links; output.links = input.links;
input.forEach(function(tok, index) { input.forEach(function(tok, index) {
if (tok.type === 'heading') {
headingIndex = index;
heading = tok;
}
if (tok.type === 'html' && common.isYAMLBlock(tok.text)) {
tok.text = parseYAML(tok.text);
}
if (tok.type === 'blockquote_start') { if (tok.type === 'blockquote_start') {
savedState.push(state);
state = 'MAYBE_STABILITY_BQ'; state = 'MAYBE_STABILITY_BQ';
return; return;
} }
if (tok.type === 'blockquote_end' && state === 'MAYBE_STABILITY_BQ') { if (tok.type === 'blockquote_end' && state === 'MAYBE_STABILITY_BQ') {
state = savedState.pop(); state = null;
return; return;
} }
if ((tok.type === 'paragraph' && state === 'MAYBE_STABILITY_BQ') || if ((tok.type === 'paragraph' && state === 'MAYBE_STABILITY_BQ') ||
@ -271,50 +274,7 @@ function parseLists(input) {
return; return;
} else if (state === 'MAYBE_STABILITY_BQ') { } else if (state === 'MAYBE_STABILITY_BQ') {
output.push({ type: 'blockquote_start' }); output.push({ type: 'blockquote_start' });
state = savedState.pop(); state = null;
}
}
if (state === null ||
(state === 'AFTERHEADING' && tok.type === 'heading')) {
if (tok.type === 'heading') {
headingIndex = index;
heading = tok;
state = 'AFTERHEADING';
}
output.push(tok);
return;
}
if (state === 'AFTERHEADING') {
if (tok.type === 'list_start') {
state = 'LIST';
if (depth === 0) {
output.push({ type: 'html', text: '<div class="signature">' });
}
depth++;
output.push(tok);
return;
}
if (tok.type === 'html' && common.isYAMLBlock(tok.text)) {
tok.text = parseYAML(tok.text);
}
state = null;
output.push(tok);
return;
}
if (state === 'LIST') {
if (tok.type === 'list_start') {
depth++;
output.push(tok);
return;
}
if (tok.type === 'list_end') {
depth--;
output.push(tok);
if (depth === 0) {
state = null;
output.push({ type: 'html', text: '</div>' });
}
return;
} }
} }
output.push(tok); output.push(tok);