11const OpenAPISampler = require ( 'openapi-sampler' ) ;
22const _ = require ( 'lodash' ) ;
3+ const slugg = require ( 'slugg' ) ;
34const md = require ( 'markdown-it' ) ( ) ;
45
56const sharedStart = ( array ) => {
@@ -41,25 +42,31 @@ const resolveAllOf = (schema) => {
4142
4243const generateExample = schema => OpenAPISampler . sample ( schema ) ;
4344
45+ const mdToHTML = ( text ) => {
46+ const finalText = text || '' ;
47+ if ( finalText . match ( / \n / ) ) return md . render ( finalText ) ;
48+ return md . renderInline ( finalText ) ;
49+ } ;
50+
4451const beautifySchema = ( schema ) => {
4552 resolveAllOf ( schema ) ;
53+ schema . summaryAsHTML = mdToHTML ( schema . summary ) ;
54+ schema . descriptionAsHTML = mdToHTML ( schema . description ) ;
55+
56+ if ( schema . title ) schema . slug = `schema-${ slugg ( schema . title ) } ` ;
4657 if ( ! schema . example ) schema . generatedExample = generateExample ( schema ) ;
4758
48- _ . each ( schema . properties , ( prop ) => {
49- if ( prop . description ) prop . descriptionAsHTML = md . render ( prop . description || '' ) ;
50- if ( prop . properties ) beautifySchema ( prop ) ;
51- } ) ;
52- _ . each ( schema . additionalProperties , ( prop ) => {
53- if ( prop . description ) prop . descriptionAsHTML = md . render ( prop . description || '' ) ;
54- if ( prop . additionalProperties ) beautifySchema ( prop ) ;
55- } ) ;
59+ _ . each ( schema . properties , beautifySchema ) ;
60+ _ . each ( schema . additionalProperties , beautifySchema ) ;
61+ if ( schema . items ) beautifySchema ( schema . items ) ;
5662
5763 return schema ;
5864} ;
5965
60- const beautifyOperation = ( operation ) => {
61- operation . summaryAsHTML = md . render ( operation . summary || '' ) ;
62- operation . descriptionAsHTML = md . render ( operation . description || '' ) ;
66+ const beautifyOperation = ( operation , operationName , pathName ) => {
67+ operation . slug = slugg ( `op-${ operationName } -${ pathName } ` ) ;
68+ operation . summaryAsHTML = mdToHTML ( operation . summary ) ;
69+ operation . descriptionAsHTML = mdToHTML ( operation . description ) ;
6370
6471 if ( operation . requestBody ) {
6572 _ . each ( operation . requestBody . content , ( contentType ) => {
@@ -74,17 +81,22 @@ const beautifyOperation = (operation) => {
7481 operation . parameters . filter ( p => p . schema ) . forEach ( param => resolveAllOf ( param . schema ) ) ;
7582
7683 operation . parameters . forEach ( ( param ) => {
77- param . descriptionAsHTML = md . render ( param . description || '' ) ;
84+ param . descriptionAsHTML = mdToHTML ( param . description ) ;
85+ if ( param . schema ) beautifySchema ( param . schema ) ;
7886 } ) ;
7987 }
8088
8189 if ( operation . responses ) {
8290 _ . each ( operation . responses , ( response ) => {
83- if ( response . content && response . content . schema ) {
84- resolveAllOf ( response . content . schema ) ;
91+ if ( response . content ) {
92+ _ . each ( response . content , ( contentType ) => {
93+ beautifySchema ( contentType . schema ) ;
94+ } ) ;
8595 }
86- if ( response . headers && response . headers . schema ) {
87- resolveAllOf ( response . headers . schema ) ;
96+ if ( response . headers ) {
97+ _ . each ( response . headers , ( contentType ) => {
98+ beautifySchema ( contentType . schema ) ;
99+ } ) ;
88100 }
89101 } ) ;
90102 }
@@ -104,23 +116,21 @@ const cleanBrackets = text => {
104116module . exports = ( openapi ) => {
105117 openapi . basePath = openapi . basePath || '' ;
106118 openapi . info = openapi . info || { } ;
107- openapi . info . descriptionAsHTML = md . render ( openapi . info . description || '' ) ;
119+ openapi . info . descriptionAsHTML = mdToHTML ( openapi . info . description ) ;
108120
109121 if ( ! openapi . components ) openapi . components = { } ;
110122 if ( ! openapi . components . schemas ) {
111123 openapi . __noSchemas = true ;
112124 openapi . components . schemas = { } ;
113125 }
114126
115- _ . each ( openapi . components . schemas , ( schema , schemaName ) => {
116- openapi . components . schemas [ schemaName ] = beautifySchema ( schema ) ;
117- } ) ;
127+ _ . each ( openapi . components . schemas , beautifySchema ) ;
118128
119129 if ( openapi . servers ) {
120130 _ . each ( openapi . servers , server => {
121- server . descriptionAsHTML = md . render ( server . description || '' ) ;
131+ server . descriptionAsHTML = mdToHTML ( server . description ) ;
122132 _ . each ( server . variables , variable => {
123- variable . descriptionAsHTML = md . render ( variable . description || '' ) ;
133+ variable . descriptionAsHTML = mdToHTML ( variable . description ) ;
124134 } ) ;
125135 } ) ;
126136 }
@@ -134,7 +144,7 @@ module.exports = (openapi) => {
134144 throw new Error ( `Security definition "${ name } " is not in included in #/components/securitySchemes.` ) ;
135145 }
136146
137- openapi . components . securitySchemes [ name ] . descriptionAsHTML = md . render ( openapi . components . securitySchemes [ name ] . description || '' ) ;
147+ openapi . components . securitySchemes [ name ] . descriptionAsHTML = mdToHTML ( openapi . components . securitySchemes [ name ] . description ) ;
138148 openapi . _security . push ( openapi . components . securitySchemes [ name ] ) ;
139149 } ) ;
140150 }
@@ -153,7 +163,7 @@ module.exports = (openapi) => {
153163 const httpMethods = [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'COPY' , 'HEAD' , 'OPTIONS' , 'LINK' , 'UNLIK' , 'PURGE' , 'LOCK' , 'UNLOCK' , 'PROPFIND' ] ;
154164
155165 _ . each ( path , ( operation , operationName ) => {
156- if ( httpMethods . includes ( operationName . toUpperCase ( ) ) ) beautifyOperation ( operation ) ;
166+ if ( httpMethods . includes ( operationName . toUpperCase ( ) ) ) beautifyOperation ( operation , operationName , pathName ) ;
157167 } ) ;
158168 } ) ;
159169
0 commit comments