forked from italia/api-oas-checker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ruleset_doc_generator.mjs
84 lines (75 loc) · 2.19 KB
/
ruleset_doc_generator.mjs
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
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'fs';
import path from 'path';
import yaml from 'yaml';
import marked from 'marked';
import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
import { getDocFilename, autoLinkRFC } from './src/utils.mjs';
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);
const { file, title } = yargs(hideBin(process.argv))
.option('file', {
alias: 'f',
demandOption: true,
description: 'The ruleset file used for making doc',
type: 'string',
})
.option('title', {
alias: 't',
demandOption: true,
description: 'The title of the output doc',
type: 'string',
}).argv;
const content = fs.readFileSync(file, 'utf8');
const doc = yaml.parseDocument(content).toJSON();
const rulesMd = Object.entries(doc.rules).reduce(
(rules, [key, value]) => {
// https://github.com/italia/api-oas-checker/issues/122
if (value === false) return rules;
if (value.description === undefined)
throw new Error(`Rule ${key} doesn't have a description. Rule description is a required field`);
const description = autoLinkRFC(value.description);
rules.push(`## ${key}\n\n${description}`);
return rules;
},
[`# ${title}`]
);
const rulesHtml = DOMPurify.sanitize(marked(rulesMd.join('\n\n')), { USE_PROFILES: { html: true } });
const docFilename = getDocFilename(file);
const boostrapCss = fs.readFileSync(
path.resolve(path.dirname(''), 'node_modules/bootstrap-italia/dist/css/bootstrap-italia.min.css'),
'utf-8'
);
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
${boostrapCss}
h2, h3 {
color: var(--primary)
}
h1, h2 {
margin: 30px 0;
}
h3 {
margin: 48px 0 24px 0;
}
</style>
<title>${title}</title>
</head>
<body>
<div class="container">
<div class="row">
<div style="margin: auto" class="col-xs-12 col-sm-10 col-md-8 col-lg-6">
${rulesHtml}
</div>
</div>
</div>
</body>
</html>`;
fs.writeFileSync(docFilename, html);