summaryrefslogtreecommitdiff
path: root/tools/doc/generate.js
diff options
context:
space:
mode:
authorSam Ruby <rubys@intertwingly.net>2018-07-06 21:46:38 -0400
committerVse Mozhet Byt <vsemozhetbyt@gmail.com>2018-07-25 21:33:06 +0300
commitf41dd5592ef4df3d200269e536a1693919d73b25 (patch)
tree3dab1cd7984ccc3ede8d5978695bda5106523edb /tools/doc/generate.js
parentb675fe360e60e4a21fd683e980066ca09adc1a03 (diff)
downloadandroid-node-v8-f41dd5592ef4df3d200269e536a1693919d73b25.tar.gz
android-node-v8-f41dd5592ef4df3d200269e536a1693919d73b25.tar.bz2
android-node-v8-f41dd5592ef4df3d200269e536a1693919d73b25.zip
tools: produce JSON documentation using unified/remark/rehype
PR-URL: https://github.com/nodejs/node/pull/21697 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'tools/doc/generate.js')
-rw-r--r--tools/doc/generate.js59
1 files changed, 39 insertions, 20 deletions
diff --git a/tools/doc/generate.js b/tools/doc/generate.js
index 8ed97610cf..99dce4122a 100644
--- a/tools/doc/generate.js
+++ b/tools/doc/generate.js
@@ -22,25 +22,34 @@
'use strict';
const fs = require('fs');
+const path = require('path');
+const unified = require('unified');
+const markdown = require('remark-parse');
+const remark2rehype = require('remark-rehype');
+const raw = require('rehype-raw');
+const htmlStringify = require('rehype-stringify');
+
+const html = require('./html');
+const json = require('./json');
// Parse the args.
// Don't use nopt or whatever for this. It's simple enough.
const args = process.argv.slice(2);
-let format = 'json';
let filename = null;
let nodeVersion = null;
let analytics = null;
+let outputDir = null;
args.forEach(function(arg) {
if (!arg.startsWith('--')) {
filename = arg;
- } else if (arg.startsWith('--format=')) {
- format = arg.replace(/^--format=/, '');
} else if (arg.startsWith('--node-version=')) {
nodeVersion = arg.replace(/^--node-version=/, '');
} else if (arg.startsWith('--analytics=')) {
analytics = arg.replace(/^--analytics=/, '');
+ } else if (arg.startsWith('--output-directory=')) {
+ outputDir = arg.replace(/^--output-directory=/, '');
}
});
@@ -48,27 +57,37 @@ nodeVersion = nodeVersion || process.version;
if (!filename) {
throw new Error('No input file specified');
+} else if (!outputDir) {
+ throw new Error('No output directory specified');
}
+
fs.readFile(filename, 'utf8', (er, input) => {
if (er) throw er;
- switch (format) {
- case 'json':
- require('./json.js')(input, filename, (er, obj) => {
- if (er) throw er;
- console.log(JSON.stringify(obj, null, 2));
- });
- break;
- case 'html':
- require('./html')({ input, filename, nodeVersion, analytics },
- (err, html) => {
- if (err) throw err;
- console.log(html);
- });
- break;
+ const content = unified()
+ .use(markdown)
+ .use(json.jsonAPI, { filename })
+ .use(html.firstHeader)
+ .use(html.preprocessText)
+ .use(html.preprocessElements, { filename })
+ .use(html.buildToc, { filename })
+ .use(remark2rehype, { allowDangerousHTML: true })
+ .use(raw)
+ .use(htmlStringify)
+ .processSync(input);
- default:
- throw new Error(`Invalid format: ${format}`);
- }
+ const basename = path.basename(filename, '.md');
+
+ html.toHTML(
+ { input, content, filename, nodeVersion, analytics },
+ (err, html) => {
+ const target = path.join(outputDir, `${basename}.html`);
+ if (err) throw err;
+ fs.writeFileSync(target, html);
+ }
+ );
+
+ const target = path.join(outputDir, `${basename}.json`);
+ fs.writeFileSync(target, JSON.stringify(content.json, null, 2));
});