From 974df9c2be11910d7aef381d73cb11a4bb547c20 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Fri, 4 May 2018 12:16:38 +0300 Subject: [PATCH] tools: remove redundant code in doc/html.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR reduces code by 40 lines and docs size by ~7.5 KB. Only
...
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 Reviewed-By: Tobias Nießen Reviewed-By: Trivikram Kamat --- test/doctool/test-doctool-html.js | 6 +-- tools/doc/html.js | 64 ++++++------------------------- 2 files changed, 15 insertions(+), 55 deletions(-) diff --git a/test/doctool/test-doctool-html.js b/test/doctool/test-doctool-html.js index 91037bfd6501bc..91a18d536a6992 100644 --- a/test/doctool/test-doctool-html.js +++ b/test/doctool/test-doctool-html.js @@ -29,11 +29,11 @@ const testData = [ file: fixtures.path('order_of_end_tags_5873.md'), html: '

ClassMethod: Buffer.from(array) ' + '#

' }, { file: fixtures.path('doc_with_yaml.md'), diff --git a/tools/doc/html.js b/tools/doc/html.js index 439fc057012ca7..1bb5fa630fa8c2 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -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. @@ -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') || @@ -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: '
' }); - } - 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: '
' }); - } - return; + state = null; } } output.push(tok);