Skip to content

Commit

Permalink
feat(markdown): show examples
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 10, 2019
1 parent cc07df2 commit c8e8dfa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
const merged = !!(definition.allOf || definition.anyOf || definition.oneOf ||definition.not);

if (array && definition.items) {
console.log('an array!');
return maketypefact(definition.items, isarray + '[]');
}

Expand Down Expand Up @@ -308,7 +307,8 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
heading(level + 1, text(name)),
description,
paragraph(inlineCode(name)),
makefactlist(name, definition, required)
makefactlist(name, definition, required),
...makeexamples(definition, level + 1)
];
})));
}
Expand Down Expand Up @@ -354,6 +354,18 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
return [];
}

function makeexamples(schema, level = 1) {
if (schema.examples && schema.examples.length > 0) {
return [
heading(level + 1, text(i18n`${schema[s.parent]? schema[s.pointer].split('/').pop() : gentitle(schema[s.titles], schema.type)} Examples`)),
...schema.examples.map(example => paragraph(code('json', JSON.stringify(example, undefined, 2))))
]
}
return [
paragraph(text(i18n`no examples provided`))
];
}

console.log('generating markdown');
return (schemas) => {
return foldl(schemas, {}, (pv, schema) => {
Expand All @@ -363,6 +375,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
pv[schema[s.slug]] = root([
// todo add more elements
...makeheader(schema),
...makeexamples(schema, 1),
...makedefinitions(schema, slugger),
...makeproperties(schema, slugger),
]);
Expand Down
15 changes: 14 additions & 1 deletion lib/schemaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@
* governing permissions and limitations under the License.
*/
const ghslugger = require('github-slugger');
const { basename } = require('path');
const { basename, dirname, resolve } = require('path');
const { formatmeta } = require('./formatInfo');
const symbols = require('./symbols');

const myslug = Symbol('myslug');

function loadExamples(file, num = 1) {
const examplefile = resolve(dirname(file), basename(file).replace(/\..*$/, '.example.' + num + '.json'));
try {
const example = require(examplefile);
return [example, ...loadExamples(file, num + 1)];
} catch {
return [];
}
}

const handler = ({
root = '', filename = '.', schemas, parent, slugger,
}) => {
Expand Down Expand Up @@ -86,6 +96,9 @@ const handler = ({
}

const retval = Reflect.get(target, prop, receiver);
if (retval === undefined && prop === 'examples' && !receiver[symbols.parent]) {
return loadExamples(receiver[symbols.filename], 1);
}
if (typeof retval === 'object') {
if (retval.$ref) {
const [uri, pointer] = retval.$ref.split('#');
Expand Down

0 comments on commit c8e8dfa

Please sign in to comment.