/
fix-api.js
48 lines (43 loc) · 1.92 KB
/
fix-api.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
const fs = require('fs');
if (process.argv.length !== 3 || process.argv[1] === 'help') {
console.error(`
usage:
node fix-api.js path/to/file.md
`);
process.exit(1);
}
const file = process.argv[2];
fs.readFile(file, (err, b) => {
if (err) {
throw err;
}
const t = b
.toString()
.replace(/^id: api/img,'')
.replace(/^title:(.*)/im, 'title: REST API\nid: api') // improve title, add docusaurus id
.replace(/^language_tabs:.*\n/im, '') // not supported by docusaurus
.replace(/^toc_footers.*\n/im, '') // not supported by docusaurus
.replace(/^includes.*\n/im, '') // not supported by docusaurus
.replace(/^search.*\n/im, '') // not supported by docusaurus
.replace(/^highlight_theme.*\n/im, '') // not supported by docusaurus
.replace(/^headingLevel.*\n/im, '') // not supported by docusaurus
// .replace(/^<h1.*\n/im, '') // remove first headline (this is the title of the main package usually)
// .replace(/^> Scroll down for example requests and responses.*\n/im, '') // Irrelevant information
// .replace(/^Base Urls:*\n/im, '') // Irrelevant information, let's replace it with something useful instead!
// .replace(/^\* <a href="\/">\/<\/a>\n/im, '') // Irrelevant information
// .replace(/<h2 id="toc([a-zA-Z0-9_\-]+)">([a-zA-Z0-9_\-]+)<\/h2>\n/gim, '## $2')
// .replace(/<h1 id="ory-([a-zA-Z0-9_\-]+)">([a-zA-Z0-9_\-]+)<\/h2>\n/gim, '## $2')
.replace(/\n\s*\n/g, '\n\n', -1)
.replace(/^-(\s.*)\n/gim, '-$1', -1)
.replace(/\n\n---/gi, '\n---\n\n')
// .replace(/\n\s*\n```/gi, '\n```')
// .replace(/^<h3 id="[0-9a-zA-Z0-9\-_.]+-responses">Responses<\/h3>$/gim, '#### Summary',-1)
// .replace(/^> Example responses/gim, '### Responses',-1)
// .replace(/^> Body parameter/gim, '### Request body',-1)
.replace(/^> ([0-9]+) Response$/gim, '###### $1 response', -1);
fs.writeFile(file, t, (err) => {
if (err) {
throw err;
}
});
});