Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: remove redundant code in doc/html.js #20514

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/doctool/test-doctool-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const testData = [
file: fixtures.path('order_of_end_tags_5873.md'),
html: '<h3>ClassMethod: Buffer.from(array) <span> ' +
'<a class="mark" href="#foo_class_method_buffer_from_array" ' +
'id="foo_class_method_buffer_from_array">#</a> </span> </h3><div' +
'class="signature"><ul><li><code>array</code><a ' +
'id="foo_class_method_buffer_from_array">#</a> </span> </h3>' +
'<ul><li><code>array</code><a ' +
'href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/' +
'Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a></li>' +
'</ul></div>'
'</ul>'
},
{
file: fixtures.path('doc_with_yaml.md'),
Expand Down
64 changes: 12 additions & 52 deletions tools/doc/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function render(opts, cb) {
filename = path.basename(filename, '.md');

parseText(lexed);
lexed = parseLists(lexed);
lexed = preprocessElements(lexed);

// Generate the table of contents.
// This mutates the lexed contents in-place.
Expand Down Expand Up @@ -231,25 +231,28 @@ function parseText(lexed) {
});
}

// Just update the list item text in-place.
// Lists that come right after a heading are what we're after.
function parseLists(input) {
// Preprocess stability blockquotes and YAML blocks.
function preprocessElements(input) {
var state = null;
const savedState = [];
var depth = 0;
const output = [];
let headingIndex = -1;
let heading = null;

output.links = input.links;
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') {
savedState.push(state);
state = 'MAYBE_STABILITY_BQ';
return;
}
if (tok.type === 'blockquote_end' && state === 'MAYBE_STABILITY_BQ') {
state = savedState.pop();
state = null;
return;
}
if ((tok.type === 'paragraph' && state === 'MAYBE_STABILITY_BQ') ||
Expand All @@ -271,50 +274,7 @@ function parseLists(input) {
return;
} else if (state === 'MAYBE_STABILITY_BQ') {
output.push({ type: 'blockquote_start' });
state = savedState.pop();
}
}
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;
state = null;
}
}
output.push(tok);
Expand Down