diff --git a/examples/docs/README.md b/examples/docs/README.md index 3af4eca2efde0fcff27e760d8f3e7e1e952a99fb..3d8ad58703bde17e2f6edcdcd234edf39b25cacf 100644 --- a/examples/docs/README.md +++ b/examples/docs/README.md @@ -11,6 +11,7 @@ * [Custom](./deepextending.schema.md) – `https://example.com/schemas/deepextending` * [Definitions](./definitions.schema.md) – `https://example.com/schemas/definitions` * [Example](./example.schema.md) – `https://example.com/schemas/example` +* [Examples](./examples.schema.md) – `https://example.com/schemas/examples` * [Extending](./extending.schema.md) – `https://example.com/schemas/extending` * [Extensible](./extensible.schema.md) – `https://example.com/schemas/extensible` * [Simple](./simple.schema.md) – `https://example.com/schemas/simple` diff --git a/examples/docs/example.schema.md b/examples/docs/example.schema.md index ce447679ca0588a852bf9ff4227dc9317986708b..17925e40fc7f54966d9874331e69d2a37c790ad5 100644 --- a/examples/docs/example.schema.md +++ b/examples/docs/example.schema.md @@ -15,4 +15,9 @@ This is an example schema with examples. Too many examples? There can never be t |----------|------------|-------------------|------------| | Can be instantiated | No | Forbidden | [example.schema.json](example.schema.json) | +## Example Example +```json +{"foo":"bar","bar":"baz"} +``` + --- \ No newline at end of file diff --git a/examples/docs/examples.schema.md b/examples/docs/examples.schema.md new file mode 100644 index 0000000000000000000000000000000000000000..641b7da504a355029e3faccfc72e0277a39991bd --- /dev/null +++ b/examples/docs/examples.schema.md @@ -0,0 +1,29 @@ +--- +template: reference +foo: bar +--- + +# Examples Schema + +``` +https://example.com/schemas/examples +``` + +This is an example schema with *multiple* examples. Too many examples? There can never be too many examples! + +| Abstract | Extensible | Custom Properties | Defined In | +|----------|------------|-------------------|------------| +| Can be instantiated | No | Forbidden | [examples.schema.json](examples.schema.json) | + +## Examples Examples + +```json +{"foo":"bi","bar":"bu"} +``` + +```json +{"foo":"zip","bar":"zap"} +``` + + +--- \ No newline at end of file diff --git a/examples/generated-schemas/examples.schema.json b/examples/generated-schemas/examples.schema.json new file mode 100644 index 0000000000000000000000000000000000000000..91c9b2a35922886e33695e237884245e18796a5f --- /dev/null +++ b/examples/generated-schemas/examples.schema.json @@ -0,0 +1,38 @@ +{ + "meta:license": [ + "Copyright 2017 Adobe Systems Incorporated. All rights reserved.", + "This file is licensed to you under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License. You may obtain a copy", + "of the License at http://www.apache.org/licenses/LICENSE-2.0" + ], + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "https://example.com/schemas/examples", + "title": "Examples", + "type": "object", + "description": "This is an example schema with *multiple* examples. Too many examples? There can never be too many examples!", + "properties": { + "foo": { + "type": "string", + "description": "A simple string.", + "meta:example": "bar" + }, + "bar": { + "type": "string", + "description": "A simple string.", + "meta:example": [ + "bar", + "baz" + ] + } + }, + "examples": [ + { + "foo": "bi", + "bar": "bu" + }, + { + "foo": "zip", + "bar": "zap" + } + ] +} \ No newline at end of file diff --git a/examples/schemas/examples.example.1.json b/examples/schemas/examples.example.1.json new file mode 100644 index 0000000000000000000000000000000000000000..ab2d990f25d3282b48a3c71aeaf14f9eacddaf08 --- /dev/null +++ b/examples/schemas/examples.example.1.json @@ -0,0 +1,4 @@ +{ + "foo": "bi", + "bar": "bu" +} \ No newline at end of file diff --git a/examples/schemas/examples.example.2.json b/examples/schemas/examples.example.2.json new file mode 100644 index 0000000000000000000000000000000000000000..36b50ca730df90739f09cd17bfd2edb32ceb9369 --- /dev/null +++ b/examples/schemas/examples.example.2.json @@ -0,0 +1,4 @@ +{ + "foo": "zip", + "bar": "zap" +} \ No newline at end of file diff --git a/lib/markdownWriter.js b/lib/markdownWriter.js index d7ba95752759ee0a79cf04212a30fb2919d2dce8..d8d19c69e9542dd60bf2c2d5b30c2b43f85b784e 100644 --- a/lib/markdownWriter.js +++ b/lib/markdownWriter.js @@ -62,6 +62,17 @@ function flatten(dependencies) { return deps; } +function stringifyExamples(examples) { + if (examples) { + console.log(examples); + return examples.map(example => { + return JSON.stringify(example, 2); + }); + } else { + return false; + } +} + const generateMarkdown = function(filename, schema, schemaPath, outDir, dependencyMap) { var ctx = { schema: schema, @@ -83,6 +94,7 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen props: schemaProps(schema, schemaPath, filename) } ], //[ 'divider.ejs', null ], //[ 'topSchema.ejs', ctx ], + [ 'examples.ejs', { examples: stringifyExamples(schema.examples), title: schema.title } ], [ 'divider.ejs', null ] ].map(([ template, context ]) => { return [ diff --git a/templates/md/examples.ejs b/templates/md/examples.ejs new file mode 100644 index 0000000000000000000000000000000000000000..76c997ddd0d98f1feb0c73a0e205d1eebdd84808 --- /dev/null +++ b/templates/md/examples.ejs @@ -0,0 +1,21 @@ +<% /** + * Copyright 2017 Adobe Systems Incorporated. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + */ %><% if (examples && examples.length == 1) { %> +## <%=title %> Example + +```json +<%- examples[0] %> +``` +<% } else if (examples && examples.length > 1) { %> +## <%=title %> Examples +<% _.forEach(examples, example => { %> + +```json +<%- example %> +``` + +<% }); %> +<% } %> \ No newline at end of file