-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
248 lines (191 loc) · 6.57 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html>
<html>
<head>
<title>Swagger-to-Docs</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/cferdinandi/kraken@12.0.3/dist/main.min.css">
<style type="text/css">
body {
margin: 0 auto;
max-width: 80em;
width: 88%;
}
label {
font-weight: bold;
}
textarea {
height: 100%;
min-height: 12em;
width: 100%;
}
[role="status"] {
margin-bottom: 1em;
min-height: 2em;
font-style: italic;
}
</style>
</head>
<body>
<h1>Swagger-to-Docs</h1>
<div>
<label for="swagger">Swagger Definition</label>
<input type="file" accept=".json,application/json" id="swagger">
</div>
<div role="status"></div>
<details class="margin-bottom">
<summary class="margin-bottom-small"><strong>Get the HTML</strong></summary>
<label class="visually-hidden" for="markup">Documentation HTML</label>
<textarea id="markup" readonly></textarea>
</details>
<strong>Preview</strong>
<div id="preview"><em>Upload a Swagger definition to produce documenation.</em></div>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default%2CObject.values"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/cure53/dompurify@2/dist/purify.min.js"></script>
<script>
//
// @todo
//
// 1. Handle untagged items (or items that don't match an existing tag)
// 2. Make sure tags with no items don't return content
// 3. Add items property to params table
//
// Variables
//
// UI elements
var swagger = document.querySelector('#swagger');
var markup = document.querySelector('#markup');
var prevew = document.querySelector('#preview');
var notification = document.querySelector('[role="status"]');
//
// Methods
//
var getParamsTable = function (params) {
if (!params.length) return '';
return `
<h4>Parameters</h4>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>In</th>
<th>Values</th>
<th>Required</th>
</tr>
</thead>
<tbody>
${params.map(function (param) {
console.log(param.name, param.schema);
// Add item options if available
var val = [];
if (param.items && param.items.enum) {
val.push(param.items.enum.map(function (item) { return `<code>${item}</code>` }).join(', '));
}
if (param.items && param.items.default) {
val.push(`<strong>Default:</strong> <code>${param.items.default}</code>`)
}
return `
<tr>
<td><code>${param.name}</code></td>
<td>${param.description ? param.description : ''}</td>
<td>${param.type ? param.type : ''}</td>
<td>${param.in ? param.in : ''}</td>
<td>${val.join('<br>')}</td>
<td><code>${param.required ? param.required : 'false'}</code></td>
</tr>`;
}).join('')}
</tbody>
</table>`;
};
var generateSectionHTML = function (section, json) {
var html = Object.keys(json.paths).map(function (key) {
return Object.keys(json.paths[key]).map(function (method) {
var methodObj = json.paths[key][method];
// If this item isn't in this section, skip it
if (!methodObj.tags.includes(section.name)) return '';
return `
<h3><code>${method.toUpperCase()}</code> <code>${key}</code></h3>
${methodObj.description ? marked(methodObj.description) : ''}
${methodObj.summary && !methodObj.description ? marked(methodObj.summary) : ''}
${methodObj.consumes || methodObj.produces ? `
<ul>
${methodObj.consumes ? `<li><strong>Request Formats:</strong> ${methodObj.consumes.join(', ')}</li>` : ''}
${methodObj.produces ? `<li><strong>Response Formats:</strong> ${methodObj.produces.join(', ')}</li>` : ''}
</ul>` : ''}
${getParamsTable(methodObj.parameters)}
<h4>Sample Request</h4>
<p><em>Coming soon...</em></p>
<h4>Sample Response</h4>
<p><em>Coming soon...</em></p>`;
}).join('');
}).join('');
// If there's no content, return an empty string
if (!html.length) return '';
return `
<h2>${section.name}</h2>
${section.description ? marked(section.description) : ''}
${section.externalDocs ? Object.values(section.externalDocs).map(function (doc) { return marked.parseInline(doc); }).join(' ') : ''}
${html}`;
};
var generateHTML = function (json) {
// Get contact details
var contact = json.info.contact ? Object.values(json.info.contact).map(function (item) { return marked.parseInline(item); }) : null;
// Generate the HTML
var html =
`${json.info.description ? marked(json.info.description) : ''}
<ul class="docs-info">
${json.info.version ? `<li><strong>Version:</strong> ${json.info.version}</li>` : ''}
${json.info.termsOfService ? `<li><strong>Terms of Use:</strong> <a href="${json.info.termsOfService}">${json.info.termsOfService}</a></li>` : ''}
${json.info.license && json.info.license.name ? `<li><strong>License:</strong> ${json.info.license.url ? `<a href="${json.info.license.url}">${json.info.license.name}</a>` : json.info.license.name}</li>` : ''}
${contact ? `<li><strong>Contact:</strong> ${contact.join(', ')}</li>` : ''}
</ul>
${json.tags.map(function (section) {
return generateSectionHTML(section, json);
}).join('')}`;
// Sanitize it
var sanitized = DOMPurify.sanitize(html);
// Inject into the UI
markup.value = sanitized;
preview.innerHTML = sanitized;
notification.textContent = 'Static documentation based on your Swagger definition has been generated below.';
};
/**
* Generate HTML
* @param {Event} event The event object
*/
var createDocs = function (event) {
// Convert string to JSON
var json = JSON.parse(event.target.result);
console.log(json);
// Generate the HTML
generateHTML(json);
};
/**
* If there's an error reading the file, handle it
* @todo add proper error handling
*/
var handleError = function (event) {
console.warn(event);
notification.textContent = 'There was a problem processing your Swagger file. Please try again.';
};
/**
* Handle file uploads
*/
var changeHandler = function () {
// Only run if there's a file
if (!swagger.files.length) return;
// Read the file
var reader = new FileReader();
reader.readAsText(swagger.files[0], 'UTF-8');
// After file is read
reader.onload = createDocs;
reader.onerror = handleError;
};
//
// Inits & Event Listener
//
swagger.addEventListener('change', changeHandler);
</script>
</body>
</html>