1+ import yaml from 'js-yaml' ;
2+
13import type { OpenAPIV3 } from '@gitbook/openapi-parser' ;
24import type { OpenAPIContext , OpenAPIUniversalContext } from './context' ;
35import { json2xml } from './json2xml' ;
@@ -13,7 +15,7 @@ export function OpenAPIExample(props: {
1315 syntax : string ;
1416} ) {
1517 const { example, context, syntax } = props ;
16- const code = stringifyExample ( { example, xml : syntax === 'xml' } ) ;
18+ const code = stringifyExample ( { example, syntax } ) ;
1719
1820 if ( code === null ) {
1921 return < OpenAPIEmptyExample context = { context } /> ;
@@ -22,8 +24,10 @@ export function OpenAPIExample(props: {
2224 return context . renderCodeBlock ( { code, syntax } ) ;
2325}
2426
25- function stringifyExample ( args : { example : OpenAPIV3 . ExampleObject ; xml : boolean } ) : string | null {
26- const { example, xml } = args ;
27+ function stringifyExample ( args : { example : OpenAPIV3 . ExampleObject ; syntax : string } ) :
28+ | string
29+ | null {
30+ const { example, syntax } = args ;
2731
2832 if ( ! example . value ) {
2933 return null ;
@@ -33,10 +37,14 @@ function stringifyExample(args: { example: OpenAPIV3.ExampleObject; xml: boolean
3337 return example . value ;
3438 }
3539
36- if ( xml ) {
40+ if ( syntax === ' xml' ) {
3741 return json2xml ( example . value ) ;
3842 }
3943
44+ if ( syntax === 'yaml' ) {
45+ return yaml . dump ( example . value ) . replace ( / ' / g, '' ) . replace ( / \\ n / g, '\n' ) ;
46+ }
47+
4048 return stringifyOpenAPI ( example . value , null , 2 ) ;
4149}
4250
0 commit comments