Skip to content

Commit 797eeaf

Browse files
committed
Fix many bugs related to HTML rendering
1 parent 81c6f17 commit 797eeaf

9 files changed

Lines changed: 70 additions & 32 deletions

File tree

lib/beautifier.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const OpenAPISampler = require('openapi-sampler');
22
const _ = require('lodash');
3+
const slugg = require('slugg');
34
const md = require('markdown-it')();
45

56
const sharedStart = (array) => {
@@ -41,25 +42,31 @@ const resolveAllOf = (schema) => {
4142

4243
const 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+
4451
const 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 => {
104116
module.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

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"oas-validator": "^1.1.7",
5757
"openapi-sampler": "^1.0.0-beta.13",
5858
"project-name-generator": "2.1.1",
59+
"slugg": "^1.2.1",
5960
"word-wrap": "1.1.0"
6061
}
6162
}

templates/markdown/.partials/operation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<a id="{{operation.slug}}" />
2+
13
{{#if operation.summary}}
24
> {{{operation.summary}}}
35

templates/markdown/.partials/param-prop.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<tr>
22
<td>{{tree path}}{{propName}} {{#if required}}<strong>(required)</strong>{{/if}}</td>
33
<td>
4-
{{prop.type}}
5-
{{~#if prop.anyOf}}anyOf{{~/if}}
6-
{{~#if prop.oneOf}}oneOf{{~/if}}
7-
{{~#if prop.items.type}}({{prop.items.type}}){{~/if}}
4+
{{#if prop.schema}}
5+
{{prop.schema.type}}
6+
{{~#if prop.schema.anyOf}}anyOf{{~/if}}
7+
{{~#if prop.schema.oneOf}}oneOf{{~/if}}
8+
{{~#if prop.schema.items.type}}({{prop.schema.items.type}}){{~/if}}
9+
{{else}}
10+
unknown
11+
{{/if}}
812
</td>
913
<td>{{prop.in}}</td>
10-
<td>{{{prop.schema.description}}}</td>
14+
<td>{{{prop.descriptionAsHTML}}}</td>
1115
<td>{{{acceptedValues prop.enum}}}</td>
1216
</tr>
1317
{{#each prop.anyOf}}

templates/markdown/.partials/schema-prop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{{~#if prop.oneOf}}oneOf{{~/if}}
77
{{~#if prop.items.type}}({{prop.items.type}}){{~/if}}
88
</td>
9-
<td>{{{prop.description}}}</td>
9+
<td>{{{prop.descriptionAsHTML}}}</td>
1010
<td>{{{acceptedValues prop.enum}}}</td>
1111
</tr>
1212
{{#each prop.anyOf}}

templates/markdown/.partials/schema.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{{#if renderSlug}}<a id="{{schema.slug}}" />{{/if}}
2+
13
{{#unless hideTitle}}
24
{{#if schema.title}}
35
#### {{schema.title}}

templates/markdown/.partials/schemas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
## Schemas
33

44
{{#each openapi.components.schemas}}
5-
{{~>schema schema=. schemaName=@key~}}
5+
{{~>schema schema=. schemaName=@key renderSlug=true~}}
66
{{/each}}
77
{{/if}}

templates/markdown/openapi.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# {{swagger.info.title}}
22

3-
{{{swagger.info.description}}}
3+
{{{swagger.info.descriptionAsHTML}}}
44

55
## Table of Contents
66

@@ -12,9 +12,23 @@
1212
{{/if}}
1313
{{#if openapi.paths}}
1414
* [Paths](#paths)
15+
{{#each openapi.paths as |path pathName|}}
16+
{{#each path as |op opName|}}
17+
{{#validMethod opName}}
18+
- [`{{uppercase opName}}` {{pathName}}](#{{op.slug}})
19+
{{/validMethod}}
20+
{{/each}}
21+
{{/each}}
1522
{{/if}}
1623
{{#if openapi.components.schemas}}
1724
* [Schemas](#schemas)
25+
{{#each openapi.components.schemas as |schema schemaName|}}
26+
{{#if schema.slug}}
27+
- [{{schema.title}}](#{{schema.slug}})
28+
{{else}}
29+
- {{schema.title}}
30+
{{/if}}
31+
{{/each}}
1832
{{/if}}
1933

2034
{{> info}}

0 commit comments

Comments
 (0)