forked from hegrec/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger2md.js
90 lines (74 loc) · 2.65 KB
/
swagger2md.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
var fs = require( 'fs' );
var path = require( 'path' );
var process = require( "process" );
var converter = require('widdershins');
var yaml = require('js-yaml');
function deleteExistingMd(endPath){
//console.log('Starting from dir '+endPath+'/');
if (!fs.existsSync(endPath)){
console.log("no dir ",endPath);
return;
}
var files=fs.readdirSync(endPath);
for(var i=0;i<files.length;i++){
var filename=path.join(endPath,files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
fromDir(filename,filter); //recurse
}
else if (filename.indexOf('.html.md')>=0) {
if (filename.indexOf('index')>=0) {
console.log('Found index, continuing...');
}
else {
console.log('-- found: ',filename);
fs.unlink(filename);
console.log('-- deleted: ',filename);
}
}
};
};
function generateMdFromSwagger(startPath, endPath){
//console.log('Starting from dir '+startPath+'/');
if (!fs.existsSync(startPath)){
console.log("no dir ",startPath);
return;
}
var files=fs.readdirSync(startPath);
for(var i=0;i<files.length;i++){
var filename=path.join(startPath,files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
fromDir(filename,filter); //recurse
}
else if (filename.indexOf('.yaml')>=0) {
console.log('-- found: ',filename);
var newFilename = filename.replace(startPath, endPath).replace('.yaml', '.html.md');
console.log(newFilename);
var swagger = {};
var s = fs.readFileSync(filename,'utf8');
swagger = yaml.safeLoad(s);
var generated = converter.convert(swagger,options);
// Warning: hacky bullshit
generated = generated.replace('language_tabs:', 'layout: "apitwocolumn"\nlanguage_tabs:');
// end hacky bullshit
fs.writeFileSync(newFilename, generated, 'utf8')
console.log('-- generated: ', newFilename);
};
};
};
// Set up directory
var startPath = "data";
var endPath = "source/api/v3";
// Set up widdershins converter options
var options = {}; // defaults shown
options.codeSamples = true;
options.yaml = true;
options.language_tabs = [''];
//options.loadedFrom = sourceUrl;
options.user_templates = './user_templates';
options.theme = 'darkula';
// Delete all existing .generated.md files
// Generate new markdown from swagger files
deleteExistingMd(endPath);
generateMdFromSwagger(startPath, endPath);