Skip to content

Commit

Permalink
get test-ci passing again
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Jul 24, 2018
1 parent 3a4489a commit 81d3985
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
30 changes: 28 additions & 2 deletions test/doctool/test-doctool-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,34 @@ try {
const assert = require('assert');
const { readFile } = require('fs');
const fixtures = require('../common/fixtures');
const toHTML = require('../../tools/doc/html.js');
const html = require('../../tools/doc/html.js');
const path = require('path');

module.paths.unshift(
path.join(__dirname, '..', '..', 'tools', 'doc', 'node_modules'));
const unified = require('unified');
const markdown = require('remark-parse');
const remark2rehype = require('remark-rehype');
const raw = require('rehype-raw');
const htmlStringify = require('rehype-stringify');

function toHTML({ input, filename, nodeVersion, analytics }, cb) {
const content = unified()
.use(markdown)
.use(html.firstHeader)
.use(html.preprocessText)
.use(html.preprocessElements, { filename })
.use(html.buildToc, { filename })
.use(remark2rehype, { allowDangerousHTML: true })
.use(raw)
.use(htmlStringify)
.processSync(input);

html.toHTML(
{ input, content, filename, nodeVersion, analytics },
cb
);
}

// Test data is a list of objects with two properties.
// The file property is the file path.
Expand Down Expand Up @@ -80,7 +107,6 @@ testData.forEach(({ file, html, analyticsId }) => {

readFile(file, 'utf8', common.mustCall((err, input) => {
assert.ifError(err);

toHTML(
{
input: input,
Expand Down
44 changes: 30 additions & 14 deletions test/doctool/test-doctool-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ try {

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const fixtures = require('../common/fixtures');
const json = require('../../tools/doc/json.js');

module.paths.unshift(
path.join(__dirname, '..', '..', 'tools', 'doc', 'node_modules'));
const unified = require('unified');
const markdown = require('remark-parse');

function toJSON(input, filename, cb) {
function nullCompiler() {
this.Compiler = (tree) => tree;
}

unified()
.use(markdown)
.use(json.jsonAPI, { filename })
.use(nullCompiler)
.process(input, cb);
}

// Outputs valid json with the expected fields when given simple markdown
// Test data is a list of objects with two properties.
// The file property is the file path.
Expand All @@ -21,15 +39,16 @@ const testData = [
{
file: fixtures.path('sample_document.md'),
json: {
type: 'module',
source: 'foo',
modules: [{
textRaw: 'Sample Markdown',
name: 'sample_markdown',
modules: [{
textRaw: 'Seussian Rhymes',
name: 'seussian_rhymes',
desc: '<ol>\n<li>fish</li>\n<li><p>fish</p>\n</li>\n<li>' +
'<p>Red fish</p>\n</li>\n<li>Blue fish</li>\n</ol>\n',
desc: '<ol>\n<li>fish</li>\n<li>fish</li>\n</ol>\n' +
'<ul>\n<li>Red fish</li>\n<li>Blue fish</li>\n</ul>',
type: 'module',
displayName: 'Seussian Rhymes'
}],
Expand All @@ -41,6 +60,7 @@ const testData = [
{
file: fixtures.path('order_of_end_tags_5873.md'),
json: {
type: 'module',
source: 'foo',
modules: [{
textRaw: 'Title',
Expand All @@ -55,15 +75,10 @@ const testData = [
signatures: [
{
params: [{
textRaw: '`array` {Array} ',
textRaw: '`array` {Array}',
name: 'array',
type: 'Array'
}]
},
{
params: [{
name: 'array'
}]
}
]
}],
Expand All @@ -78,6 +93,7 @@ const testData = [
{
file: fixtures.path('doc_with_yaml.md'),
json: {
type: 'module',
source: 'foo',
modules: [
{
Expand All @@ -92,7 +108,7 @@ const testData = [
changes: []
},
desc: '<p>Describe <code>Foobar</code> in more detail ' +
'here.</p>\n',
'here.</p>',
type: 'module',
displayName: 'Foobar'
},
Expand All @@ -110,7 +126,7 @@ const testData = [
]
},
desc: '<p>Describe <code>Foobar II</code> in more detail ' +
'here. fg(1)</p>\n',
'here. fg(1)</p>',
type: 'module',
displayName: 'Foobar II'
},
Expand All @@ -123,15 +139,15 @@ const testData = [
changes: []
},
desc: '<p>Describe <code>Deprecated thingy</code> in more ' +
'detail here. fg(1p)</p>\n',
'detail here. fg(1p)</p>',
type: 'module',
displayName: 'Deprecated thingy'
},
{
textRaw: 'Something',
name: 'something',
desc: '<!-- This is not a metadata comment -->\n<p>' +
'Describe <code>Something</code> in more detail here.</p>\n',
'Describe <code>Something</code> in more detail here.</p>',
type: 'module',
displayName: 'Something'
}
Expand All @@ -147,9 +163,9 @@ const testData = [
testData.forEach((item) => {
fs.readFile(item.file, 'utf8', common.mustCall((err, input) => {
assert.ifError(err);
json(input, 'foo', common.mustCall((err, output) => {
toJSON(input, 'foo', common.mustCall((err, output) => {
assert.ifError(err);
assert.deepStrictEqual(output, item.json);
assert.deepStrictEqual(output.json, item.json);
}));
}));
});
8 changes: 6 additions & 2 deletions tools/doc/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fs.readFile(filename, 'utf8', (er, input) => {

const content = unified()
.use(markdown)
.use(json.jsonAPI, { filename, outputDir })
.use(json.jsonAPI, { filename })
.use(html.firstHeader)
.use(html.preprocessText)
.use(html.preprocessElements, { filename })
Expand All @@ -77,13 +77,17 @@ fs.readFile(filename, 'utf8', (er, input) => {
.use(htmlStringify)
.processSync(input);

const basename = path.basename(filename, '.md');

html.toHTML(
{ input, content, filename, nodeVersion, analytics },
(err, html) => {
const basename = path.basename(filename, '.md');
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));
});
10 changes: 3 additions & 7 deletions tools/doc/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ const unified = require('unified');
const common = require('./common.js');
const html = require('remark-html');
const select = require('unist-util-select');
const path = require('path');
const fs = require('fs');

module.exports = { jsonAPI };

// Unified processor: input is https://github.com/syntax-tree/mdast,
// output is: https://gist.github.com/1777387.
function jsonAPI({ filename, outputDir }) {
function jsonAPI({ filename }) {
return (tree, file) => {

const exampleHeading = /^example/i;
Expand All @@ -54,14 +52,12 @@ function jsonAPI({ filename, outputDir }) {
}
});

// Collect and output results.
// Collect and capture results.
const result = { type: 'module', source: filename };
while (sections.length > 0) {
doSection(sections.shift(), result);
}
const basename = path.basename(filename, '.md');
const target = path.join(outputDir, `${basename}.json`);
fs.writeFileSync(target, JSON.stringify(result, null, 2));
file.json = result;

// Process a single section (recursively, including subsections).
function doSection(section, parent) {
Expand Down

0 comments on commit 81d3985

Please sign in to comment.