From 1241b90a137e409b76ebfb035291c83a73a4940c Mon Sep 17 00:00:00 2001
From: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Date: Wed, 25 Apr 2018 23:44:38 +0300
Subject: [PATCH] tools: simplify HTML generation

PR-URL: https://github.com/nodejs/node/pull/20307
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
---
 Makefile                          |  2 +-
 test/doctool/test-doctool-html.js |  2 -
 tools/doc/generate.js             |  5 +-
 tools/doc/html.js                 | 85 ++++++++-----------------------
 4 files changed, 24 insertions(+), 70 deletions(-)

diff --git a/Makefile b/Makefile
index 93e1831421de07..fe7595bea70dee 100644
--- a/Makefile
+++ b/Makefile
@@ -603,7 +603,7 @@ tools/doc/node_modules/js-yaml/package.json:
 
 gen-json = tools/doc/generate.js --format=json $< > $@
 gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
-			--template=doc/template.html --analytics=$(DOCS_ANALYTICS) $< > $@
+			--analytics=$(DOCS_ANALYTICS) $< > $@
 
 out/doc/api/%.json: doc/api/%.md
 	$(call available-node, $(gen-json))
diff --git a/test/doctool/test-doctool-html.js b/test/doctool/test-doctool-html.js
index 40d7b54f59320a..9c30219c647273 100644
--- a/test/doctool/test-doctool-html.js
+++ b/test/doctool/test-doctool-html.js
@@ -10,7 +10,6 @@ try {
 
 const assert = require('assert');
 const fs = require('fs');
-const path = require('path');
 const fixtures = require('../common/fixtures');
 const processIncludes = require('../../tools/doc/preprocess.js');
 const html = require('../../tools/doc/html.js');
@@ -107,7 +106,6 @@ testData.forEach((item) => {
         {
           input: preprocessed,
           filename: 'foo',
-          template: path.resolve(__dirname, '../../doc/template.html'),
           nodeVersion: process.version,
           analytics: item.analyticsId,
         },
diff --git a/tools/doc/generate.js b/tools/doc/generate.js
index 0da9dba4e6558f..9f217b19c7225f 100644
--- a/tools/doc/generate.js
+++ b/tools/doc/generate.js
@@ -29,7 +29,6 @@ const fs = require('fs');
 
 const args = process.argv.slice(2);
 let format = 'json';
-let template = null;
 let filename = null;
 let nodeVersion = null;
 let analytics = null;
@@ -39,8 +38,6 @@ args.forEach(function(arg) {
     filename = arg;
   } else if (arg.startsWith('--format=')) {
     format = arg.replace(/^--format=/, '');
-  } else if (arg.startsWith('--template=')) {
-    template = arg.replace(/^--template=/, '');
   } else if (arg.startsWith('--node-version=')) {
     nodeVersion = arg.replace(/^--node-version=/, '');
   } else if (arg.startsWith('--analytics=')) {
@@ -71,7 +68,7 @@ function next(er, input) {
       break;
 
     case 'html':
-      require('./html')({ input, filename, template, nodeVersion, analytics },
+      require('./html')({ input, filename, nodeVersion, analytics },
                         (err, html) => {
                           if (err) throw err;
                           console.log(html);
diff --git a/tools/doc/html.js b/tools/doc/html.js
index ff0230309ee99a..439fc057012ca7 100644
--- a/tools/doc/html.js
+++ b/tools/doc/html.js
@@ -25,7 +25,6 @@ const common = require('./common.js');
 const fs = require('fs');
 const marked = require('marked');
 const path = require('path');
-const preprocess = require('./preprocess.js');
 const typeParser = require('./type-parser.js');
 
 module.exports = toHTML;
@@ -42,76 +41,36 @@ marked.setOptions({
   renderer: renderer
 });
 
-// TODO(chrisdickinson): never stop vomiting / fix this.
-const gtocPath = path.resolve(path.join(
-  __dirname,
-  '..',
-  '..',
-  'doc',
-  'api',
-  '_toc.md'
-));
-var gtocLoading = null;
-var gtocData = null;
+const docPath = path.resolve(__dirname, '..', '..', 'doc');
+
+const gtocPath = path.join(docPath, 'api', '_toc.md');
+const gtocMD = fs.readFileSync(gtocPath, 'utf8').replace(/^@\/\/.*$/gm, '');
+const gtocHTML = marked(gtocMD).replace(
+  /<a href="(.*?)"/g,
+  (all, href) => `<a class="nav-${toID(href)}" href="${href}"`
+);
+
+const templatePath = path.join(docPath, 'template.html');
+const template = fs.readFileSync(templatePath, 'utf8');
+
 var docCreated = null;
 var nodeVersion = null;
 
 /**
- * opts: input, filename, template, nodeVersion.
+ * opts: input, filename, nodeVersion.
  */
 function toHTML(opts, cb) {
-  const template = opts.template;
-
   nodeVersion = opts.nodeVersion || process.version;
   docCreated = opts.input.match(DOC_CREATED_REG_EXP);
 
-  if (gtocData) {
-    return onGtocLoaded();
-  }
-
-  if (gtocLoading === null) {
-    gtocLoading = [onGtocLoaded];
-    return loadGtoc(function(err, data) {
-      if (err) throw err;
-      gtocData = data;
-      gtocLoading.forEach(function(xs) {
-        xs();
-      });
-    });
-  }
-
-  if (gtocLoading) {
-    return gtocLoading.push(onGtocLoaded);
-  }
-
-  function onGtocLoaded() {
-    const lexed = marked.lexer(opts.input);
-    fs.readFile(template, 'utf8', function(er, template) {
-      if (er) return cb(er);
-      render({
-        lexed: lexed,
-        filename: opts.filename,
-        template: template,
-        nodeVersion: nodeVersion,
-        analytics: opts.analytics,
-      }, cb);
-    });
-  }
-}
-
-function loadGtoc(cb) {
-  fs.readFile(gtocPath, 'utf8', function(err, data) {
-    if (err) return cb(err);
-
-    preprocess(gtocPath, data, function(err, data) {
-      if (err) return cb(err);
-
-      data = marked(data).replace(/<a href="(.*?)"/gm, function(a, m) {
-        return `<a class="nav-${toID(m)}" href="${m}"`;
-      });
-      return cb(null, data);
-    });
-  });
+  const lexed = marked.lexer(opts.input);
+  render({
+    lexed: lexed,
+    filename: opts.filename,
+    template: template,
+    nodeVersion: nodeVersion,
+    analytics: opts.analytics,
+  }, cb);
 }
 
 function toID(filename) {
@@ -150,7 +109,7 @@ function render(opts, cb) {
     template = template.replace(/__TOC__/g, toc);
     template = template.replace(
       /__GTOC__/g,
-      gtocData.replace(`class="nav-${id}`, `class="nav-${id} active`)
+      gtocHTML.replace(`class="nav-${id}`, `class="nav-${id} active`)
     );
 
     if (opts.analytics) {