forked from OAI/OpenAPI-Specification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd2html.js
418 lines (375 loc) · 18.8 KB
/
md2html.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* ReSpec supports markdown formatting, but this shows up on the page before being rendered
Hence we render the markdown to HTML ourselves, this gives us
complete control over formatting and syntax highlighting */
'use strict';
/**
@author Mike Ralphson <[email protected]>
**/
const fs = require('fs');
const path = require('path');
const url = require('url');
const hljs = require('highlight.js');
const cheerio = require('cheerio');
let argv = require('yargs')
.boolean('respec')
.alias('r','respec')
.describe('respec','Output in respec format')
.default('respec',true)
.string('maintainers')
.alias('m','maintainers')
.describe('maintainers','path to MAINTAINERS.md')
.demandCommand(1)
.argv;
const abstract = 'What is the OpenAPI Specification?';
let maintainers = [];
let emeritus = [];
const md = require('markdown-it')({
html: true,
linkify: true,
typographer: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) { // && !argv.respec) {
try {
return '<pre class="nohighlight"><code>' +
hljs.highlight(str, { language: lang }).value +
'</code></pre>';
} catch (__) { }
}
return '<pre class="highlight '+lang+'"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
}
});
function preface(title,options) {
const respec = {
specStatus: "base",
latestVersion: "https://spec.openapis.org/oas/latest.html",
editors: maintainers,
formerEditors: emeritus,
publishDate: options.publishDate,
subtitle: 'Version '+options.subtitle,
edDraftURI: "https://github.com/OAI/OpenAPI-Specification/",
shortName: "OAS",
historyURI: null, // prevent ReSpec from fetching a W3C history based on the shortName
lint: false,
logos:[{
src: "https://raw.githubusercontent.com/OAI/OpenAPI-Style-Guide/master/graphics/bitmap/OpenAPI_Logo_Pantone.png",
alt: "OpenAPI Initiative",
height: 48,
url: "https://openapis.org/"}],
otherLinks: [
{
key: "Participate",
data: [
{
value: "GitHub OAI/OpenAPI-Specification",
href: "https://github.com/OAI/OpenAPI-Specification/",
},
{
value: "File a bug",
href: "https://github.com/OAI/OpenAPI-Specification/issues",
},
{
value: "Commit history",
href: `https://github.com/OAI/OpenAPI-Specification/commits/main/versions/${options.subtitle}.md`,
},
{
value: "Pull requests",
href: "https://github.com/OAI/OpenAPI-Specification/pulls",
},
],
},
],
localBiblio: {
"OpenAPI-Learn": {
title: "OpenAPI - Getting started, and the specification explained",
href: "https://learn.openapis.org/",
publisher: "OpenAPI Initiative"
},
"OpenAPI-Registry": {
title: "OpenAPI Initiative Registry",
href: "https://spec.openapis.org/registry/index.html",
publisher: "OpenAPI Initiative"
},
//TODO: remove localBiblio once Specref PRs https://github.com/tobie/specref/pulls/ralfhandl are merged
"JSON-Schema-Validation-04": {
authors: [ "Kris Zyp", "Francis Galiegue", "Gary Court" ],
href: "https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00",
publisher: "Internet Engineering Task Force (IETF)",
status: "Internet-Draft",
title: "JSON Schema: interactive and non interactive validation. Draft 4",
date: "1 February 2013"
},
"JSON-Schema-05": {
authors: [ "Austin Wright" ],
href: "https://datatracker.ietf.org/doc/html/draft-wright-json-schema-00",
publisher: "Internet Engineering Task Force (IETF)",
status: "Internet-Draft",
title: "JSON Schema: A Media Type for Describing JSON Documents. Draft 5",
date: "13 October 2016"
},
"JSON-Schema-Validation-05": {
authors: [ "Austin Wright", "G. Luff" ],
href: "https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00",
publisher: "Internet Engineering Task Force (IETF)",
status: "Internet-Draft",
title: "JSON Schema Validation: A Vocabulary for Structural Validation of JSON. Draft 5",
date: "13 October 2016"
},
"JSON-Schema-Validation-2020-12": {
authors: [ "Austin Wright", "Henry Andrews", "Ben Hutton" ],
href: "https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00",
publisher: "Internet Engineering Task Force (IETF)",
status: "Internet-Draft",
title: "JSON Schema Validation: A Vocabulary for Structural Validation of JSON. Draft 2020-12",
date: "8 December 2020"
},
"SPDX": {
href: "https://spdx.org/licenses/",
title: "SPDX License List",
publisher: "Linux Foundation"
}
}
};
let preface = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>${md.utils.escapeHtml(title)}</title>`;
// SEO
preface += '<meta name="description" content="The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.">';
preface += '<link rel="canonical" href="https://spec.openapis.org/oas/latest.html" />';
if (options.respec) {
preface += '<script src="../js/respec-w3c.js" class="remove"></script>';
preface += `<script class="remove">var respecConfig = ${JSON.stringify(respec)};</script>`;
try {
preface += fs.readFileSync('./analytics/google.html','utf8');
}
catch (ex) {}
preface += '</head><body>';
preface += '<style>';
preface += '#respec-ui { visibility: hidden; }';
preface += 'h1,h2,h3 { color: #629b34; }';
preface += '.dt-published { color: #629b34; } .dt-published::before { content: "Published "; }';
preface += 'a[href] { color: #45512c; }'; // third OAI colour is #8ad000
preface += 'body:not(.toc-inline) #toc h2 { color: #45512c; }';
preface += 'table { display: block; width: 100%; overflow: auto; }';
preface += 'table th { font-weight: 600; }';
preface += 'table th, table td { padding: 6px 13px; border: 1px solid #dfe2e5; }';
preface += 'table tr { background-color: #fff; border-top: 1px solid #c6cbd1; }';
preface += 'table tr:nth-child(2n) { background-color: #f6f8fa; }';
preface += 'pre { background-color: #f6f8fa !important; }';
preface += 'code { color: #c83500 } th code { color: inherit }';
preface += 'a.bibref { text-decoration: underline;}';
preface += fs.readFileSync(path.resolve(__dirname,'gist.css'),'utf8').split('\n').join(' ');
preface += '</style>';
preface += `<h1 id="title">${title.split('|')[0]}</h1>`;
preface += `<p class="copyright">Copyright © ${options.publishDate.getFullYear()} the Linux Foundation</p>`;
preface += `<section class="notoc" id="abstract"><h2>${abstract}</h2>`;
preface += 'The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs, which allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic. When properly defined via OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interface descriptions have done for lower-level programming, the OpenAPI Specification removes guesswork in calling a service.';
preface += '</section>';
preface += '<section class="override" id="sotd" data-max-toc="0">';
preface += '<h2>Status of This Document</h2>';
preface += 'The source-of-truth for the specification is the GitHub markdown file referenced above.';
preface += '</section>';
}
else {
preface += '</head><body>';
}
return preface;
}
function doMaintainers() {
let m = fs.readFileSync(argv.maintainers,'utf8');
let h = md.render(m);
let $ = cheerio.load(h);
let u = $('ul').first();
$(u).children('li').each(function(e){
let t = $(this).text().split('@')[0];
maintainers.push({name:t});
});
if ($("ul").length < 2) return;
u = $("ul").last();
$(u).children('li').each(function(e){
let t = $(this).text().split('@')[0];
emeritus.push({name:t});
});
}
function getPublishDate(m) {
let result = new Date();
let h = md.render(m);
let $ = cheerio.load(h);
$('table').each(function(i,table){
const h = $(table).find('th');
const headers = [];
$(h).each(function(i,header){
headers.push($(header).text());
});
if (headers.length >= 2 && headers[0] === 'Version' && headers[1] === 'Date') {
let c = $(table).find('tr').find('td');
let v = $(c[0]).text();
let d = $(c[1]).text();
argv.subtitle = v;
if (d !== 'TBA') result = new Date(d);
}
});
return result;
}
if (argv.maintainers) {
doMaintainers();
}
let s = fs.readFileSync(argv._[0],'utf8');
if (argv.respec) {
argv.publishDate = getPublishDate(s);
}
let lines = s.split(/\r?\n/);
let prevHeading = 0;
let lastIndent = 0;
let inTOC = false;
let inDefs = false;
let inCodeBlock = false;
let bsFix = true;
let indents = [0];
// process the markdown
for (let l in lines) {
let line = lines[l];
if (line.startsWith('## Table of Contents')) inTOC = true;
if (line.startsWith('<!-- /TOC')) inTOC = false;
if (inTOC) line = '';
if (line.startsWith('## Definitions')) {
inDefs = true;
bsFix = false;
}
else if (line.startsWith('## ')) inDefs = false;
if (line.startsWith('```')) {
inCodeBlock = !inCodeBlock;
line += '\n'; // fixes formatting of first line of syntax-highlighted blocks
}
if (!inCodeBlock && line.startsWith('#')) {
let indent = 0;
while (line[indent] === '#') indent++;
let originalIndent = indent;
let prevIndent = indents[indents.length-1]; // peek
let delta = indent-prevIndent;
if (!argv.respec) {
if (delta===0) indent = lastIndent
else if (delta<0) indent = lastIndent-1
else if (delta>0) indent = lastIndent+1;
}
if (indent < 0) {
indent = 1;
}
if (argv.respec && (indent > 1)) {
indent--;
}
let newIndent = indent;
if (!argv.respec && (indent <= 2) && bsFix) {
newIndent++;
}
let title = line.split('# ')[1];
if (inDefs) title = '<dfn>'+title+'</dfn>';
line = ('#'.repeat(newIndent)+' '+title);
if (delta>0) indents.push(originalIndent);
if (delta<0) {
let d = Math.abs(delta);
while (d>0) {
indents.pop();
d--;
}
}
lastIndent = indent;
}
if (line.indexOf('<a name="')>=0) {
if (line.indexOf('<a name="parameterAllowEmptyValue"/>')>=0)
// fix syntax error in 2.0.md
line = line.replace('<a name="parameterAllowEmptyValue"/>','<span id="parameterAllowEmptyValue"></span>');
else {
line = line.replace('<a name=','<span id=');
line = line.replace('</a>','</span>');
}
}
line = line.split('\\|').join('|'); // was ¦
if (!inCodeBlock) {
// minor fixups to get RFC links to work properly
line = line.replace('RFC [','[RFC');
line = line.replace('[Authorization header as defined in ','Authorization header as defined in [');
line = line.replace('[JSON Pointer]','JSON Pointer [RFC6901]'); // only in 2.0.md
line = line.replace('[media type range](https://tools.ietf.org/html/rfc7231#appendix-D) ','media type range, see [RFC7231](https://tools.ietf.org/html/rfc7231#appendix-D), ');
line = line.replace(/\[RFC ?([0-9]{1,5})\]\(/g,'[[RFC$1]](');
// harmonize RFC URLs
//TODO: harmonize to https://www.rfc-editor.org/rfc/rfc*
line = line.replaceAll('](http://','](https://');
line = line.replace('https://www.ietf.org/rfc/rfc2119.txt','https://tools.ietf.org/html/rfc2119'); // only in 2.0.md
line = line.replace(/https:\/\/www.rfc-editor.org\/rfc\/rfc([0-9]{1,5})(\.html)?/g,'https://tools.ietf.org/html/rfc$1');
line = line.replaceAll('https://datatracker.ietf.org/doc/html/','https://tools.ietf.org/html/');
// handle url fragments in RFC links and construct section links as well as RFC links
line = line.replace(/\]\]\(https:\/\/tools.ietf.org\/html\/rfc([0-9]{1,5})\/?(\#[^)]*)?\)/g, function(match, rfcNumber, fragment) {
if (fragment) {
// Extract section title from the fragment
let sectionTitle = fragment.replace('#', '').replace(/-/g, ' ');
sectionTitle = sectionTitle.charAt(0).toUpperCase() + sectionTitle.slice(1); // Capitalize the first letter
//TODO: section links to https://www.rfc-editor.org/rfc/rfc* for newer RFCs (>= 8700)
return `]] [${sectionTitle}](https://datatracker.ietf.org/doc/html/rfc${rfcNumber}${fragment})`;
} else {
return ']]';
}
});
// non-RFC references
line = line.replace('[ABNF](https://tools.ietf.org/html/rfc5234)','[[ABNF]]');
line = line.replace('[CommonMark 0.27](https://spec.commonmark.org/0.27/)','[[CommonMark-0.27]]');
line = line.replace('[CommonMark syntax](https://spec.commonmark.org/)','[[CommonMark]] syntax');
line = line.replace('CommonMark markdown formatting','[[CommonMark]] markdown formatting');
line = line.replace('consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4)','consult [[HTML401]] [Section 17.13.4](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4)');
line = line.replace('[IANA Status Code Registry](https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml)','[[IANA-HTTP-STATUS-CODES|IANA Status Code Registry]]');
line = line.replace('[IANA Authentication Scheme registry](https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml)','[[IANA-HTTP-AUTHSCHEMES]]');
line = line.replace('[JSON Schema Specification Draft 4](https://json-schema.org/)','[[JSON-Schema-04|JSON Schema Specification Draft 4]]');
line = line.replace('[JSON Schema Core](https://tools.ietf.org/html/draft-zyp-json-schema-04)','[[JSON-Schema-04|JSON Schema Core]]');
line = line.replace('[JSON Schema Validation](https://tools.ietf.org/html/draft-fge-json-schema-validation-00)','[[JSON-Schema-Validation-04|JSON Schema Validation]]');
line = line.replace('[JSON Schema Specification Wright Draft 00](https://json-schema.org/)','[[JSON-Schema-05|JSON Schema Specification Wright Draft 00]]');
line = line.replace('[JSON Schema Core](https://tools.ietf.org/html/draft-wright-json-schema-00)','[[JSON-Schema-05|JSON Schema Core]]');
line = line.replace('[JSON Schema Validation](https://tools.ietf.org/html/draft-wright-json-schema-validation-00)','[[JSON-Schema-Validation-05|JSON Schema Validation]]');
line = line.replace('[JSON Schema Specification Draft 2020-12](https://tools.ietf.org/html/draft-bhutton-json-schema-00)','[[JSON-Schema-2020-12|JSON Schema Specification Draft 2020-12]]');
line = line.replace('[JSON Schema Core](https://tools.ietf.org/html/draft-bhutton-json-schema-00)','[[JSON-Schema-2020-12|JSON Schema Core]]');
line = line.replace('[JSON Schema Validation](https://tools.ietf.org/html/draft-bhutton-json-schema-validation-00)','[[JSON-Schema-Validation-2020-12|JSON Schema Validation]]');
line = line.replace('[SPDX](https://spdx.org/licenses/)','[[SPDX]]');
line = line.replace('[XML namespaces](https://www.w3.org/TR/xml-names11/)','[[xml-names11|XML namespaces]]');
line = line.replace('JSON standards. YAML,','[[RFC7159|JSON]] standards. [[YAML|YAML]],'); // 2.0.md only
line = line.replace('JSON or YAML format.','[[RFC7159|JSON]] or [[YAML|YAML]] format.');
line = line.replace(/YAML version \[1\.2\]\(https:\/\/(www\.)?yaml\.org\/spec\/1\.2\/spec\.html\)/,'[[YAML|YAML version 1.2]]');
}
if (!inCodeBlock && line.indexOf('](../') >= 0) {
const regExp = /\((\.\.[^)]+)\)/g;
line = line.replace(regExp,function(match,group1){
console.warn('Fixing relative link',group1,line);
return '('+url.resolve('https://github.com/OAI/OpenAPI-Specification/tree/master/versions/foo',group1)+')';
});
}
if (!inCodeBlock && argv.respec && line.startsWith('#')) {
let heading = 0;
while (line[heading] === '#') heading++;
let delta = heading-prevHeading;
if (delta>1) console.warn(delta,line);
if (delta>0) delta = 1;
let prefix = '';
let newSection = '<section>';
if (line.includes('## Version ')) {
// our conformance section is headlined with 'Version x.y.z'
newSection = '<section class="override" id="conformance">';
}
if (line.includes('Appendix')) {
newSection = '<section class="appendix">';
}
// heading level delta is either 0 or is +1/-1, or we're in respec mode
/* respec insists on <section>...</section> breaks around headings */
if (delta === 0) {
prefix = '</section>'+newSection;
}
else if (delta > 0) {
prefix = newSection.repeat(delta);
}
else {
prefix = '</section>'+('</section>').repeat(Math.abs(delta))+newSection;
}
prevHeading = heading;
line = prefix+md.render(line);
}
lines[l] = line;
}
s = preface(`OpenAPI Specification v${argv.subtitle} | Introduction, Definitions, & More`,argv)+'\n\n'+lines.join('\n');
let out = md.render(s);
out = out.replace(/\[([RGB])\]/g,'[$1]');
out = out.replace('[[IANA-HTTP-AUTHSCHEMES]]','[[IANA-HTTP-AUTHSCHEMES|IANA Authentication Scheme registry]]');
console.log(out);