Skip to content

Commit

Permalink
feat(examples): enable examples to be formatted in YAML using command…
Browse files Browse the repository at this point in the history
… line option `-f yaml`

When using the command line option `-f yaml` or `--example format yaml`, all examples will be
formatted as YAML instead of JSON
  • Loading branch information
trieloff committed Mar 4, 2020
1 parent a2bbe29 commit 01119ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ async function main(args) {
.choices('l', ['en_US', 'de'])
.default('l', 'en_US')

.alias('f', 'example-format')
.describe('f', 'how to format examples')
.choices('f', ['json', 'yaml'])
.default('f', 'json')

.alias('p', 'properties')
.array('p')
.describe('p', 'name of a custom property which should be also in the description of an element (may be used multiple times)')
Expand Down Expand Up @@ -161,6 +166,7 @@ async function main(args) {
header: argv.h,
links: docs,
includeproperties: argv.p,
exampleformat: argv.f,
rewritelinks: (origin) => {
const mddir = argv.o;
const srcdir = argv.d;
Expand Down
15 changes: 13 additions & 2 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ const {
} = require('mdast-builder');
const i18n = require('es2015-i18n-tag').default;
const ghslugger = require('github-slugger');
const yaml = require('js-yaml');
const s = require('./symbols');
const { gentitle } = require('./formattingTools');
const { keyword } = require('./keywords');

function build({
header, links = {}, includeproperties = [], rewritelinks = x => x,
header,
links = {},
includeproperties = [],
rewritelinks = x => x,
exampleformat = 'json',
} = {}) {
const formats = {
'date-time': {
Expand Down Expand Up @@ -656,7 +661,13 @@ function build({


function makeexamples(schema, level = 1) {
if (schema[keyword`examples`] && schema[keyword`examples`].length > 0) {
if (schema[keyword`examples`] && schema[keyword`examples`].length > 0 && exampleformat === 'yaml') {
return [
heading(level + 1, text(i18n`${simpletitle(schema)} Examples`)),
...schema[keyword`examples`].map(example => paragraph(code('yaml', yaml.safeDump(example, undefined, 2)))),
];
}
if (schema[keyword`examples`] && schema[keyword`examples`].length > 0 && exampleformat === 'json') {
return [
heading(level + 1, text(i18n`${simpletitle(schema)} Examples`)),
...schema[keyword`examples`].map(example => paragraph(code('json', JSON.stringify(example, undefined, 2)))),
Expand Down

0 comments on commit 01119ba

Please sign in to comment.