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

@ -1,10 +1,10 @@
var Handlebars = require('handlebars')
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)
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 = {
@ -84,7 +85,7 @@ var common = {
// throw 'Cannot format NULL object ' + value;
return;
}
if (value.example) {
return value.example;
}
@ -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)
// 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 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(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()
@ -175,7 +176,7 @@ var common = {
// TODO: This should be done in a template
$(item).html("<a href=" + refLink + ">" + ref + "</a>")
})
// Remove trailing whitespace before code tag
// var re = /([\n\r\s]+)(<\/code>)/g;
// str = $.html().replace(re, '$2')
@ -183,7 +184,7 @@ var common = {
// return '<pre><code class="hljs lang-json">' +
// this.highlight(schemaString, 'json') +
// '</code></pre>';
return $.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