Skip to content

Commit

Permalink
feat(markdownbuilder): create an option to skip any property in markd…
Browse files Browse the repository at this point in the history
…own #243
  • Loading branch information
florevallatmrf committed Aug 19, 2020
1 parent 9cd5437 commit 56b7a2f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ async function main(args) {
.alias('h', 'header')
.boolean('h')
.describe('h', 'if the value is false the header will be skipped')
.default('h', true);
.default('h', true)

.alias('s', 'skip')
.array('s')
.describe('s', 'name of a default property to skip in markdown (may be used multiple times), e.g. -s typefact -s proptable')
.default('s', []);

const docs = pipe(
iter(argv),
Expand Down Expand Up @@ -168,6 +173,7 @@ async function main(args) {
links: docs,
includeproperties: argv.p,
exampleformat: argv.f,
skipproperties: argv.s,
rewritelinks: (origin) => {
const mddir = argv.o;
const srcdir = argv.d;
Expand Down
22 changes: 19 additions & 3 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function build({
includeproperties = [],
rewritelinks = x => x,
exampleformat = 'json',
skipproperties = [],
} = {}) {
const formats = {
'date-time': {
Expand Down Expand Up @@ -325,6 +326,9 @@ function build({
* @param {*} props
*/
function makeproptable(props = {}, patternProps = {}, additionalProps, required, slugger) {
if (skipproperties.includes('proptable')) {
return paragraph();
}
const proprows = Object
.entries(props)
.map(makepropheader(required, false, slugger));
Expand Down Expand Up @@ -362,6 +366,9 @@ function build({
}

function makearrayfact(items, additional) {
if (skipproperties.includes('arrayfact')) {
return '';
}
return listItem([
paragraph([text(i18n`Type: `), text(i18n`an array where each item follows the corresponding schema in the following list:`)]),
list('ordered',
Expand Down Expand Up @@ -463,9 +470,15 @@ function build({
children.push(listItem(text(i18n`is optional`)));
}

children.push(maketypefact(definition));
children.push(makenullablefact(definition));
children.push(makedefinedinfact(definition));
if (!skipproperties.includes('typefact')) {
children.push(maketypefact(definition));
}
if (!skipproperties.includes('nullablefact')) {
children.push(makenullablefact(definition));
}
if (!skipproperties.includes('definedinfact')) {
children.push(makedefinedinfact(definition));
}

const additionalfacts = includeproperties.map((propname) => {
if (definition[propname]) {
Expand Down Expand Up @@ -524,6 +537,9 @@ function build({
}

function maketypesection(schema, level = 1) {
if (skipproperties.includes('typesection')) {
return '';
}
const { children } = maketypefact(schema);
children[0].children.shift();
return [
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/skipproperties/complete.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complete JSON Schema",
"description": "Testing for skipped props",
"type": "object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"description": "Configures the bar property",
"type": "boolean",
"default": "true"
},
"foobar": {
"description": "Overwrite the bar value",
"examples":[{
"bar": "true",
"foobar": "no"
}],
"type": "string",
"format": "yes/no"
}
},
"additionalProperties": false,
"required": [
"bar"
]
}
28 changes: 28 additions & 0 deletions test/markdownBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,31 @@ Reference this group by using
.contains('# Simple Schema');
});
});

describe('Testing Markdown Builder: Skip properties', () => {
let schemas;

before(async () => {
schemas = await loadschemas('skipproperties');
});

it('Skipped properties exist', () => {
const builder = build({});
const results = builder(schemas);

assertMarkdown(results.complete)
.contains('### bar Type')
.contains('| Property | Type | Required |')
.contains('- defined in: [Complete JSON Schema]');
});

it('Skips the expected properties', () => {
const builder = build({ skipproperties: ['typesection', 'definedinfact', 'proptable'] });
const results = builder(schemas);

assertMarkdown(results.complete)
.doesNotContain('### bar Type')
.doesNotContain('| Property | Type | Required |')
.doesNotContain('- defined in: [Complete JSON Schema]');
});
});
5 changes: 5 additions & 0 deletions test/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function assertMarkdown(node) {
const tester = {};
tester.contains = (str) => {
assert.ok(result.indexOf(str) >= 0, `Markdown output does not contain search string "${str}"
${result}`);
return tester;
};
tester.doesNotContain = (str) => {
assert.ok(result.indexOf(str) < 0, `Markdown output contains search string "${str}"
${result}`);
return tester;
};
Expand Down

0 comments on commit 56b7a2f

Please sign in to comment.