diff --git a/lib/markdownBuilder.js b/lib/markdownBuilder.js index 2942fe97..e777585a 100644 --- a/lib/markdownBuilder.js +++ b/lib/markdownBuilder.js @@ -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 + '[]'); } @@ -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) ]; }))); } @@ -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) => { @@ -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), ]); diff --git a/lib/schemaProxy.js b/lib/schemaProxy.js index 6c2ee082..d15a8abb 100644 --- a/lib/schemaProxy.js +++ b/lib/schemaProxy.js @@ -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, }) => { @@ -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('#');