Merge pull request #160 from lionelvillard/yaml

yaml example support
This commit is contained in:
Kam Low 2019-01-30 10:43:51 +01:00 committed by GitHub
commit 21252e92de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 13 deletions

1
.gitignore vendored
View File

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

View File

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

View File

@ -1,6 +1,7 @@
var cheerio = require('cheerio')
var marked = require('marked')
var highlight = require('highlight.js')
var yaml = require('js-yaml')
var _ = require('lodash')
var common = {
@ -158,16 +159,16 @@ var common = {
console.error('Cannot format example on property ', ref, ref.$ref)
},
printSchema: function(value) {
printSchema: function(value, toyaml) {
if (!value) {
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
// 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/")')
definitions.each(function(index, item) {
var ref = $(item).html()

View File

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