yaml example support

This commit is contained in:
Lionel Villard 2018-07-12 17:03:16 -04:00
parent 624f6811c7
commit a37005d564
5 changed files with 51 additions and 13 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
*cache *cache
*.log *.log
node_modules node_modules
.history

View File

@ -1,10 +1,10 @@
var Handlebars = require('handlebars') var Handlebars = require('handlebars')
var common = require('../lib/common') var common = require('../lib/common')
module.exports = function(value, options) { module.exports = function(value, options) {
var cloned = common.formatExample(value, options.data.root, options.hash) var cloned = common.formatExample(value, options.data.root, options.hash)
if (!cloned) if (!cloned)
return ''; return '';
var html = common.printSchema(cloned) var html = common.printSchema(cloned, options.hash.yaml === true)
return new Handlebars.SafeString(html) return new Handlebars.SafeString(html)
}; };

View File

@ -1,6 +1,7 @@
var cheerio = require('cheerio') var cheerio = require('cheerio')
var marked = require('marked') var marked = require('marked')
var highlight = require('highlight.js') var highlight = require('highlight.js')
var yaml = require('js-yaml')
var _ = require('lodash') var _ = require('lodash')
var common = { var common = {
@ -84,7 +85,7 @@ var common = {
// throw 'Cannot format NULL object ' + value; // throw 'Cannot format NULL object ' + value;
return; return;
} }
if (value.example) { if (value.example) {
return value.example; return value.example;
} }
@ -158,16 +159,16 @@ var common = {
console.error('Cannot format example on property ', ref, ref.$ref) console.error('Cannot format example on property ', ref, ref.$ref)
}, },
printSchema: function(value) { printSchema: function(value, toyaml) {
if (!value) { if (!value) {
return ''; return '';
} }
var schemaString = JSON.stringify(value, null, 2) var schemaString = toyaml ? yaml.safeDump(value, {skipInvalid:true}) : JSON.stringify(value, null, 2)
// Add an extra CRLR before the code so the postprocessor can determine
// Add an extra CRLR before the code so the postprocessor can determine // the correct line indent for the <pre> tag.
// the correct line indent for the <pre> tag.
var $ = cheerio.load(marked("```json\r\n" + schemaString + "\n```")) var $ = cheerio.load(marked(toyaml ? "```yaml\r\n" + schemaString + "```" : "```json\r\n" + schemaString + "\n```"))
var definitions = $('span:not(:has(span)):contains("#/definitions/")') var definitions = $('span:not(:has(span)):contains("#/definitions/")')
definitions.each(function(index, item) { definitions.each(function(index, item) {
var ref = $(item).html() var ref = $(item).html()
@ -175,7 +176,7 @@ var common = {
// TODO: This should be done in a template // TODO: This should be done in a template
$(item).html("<a href=" + refLink + ">" + ref + "</a>") $(item).html("<a href=" + refLink + ">" + ref + "</a>")
}) })
// Remove trailing whitespace before code tag // Remove trailing whitespace before code tag
// var re = /([\n\r\s]+)(<\/code>)/g; // var re = /([\n\r\s]+)(<\/code>)/g;
// str = $.html().replace(re, '$2') // str = $.html().replace(re, '$2')
@ -183,7 +184,7 @@ var common = {
// return '<pre><code class="hljs lang-json">' + // return '<pre><code class="hljs lang-json">' +
// this.highlight(schemaString, 'json') + // this.highlight(schemaString, 'json') +
// '</code></pre>'; // '</code></pre>';
return $.html() return $.html()
}, },

View File

@ -1,3 +1,3 @@
<!-- <div class="hljs"> --> <!-- <div class="hljs"> -->
{{printExample . showReadOnly=showReadOnly}} {{printExample . showReadOnly=showReadOnly yaml=false}}
<!-- </div> --> <!-- </div> -->

36
test/fixtures/Example.yml vendored Normal file
View File

@ -0,0 +1,36 @@
swagger: '2.0'
paths:
definitions:
Order:
type: object
properties:
id:
type: integer
format: int64
cheeseId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
example:
cheeseId: '1'
complete: 'true'
id: '1'
quantity: '66'
shipDate: '04-10-2017 00:09:21 UTC'
status: available