-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (110 loc) · 2.72 KB
/
script.js
File metadata and controls
126 lines (110 loc) · 2.72 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
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
#!/usr/bin/env node
'use strict';
/*
Example of using raml2html as a script.
Run this as `node script.js`
*/
const raml2html = require('..');
const path = require('path');
const ramlFile = path.join(__dirname, 'helloworld.raml');
/**
* Using the default templates.
*
* raml2html.render() needs a config object with at least a `processRamlObj` property.
* Instead of creating this config object ourselves, we can just ask for raml2html.getDefaultConfig():
*/
const config1 = raml2html.getConfigForTheme('raml2html-default-theme');
raml2html.render(ramlFile, config1).then(
result => {
console.log('1: ', result.length);
},
error => {
console.log('error! ', error);
}
);
/**
* Using your own templates using the default processRamlObj function.
*/
const config2 = raml2html.getConfigForTemplate(
'./custom-template-test/template.nunjucks'
);
raml2html.render(ramlFile, config2).then(
result => {
console.log('2: ', result.trim().length);
},
error => {
console.log('error! ', error);
}
);
/**
* If you want to customize everything, just create the config object yourself from scratch.
*
* The important thing is to have a processRamlObj property: a function that takes a raw RAML object and returns
* a promise with the finished output
*/
const config3 = {
processRamlObj() {
return new Promise(resolve => {
resolve('<h1>\n\n\n<!--This is a test-->Hi!</h1>');
});
},
postProcessHtml: config1.postProcessHtml,
};
raml2html.render(ramlFile, config3).then(
result => {
console.log('3: ', result.length);
},
error => {
console.log('error! ', error);
}
);
/**
* You can also customize the postProcessHtml function.
*/
const config4 = {
processRamlObj() {
return new Promise(resolve => {
resolve('<h1>Hi!</h1>');
});
},
postProcessHtml() {
return new Promise(resolve => {
resolve('ABC');
});
},
};
raml2html.render(ramlFile, config4).then(
result => {
console.log('4: ', result.length);
},
error => {
console.log('error! ', error);
}
);
/**
* Testing if it works with no config at all. It outputs a JSON version of the RAML file.
*/
raml2html.render(ramlFile, {}).then(
result => {
console.log('5: ', Object.keys(result).length);
},
error => {
console.log('error! ', error);
}
);
/**
* If you want to only customize the Nunjucks configuration, just add a setupNunjucks function to the default config.
*/
const config6 = raml2html.getConfigForTheme('raml2html-default-theme');
config6.setupNunjucks = function(env) {
// Do stuff with env here
env.bla = 'bla';
};
raml2html.render(ramlFile, config6).then(
result => {
console.log('6: ', result.length);
},
error => {
console.log('error! ', error);
}
);