This repository was archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.js
More file actions
44 lines (38 loc) · 1.33 KB
/
render.js
File metadata and controls
44 lines (38 loc) · 1.33 KB
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
const nunjucks = require('nunjucks');
const fs = require('fs');
const vscode = require('vscode');
const path = require('path');
const jsyaml = require('js-yaml');
module.exports = class DocumentRender{
constructor(context) {
this.context = context;
}
render(filename, text) {
let content = "";
let ext = path.extname(filename);
if (ext == ".json") {
content = JSON.parse(text);
}else if (ext == ".yaml" || ext == ".yml") {
content = jsyaml.safeLoad(text);
// content = JSON.stringify(yaml);
} else {
return "";
}
let swaggerUIPath = this.context.extensionPath + "/swagger-ui";
let destFile = this.context.extensionPath + "/swagger-ui/index.htm";
try {
nunjucks.configure(swaggerUIPath);
let res = nunjucks.render("index.njk", {swaggerUIPath: swaggerUIPath, content: content});
fs.writeFileSync(destFile, res);
// Turn windows file paths into a URI path:
destFile = destFile.replace(/\\/g, "/");
if (destFile[0] !== "/") {
destFile = "/" + destFile;
}
return vscode.Uri.parse('file://' + destFile);
} catch (error) {
console.error(error);
}
return "";
}
}