diff --git a/.gitignore b/.gitignore index 2b2aeea..233f91a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *cache *.log node_modules +.history \ No newline at end of file diff --git a/app/helpers/printExample.js b/app/helpers/printExample.js index fc5d20e..14b6f32 100644 --- a/app/helpers/printExample.js +++ b/app/helpers/printExample.js @@ -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) }; diff --git a/app/lib/common.js b/app/lib/common.js index 173e91b..84ee3df 100644 --- a/app/lib/common.js +++ b/app/lib/common.js @@ -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
 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 
 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("" + ref + "")
     })
-
+    
     // 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 '
' +
     //   this.highlight(schemaString, 'json') +
     //   '
'; - + return $.html() }, diff --git a/app/views/partials/swagger/print-example.hbs b/app/views/partials/swagger/print-example.hbs index c835949..03d61aa 100644 --- a/app/views/partials/swagger/print-example.hbs +++ b/app/views/partials/swagger/print-example.hbs @@ -1,3 +1,3 @@ - {{printExample . showReadOnly=showReadOnly}} + {{printExample . showReadOnly=showReadOnly yaml=false}} diff --git a/test/fixtures/Example.yml b/test/fixtures/Example.yml new file mode 100644 index 0000000..a05f555 --- /dev/null +++ b/test/fixtures/Example.yml @@ -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 + \ No newline at end of file