GeezerVison is showing github.com — tools: produce JSON documentation using unified/remark/rehype · nodejs/node@c85d00b
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,6 +107,9 @@ deps/npm/node_modules/.bin/ | 107 | 107 | /*.pkg | 108 | 108 | /SHASUMS*.txt* | 109 | 109 |
| | 110 | +# api docs artifacts | | 111 | +tools/doc/node_modules | | 112 | + | 110 | 113 | # test artifacts | 111 | 114 | tools/remark-cli/node_modules | 112 | 115 | tools/remark-preset-lint-node/node_modules | |
Collapse file Copy file name to clipboardExpand all lines: Makefile +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -320,7 +320,7 @@ ifeq ($(OSTYPE),aix) | 320 | 320 | DOCBUILDSTAMP_PREREQS := $(DOCBUILDSTAMP_PREREQS) out/$(BUILDTYPE)/node.exp | 321 | 321 | endif | 322 | 322 |
| 323 | | -test/addons/.docbuildstamp: $(DOCBUILDSTAMP_PREREQS) | | 323 | +test/addons/.docbuildstamp: $(DOCBUILDSTAMP_PREREQS) tools/doc/node_modules | 324 | 324 | $(RM) -r test/addons/??_*/ | 325 | 325 | [ -x $(NODE) ] && $(NODE) $< || node $< | 326 | 326 | touch $@ | @@ -633,15 +633,15 @@ available-node = \ | 633 | 633 | run-npm-install = $(PWD)/$(NPM) install --production --no-package-lock | 634 | 634 | run-npm-ci = $(PWD)/$(NPM) ci | 635 | 635 |
| 636 | | -gen-json = tools/doc/generate.js --format=json $< > $@ | 637 | | -gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \ | 638 | | - --analytics=$(DOCS_ANALYTICS) $< > $@ | | 636 | +tools/doc/node_modules/js-yaml/package.json: | | 637 | + cd tools/doc && $(call available-node,$(run-npm-install)) | 639 | 638 |
| 640 | | -out/doc/api/%.json: doc/api/%.md tools/doc/generate.js tools/doc/json.js | 641 | | - $(call available-node, $(gen-json)) | | 639 | +gen-api = tools/doc/generate.js --node-version=$(FULLVERSION) \ | | 640 | + --analytics=$(DOCS_ANALYTICS) $< --output-directory=out/doc/api | 642 | 641 |
| 643 | | -out/doc/api/%.html: doc/api/%.md tools/doc/generate.js tools/doc/html.js | 644 | | - $(call available-node, $(gen-html)) | | 642 | +out/doc/api/%.json out/doc/api/%.html: doc/api/%.md tools/doc/generate.js \ | | 643 | + tools/doc/html.js tools/doc/json.js | | 644 | + $(call available-node, $(gen-api)) | 645 | 645 |
| 646 | 646 | out/doc/api/all.html: $(apidocs_html) tools/doc/allhtml.js | 647 | 647 | $(call available-node, tools/doc/allhtml.js) | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/https.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -123,7 +123,7 @@ changes: | 123 | 123 | pr-url: https://github.com/nodejs/node/pull/10638 | 124 | 124 | description: The `options` parameter can be a WHATWG `URL` object. | 125 | 125 | --> | 126 | | -- `url` {string | URL} | | 126 | +* `url` {string | URL} | 127 | 127 | * `options` {Object | string | URL} Accepts the same `options` as | 128 | 128 | [`https.request()`][], with the `method` always set to `GET`. | 129 | 129 | * `callback` {Function} | @@ -174,7 +174,7 @@ changes: | 174 | 174 | pr-url: https://github.com/nodejs/node/pull/10638 | 175 | 175 | description: The `options` parameter can be a WHATWG `URL` object. | 176 | 176 | --> | 177 | | -- `url` {string | URL} | | 177 | +* `url` {string | URL} | 178 | 178 | * `options` {Object | string | URL} Accepts all `options` from | 179 | 179 | [`http.request()`][], with some differences in default values: | 180 | 180 | - `protocol` **Default:** `'https:'` | |
Collapse file Copy file name to clipboardExpand all lines: test/doctool/test-doctool-html.js +28-2Lines changed: 28 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | const assert = require('assert'); | 12 | 12 | const { readFile } = require('fs'); | 13 | 13 | const fixtures = require('../common/fixtures'); | 14 | | -const toHTML = require('../../tools/doc/html.js'); | | 14 | +const html = require('../../tools/doc/html.js'); | | 15 | +const path = require('path'); | | 16 | + | | 17 | +module.paths.unshift( | | 18 | + path.join(__dirname, '..', '..', 'tools', 'doc', 'node_modules')); | | 19 | +const unified = require('unified'); | | 20 | +const markdown = require('remark-parse'); | | 21 | +const remark2rehype = require('remark-rehype'); | | 22 | +const raw = require('rehype-raw'); | | 23 | +const htmlStringify = require('rehype-stringify'); | | 24 | + | | 25 | +function toHTML({ input, filename, nodeVersion, analytics }, cb) { | | 26 | + const content = unified() | | 27 | + .use(markdown) | | 28 | + .use(html.firstHeader) | | 29 | + .use(html.preprocessText) | | 30 | + .use(html.preprocessElements, { filename }) | | 31 | + .use(html.buildToc, { filename }) | | 32 | + .use(remark2rehype, { allowDangerousHTML: true }) | | 33 | + .use(raw) | | 34 | + .use(htmlStringify) | | 35 | + .processSync(input); | | 36 | + | | 37 | + html.toHTML( | | 38 | + { input, content, filename, nodeVersion, analytics }, | | 39 | + cb | | 40 | + ); | | 41 | +} | 15 | 42 |
| 16 | 43 | // Test data is a list of objects with two properties. | 17 | 44 | // The file property is the file path. | @@ -80,7 +107,6 @@ testData.forEach(({ file, html, analyticsId }) => { | 80 | 107 |
| 81 | 108 | readFile(file, 'utf8', common.mustCall((err, input) => { | 82 | 109 | assert.ifError(err); | 83 | | - | 84 | 110 | toHTML( | 85 | 111 | { | 86 | 112 | input: input, | |
Collapse file Copy file name to clipboardExpand all lines: test/doctool/test-doctool-json.js +30-14Lines changed: 30 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 |
| 11 | 11 | const assert = require('assert'); | 12 | 12 | const fs = require('fs'); | | 13 | +const path = require('path'); | 13 | 14 | const fixtures = require('../common/fixtures'); | 14 | 15 | const json = require('../../tools/doc/json.js'); | 15 | 16 |
| | 17 | +module.paths.unshift( | | 18 | + path.join(__dirname, '..', '..', 'tools', 'doc', 'node_modules')); | | 19 | +const unified = require('unified'); | | 20 | +const markdown = require('remark-parse'); | | 21 | + | | 22 | +function toJSON(input, filename, cb) { | | 23 | + function nullCompiler() { | | 24 | + this.Compiler = (tree) => tree; | | 25 | + } | | 26 | + | | 27 | + unified() | | 28 | + .use(markdown) | | 29 | + .use(json.jsonAPI, { filename }) | | 30 | + .use(nullCompiler) | | 31 | + .process(input, cb); | | 32 | +} | | 33 | + | 16 | 34 | // Outputs valid json with the expected fields when given simple markdown | 17 | 35 | // Test data is a list of objects with two properties. | 18 | 36 | // The file property is the file path. | @@ -21,15 +39,16 @@ const testData = [ | 21 | 39 | { | 22 | 40 | file: fixtures.path('sample_document.md'), | 23 | 41 | json: { | | 42 | + type: 'module', | 24 | 43 | source: 'foo', | 25 | 44 | modules: [{ | 26 | 45 | textRaw: 'Sample Markdown', | 27 | 46 | name: 'sample_markdown', | 28 | 47 | modules: [{ | 29 | 48 | textRaw: 'Seussian Rhymes', | 30 | 49 | name: 'seussian_rhymes', | 31 | | - desc: '<ol>\n<li>fish</li>\n<li><p>fish</p>\n</li>\n<li>' + | 32 | | - '<p>Red fish</p>\n</li>\n<li>Blue fish</li>\n</ol>\n', | | 50 | + desc: '<ol>\n<li>fish</li>\n<li>fish</li>\n</ol>\n' + | | 51 | + '<ul>\n<li>Red fish</li>\n<li>Blue fish</li>\n</ul>', | 33 | 52 | type: 'module', | 34 | 53 | displayName: 'Seussian Rhymes' | 35 | 54 | }], | @@ -41,6 +60,7 @@ const testData = [ | 41 | 60 | { | 42 | 61 | file: fixtures.path('order_of_end_tags_5873.md'), | 43 | 62 | json: { | | 63 | + type: 'module', | 44 | 64 | source: 'foo', | 45 | 65 | modules: [{ | 46 | 66 | textRaw: 'Title', | @@ -55,15 +75,10 @@ const testData = [ | 55 | 75 | signatures: [ | 56 | 76 | { | 57 | 77 | params: [{ | 58 | | - textRaw: '`array` {Array} ', | | 78 | + textRaw: '`array` {Array}', | 59 | 79 | name: 'array', | 60 | 80 | type: 'Array' | 61 | 81 | }] | 62 | | - }, | 63 | | - { | 64 | | - params: [{ | 65 | | - name: 'array' | 66 | | - }] | 67 | 82 | } | 68 | 83 | ] | 69 | 84 | }], | @@ -78,6 +93,7 @@ const testData = [ | 78 | 93 | { | 79 | 94 | file: fixtures.path('doc_with_yaml.md'), | 80 | 95 | json: { | | 96 | + type: 'module', | 81 | 97 | source: 'foo', | 82 | 98 | modules: [ | 83 | 99 | { | @@ -92,7 +108,7 @@ const testData = [ | 92 | 108 | changes: [] | 93 | 109 | }, | 94 | 110 | desc: '<p>Describe <code>Foobar</code> in more detail ' + | 95 | | - 'here.</p>\n', | | 111 | + 'here.</p>', | 96 | 112 | type: 'module', | 97 | 113 | displayName: 'Foobar' | 98 | 114 | }, | @@ -110,7 +126,7 @@ const testData = [ | 110 | 126 | ] | 111 | 127 | }, | 112 | 128 | desc: '<p>Describe <code>Foobar II</code> in more detail ' + | 113 | | - 'here. fg(1)</p>\n', | | 129 | + 'here. fg(1)</p>', | 114 | 130 | type: 'module', | 115 | 131 | displayName: 'Foobar II' | 116 | 132 | }, | @@ -123,15 +139,15 @@ const testData = [ | 123 | 139 | changes: [] | 124 | 140 | }, | 125 | 141 | desc: '<p>Describe <code>Deprecated thingy</code> in more ' + | 126 | | - 'detail here. fg(1p)</p>\n', | | 142 | + 'detail here. fg(1p)</p>', | 127 | 143 | type: 'module', | 128 | 144 | displayName: 'Deprecated thingy' | 129 | 145 | }, | 130 | 146 | { | 131 | 147 | textRaw: 'Something', | 132 | 148 | name: 'something', | 133 | 149 | desc: '<!-- This is not a metadata comment -->\n<p>' + | 134 | | - 'Describe <code>Something</code> in more detail here.</p>\n', | | 150 | + 'Describe <code>Something</code> in more detail here.</p>', | 135 | 151 | type: 'module', | 136 | 152 | displayName: 'Something' | 137 | 153 | } | @@ -147,9 +163,9 @@ const testData = [ | 147 | 163 | testData.forEach((item) => { | 148 | 164 | fs.readFile(item.file, 'utf8', common.mustCall((err, input) => { | 149 | 165 | assert.ifError(err); | 150 | | - json(input, 'foo', common.mustCall((err, output) => { | | 166 | + toJSON(input, 'foo', common.mustCall((err, output) => { | 151 | 167 | assert.ifError(err); | 152 | | - assert.deepStrictEqual(output, item.json); | | 168 | + assert.deepStrictEqual(output.json, item.json); | 153 | 169 | })); | 154 | 170 | })); | 155 | 171 | }); |
Collapse file Copy file name to clipboardExpand all lines: tools/doc/generate.js +39-20Lines changed: 39 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const fs = require('fs'); | | 25 | +const path = require('path'); | | 26 | +const unified = require('unified'); | | 27 | +const markdown = require('remark-parse'); | | 28 | +const remark2rehype = require('remark-rehype'); | | 29 | +const raw = require('rehype-raw'); | | 30 | +const htmlStringify = require('rehype-stringify'); | | 31 | + | | 32 | +const html = require('./html'); | | 33 | +const json = require('./json'); | 25 | 34 |
| 26 | 35 | // Parse the args. | 27 | 36 | // Don't use nopt or whatever for this. It's simple enough. | 28 | 37 |
| 29 | 38 | const args = process.argv.slice(2); | 30 | | -let format = 'json'; | 31 | 39 | let filename = null; | 32 | 40 | let nodeVersion = null; | 33 | 41 | let analytics = null; | | 42 | +let outputDir = null; | 34 | 43 |
| 35 | 44 | args.forEach(function(arg) { | 36 | 45 | if (!arg.startsWith('--')) { | 37 | 46 | filename = arg; | 38 | | - } else if (arg.startsWith('--format=')) { | 39 | | - format = arg.replace(/^--format=/, ''); | 40 | 47 | } else if (arg.startsWith('--node-version=')) { | 41 | 48 | nodeVersion = arg.replace(/^--node-version=/, ''); | 42 | 49 | } else if (arg.startsWith('--analytics=')) { | 43 | 50 | analytics = arg.replace(/^--analytics=/, ''); | | 51 | + } else if (arg.startsWith('--output-directory=')) { | | 52 | + outputDir = arg.replace(/^--output-directory=/, ''); | 44 | 53 | } | 45 | 54 | }); | 46 | 55 |
| 47 | 56 | nodeVersion = nodeVersion || process.version; | 48 | 57 |
| 49 | 58 | if (!filename) { | 50 | 59 | throw new Error('No input file specified'); | | 60 | +} else if (!outputDir) { | | 61 | + throw new Error('No output directory specified'); | 51 | 62 | } | 52 | 63 |
| | 64 | + | 53 | 65 | fs.readFile(filename, 'utf8', (er, input) => { | 54 | 66 | if (er) throw er; | 55 | | - switch (format) { | 56 | | - case 'json': | 57 | | - require('./json.js')(input, filename, (er, obj) => { | 58 | | - if (er) throw er; | 59 | | - console.log(JSON.stringify(obj, null, 2)); | 60 | | - }); | 61 | | - break; | 62 | 67 |
| 63 | | - case 'html': | 64 | | - require('./html')({ input, filename, nodeVersion, analytics }, | 65 | | - (err, html) => { | 66 | | - if (err) throw err; | 67 | | - console.log(html); | 68 | | - }); | 69 | | - break; | | 68 | + const content = unified() | | 69 | + .use(markdown) | | 70 | + .use(json.jsonAPI, { filename }) | | 71 | + .use(html.firstHeader) | | 72 | + .use(html.preprocessText) | | 73 | + .use(html.preprocessElements, { filename }) | | 74 | + .use(html.buildToc, { filename }) | | 75 | + .use(remark2rehype, { allowDangerousHTML: true }) | | 76 | + .use(raw) | | 77 | + .use(htmlStringify) | | 78 | + .processSync(input); | 70 | 79 |
| 71 | | - default: | 72 | | - throw new Error(`Invalid format: ${format}`); | 73 | | - } | | 80 | + const basename = path.basename(filename, '.md'); | | 81 | + | | 82 | + html.toHTML( | | 83 | + { input, content, filename, nodeVersion, analytics }, | | 84 | + (err, html) => { | | 85 | + const target = path.join(outputDir, `${basename}.html`); | | 86 | + if (err) throw err; | | 87 | + fs.writeFileSync(target, html); | | 88 | + } | | 89 | + ); | | 90 | + | | 91 | + const target = path.join(outputDir, `${basename}.json`); | | 92 | + fs.writeFileSync(target, JSON.stringify(content.json, null, 2)); | 74 | 93 | }); |
Collapse file Copy file name to clipboardExpand all lines: tools/doc/html.js +8-17Lines changed: 8 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,11 +29,13 @@ const visit = require('unist-util-visit'); | 29 | 29 | const markdown = require('remark-parse'); | 30 | 30 | const remark2rehype = require('remark-rehype'); | 31 | 31 | const raw = require('rehype-raw'); | 32 | | -const html = require('rehype-stringify'); | | 32 | +const htmlStringify = require('rehype-stringify'); | 33 | 33 | const path = require('path'); | 34 | 34 | const typeParser = require('./type-parser.js'); | 35 | 35 |
| 36 | | -module.exports = toHTML; | | 36 | +module.exports = { | | 37 | + toHTML, firstHeader, preprocessText, preprocessElements, buildToc | | 38 | +}; | 37 | 39 |
| 38 | 40 | const docPath = path.resolve(__dirname, '..', '..', 'doc'); | 39 | 41 |
| @@ -54,26 +56,15 @@ const gtocHTML = unified() | 54 | 56 | .use(remark2rehype, { allowDangerousHTML: true }) | 55 | 57 | .use(raw) | 56 | 58 | .use(navClasses) | 57 | | - .use(html) | | 59 | + .use(htmlStringify) | 58 | 60 | .processSync(gtocMD).toString(); | 59 | 61 |
| 60 | 62 | const templatePath = path.join(docPath, 'template.html'); | 61 | 63 | const template = fs.readFileSync(templatePath, 'utf8'); | 62 | 64 |
| 63 | | -function toHTML({ input, filename, nodeVersion, analytics }, cb) { | | 65 | +function toHTML({ input, content, filename, nodeVersion, analytics }, cb) { | 64 | 66 | filename = path.basename(filename, '.md'); | 65 | 67 |
| 66 | | - const content = unified() | 67 | | - .use(markdown) | 68 | | - .use(firstHeader) | 69 | | - .use(preprocessText) | 70 | | - .use(preprocessElements, { filename }) | 71 | | - .use(buildToc, { filename }) | 72 | | - .use(remark2rehype, { allowDangerousHTML: true }) | 73 | | - .use(raw) | 74 | | - .use(html) | 75 | | - .processSync(input); | 76 | | - | 77 | 68 | const id = filename.replace(/\W+/g, '-'); | 78 | 69 |
| 79 | 70 | let HTML = template.replace('__ID__', id) | @@ -296,7 +287,7 @@ function parseYAML(text) { | 296 | 287 | .use(markdown) | 297 | 288 | .use(remark2rehype, { allowDangerousHTML: true }) | 298 | 289 | .use(raw) | 299 | | - .use(html) | | 290 | + .use(htmlStringify) | 300 | 291 | .processSync(change.description).toString(); | 301 | 292 |
| 302 | 293 | result += `<tr><td>${change.version}</td>\n` + | @@ -381,7 +372,7 @@ function buildToc({ filename }) { | 381 | 372 | .use(markdown) | 382 | 373 | .use(remark2rehype, { allowDangerousHTML: true }) | 383 | 374 | .use(raw) | 384 | | - .use(html) | | 375 | + .use(htmlStringify) | 385 | 376 | .processSync(toc).toString(); | 386 | 377 | }; | 387 | 378 | } | |
|