Skip to content

Commit 56b7a2f

Browse files
feat(markdownbuilder): create an option to skip any property in markdown #243
1 parent 9cd5437 commit 56b7a2f

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

lib/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ async function main(args) {
9999
.alias('h', 'header')
100100
.boolean('h')
101101
.describe('h', 'if the value is false the header will be skipped')
102-
.default('h', true);
102+
.default('h', true)
103+
104+
.alias('s', 'skip')
105+
.array('s')
106+
.describe('s', 'name of a default property to skip in markdown (may be used multiple times), e.g. -s typefact -s proptable')
107+
.default('s', []);
103108

104109
const docs = pipe(
105110
iter(argv),
@@ -168,6 +173,7 @@ async function main(args) {
168173
links: docs,
169174
includeproperties: argv.p,
170175
exampleformat: argv.f,
176+
skipproperties: argv.s,
171177
rewritelinks: (origin) => {
172178
const mddir = argv.o;
173179
const srcdir = argv.d;

lib/markdownBuilder.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function build({
3030
includeproperties = [],
3131
rewritelinks = x => x,
3232
exampleformat = 'json',
33+
skipproperties = [],
3334
} = {}) {
3435
const formats = {
3536
'date-time': {
@@ -325,6 +326,9 @@ function build({
325326
* @param {*} props
326327
*/
327328
function makeproptable(props = {}, patternProps = {}, additionalProps, required, slugger) {
329+
if (skipproperties.includes('proptable')) {
330+
return paragraph();
331+
}
328332
const proprows = Object
329333
.entries(props)
330334
.map(makepropheader(required, false, slugger));
@@ -362,6 +366,9 @@ function build({
362366
}
363367

364368
function makearrayfact(items, additional) {
369+
if (skipproperties.includes('arrayfact')) {
370+
return '';
371+
}
365372
return listItem([
366373
paragraph([text(i18n`Type: `), text(i18n`an array where each item follows the corresponding schema in the following list:`)]),
367374
list('ordered',
@@ -463,9 +470,15 @@ function build({
463470
children.push(listItem(text(i18n`is optional`)));
464471
}
465472

466-
children.push(maketypefact(definition));
467-
children.push(makenullablefact(definition));
468-
children.push(makedefinedinfact(definition));
473+
if (!skipproperties.includes('typefact')) {
474+
children.push(maketypefact(definition));
475+
}
476+
if (!skipproperties.includes('nullablefact')) {
477+
children.push(makenullablefact(definition));
478+
}
479+
if (!skipproperties.includes('definedinfact')) {
480+
children.push(makedefinedinfact(definition));
481+
}
469482

470483
const additionalfacts = includeproperties.map((propname) => {
471484
if (definition[propname]) {
@@ -524,6 +537,9 @@ function build({
524537
}
525538

526539
function maketypesection(schema, level = 1) {
540+
if (skipproperties.includes('typesection')) {
541+
return '';
542+
}
527543
const { children } = maketypefact(schema);
528544
children[0].children.shift();
529545
return [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Complete JSON Schema",
4+
"description": "Testing for skipped props",
5+
"type": "object",
6+
"properties": {
7+
"foo": {
8+
"type": "string"
9+
},
10+
"bar": {
11+
"description": "Configures the bar property",
12+
"type": "boolean",
13+
"default": "true"
14+
},
15+
"foobar": {
16+
"description": "Overwrite the bar value",
17+
"examples":[{
18+
"bar": "true",
19+
"foobar": "no"
20+
}],
21+
"type": "string",
22+
"format": "yes/no"
23+
}
24+
},
25+
"additionalProperties": false,
26+
"required": [
27+
"bar"
28+
]
29+
}

test/markdownBuilder.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,31 @@ Reference this group by using
402402
.contains('# Simple Schema');
403403
});
404404
});
405+
406+
describe('Testing Markdown Builder: Skip properties', () => {
407+
let schemas;
408+
409+
before(async () => {
410+
schemas = await loadschemas('skipproperties');
411+
});
412+
413+
it('Skipped properties exist', () => {
414+
const builder = build({});
415+
const results = builder(schemas);
416+
417+
assertMarkdown(results.complete)
418+
.contains('### bar Type')
419+
.contains('| Property | Type | Required |')
420+
.contains('- defined in: [Complete JSON Schema]');
421+
});
422+
423+
it('Skips the expected properties', () => {
424+
const builder = build({ skipproperties: ['typesection', 'definedinfact', 'proptable'] });
425+
const results = builder(schemas);
426+
427+
assertMarkdown(results.complete)
428+
.doesNotContain('### bar Type')
429+
.doesNotContain('| Property | Type | Required |')
430+
.doesNotContain('- defined in: [Complete JSON Schema]');
431+
});
432+
});

test/testUtils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ function assertMarkdown(node) {
2626
const tester = {};
2727
tester.contains = (str) => {
2828
assert.ok(result.indexOf(str) >= 0, `Markdown output does not contain search string "${str}"
29+
${result}`);
30+
return tester;
31+
};
32+
tester.doesNotContain = (str) => {
33+
assert.ok(result.indexOf(str) < 0, `Markdown output contains search string "${str}"
2934
${result}`);
3035
return tester;
3136
};

0 commit comments

Comments
 (0)