function getConfigForTemplate(mainTemplate) {
const templatesPath = path.dirname(fs.realpathSync(mainTemplate));
const templateFile = path.basename(fs.realpathSync(mainTemplate));
return {
processRamlObj(ramlObj, config) {
const renderer = new marked.Renderer();
renderer.table = function(thead, tbody) {
// Render Bootstrap style tables
return `<table class="table"><thead>${thead}</thead><tbody>${tbody}</tbody></table>`;
};
// Setup the Nunjucks environment with the markdown parser
const env = nunjucks.configure(templatesPath, { autoescape: false });
if (config.setupNunjucks) {
config.setupNunjucks(env);
}
markdown.register(env, md => marked(md, { renderer }));
ramlObj.isStandardType = function(type) {
if (typeof type === 'object') {
return false;
}
return type && type.indexOf('{') === -1 && type.indexOf('<') === -1;
};
// Render the main template using the raml object and fix the double quotes
let html = env.render(templateFile, ramlObj);
html = html.replace(/"/g, '"');
// Return the promise with the html
return new Promise(resolve => {
resolve(html);
});
},
postProcessHtml(html) {
// Minimize the generated html and return the promise with the result
const minimize = new Minimize({ quotes: true });
return new Promise((resolve, reject) => {
minimize.parse(html, (error, result) => {
if (error) {
reject(new Error(error));
} else {
resolve(result);
}
});
});
},
};
}...
### As a library
#### Using the default theme, different themes, or your own Nunjucks templates
```javascript
const raml2html = require('raml2html');
const configWithDefaultTheme = raml2html.getConfigForTheme();
const configForDifferentTheme = raml2html.getConfigForTheme('raml2html-markdown-theme');
const configWithCustomTemplate = raml2html.getConfigForTemplate('~/path/to/my-custom
-template.nunjucks');
// source can either be a filename, url, or parsed RAML object
raml2html.render(source, configWithDefaultTheme).then(function(result) {
// Save the result to a file or do something else with the result
}, function(error) {
// Output error
});
...function getConfigForTheme(theme, programArguments) {
if (!theme) {
theme = 'raml2html-default-theme';
}
try {
// See if the theme supplies its own config object (or function that creates this object), and return it
const config = require(theme);
// If it's a function then call it with the program arguments
if (typeof config === 'function') {
return config(programArguments);
}
// Otherwise we assume it's a config object (default behavior)
return config;
} catch (err) {
// Nope, forward to getConfigForTemplate
const templatesPath = path.dirname(
require.resolve(`${theme}/package.json`)
);
return getConfigForTemplate(path.join(templatesPath, 'index.nunjucks'));
}
}...
```
### As a library
#### Using the default theme, different themes, or your own Nunjucks templates
```javascript
const raml2html = require('raml2html');
const configWithDefaultTheme = raml2html.getConfigForTheme();
const configForDifferentTheme = raml2html.getConfigForTheme('raml2html-markdown-theme');
const configWithCustomTemplate = raml2html.getConfigForTemplate('~/path/to/my-custom-template.nunjucks');
// source can either be a filename, url, or parsed RAML object
raml2html.render(source, configWithDefaultTheme).then(function(result) {
// Save the result to a file or do something else with the result
}, function(error) {
...function render(source, config) {
config = config || {};
config.raml2HtmlVersion = pjson.version;
return raml2obj.parse(source).then(ramlObj => {
ramlObj.config = config;
if (config.processRamlObj) {
return config.processRamlObj(ramlObj, config).then(html => {
if (config.postProcessHtml) {
return config.postProcessHtml(html);
}
return html;
});
}
return ramlObj;
});
}...
if (typeof type === 'object') {
return false;
}
return type && type.indexOf('{') === -1 && type.indexOf('<') === -1;
};
// Render the main template using the raml object and fix the double quotes
let html = env.render(templateFile, ramlObj);
html = html.replace(/"/g, '"');
// Return the promise with the html
return new Promise(resolve => {
resolve(html);
});
},
...