Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ $ api2html --help
Options:

-V, --version output the version number
-r, --resolve <source> resolve external dependencies, source should be a url or a path
-o, --out <outputPath> output path for the resulting HTML document
-t, --theme <themeName> theme to use (see https://highlightjs.org/static/demo/ for a list)
-c, --customLogo <logoPath> use custom logo at the respective path
Expand Down Expand Up @@ -81,3 +82,11 @@ This will render the `api.yml` file in the current directory as `myapi.html` fil
```bash
$ api2html -o myapi.html -l go,javascript -t arta myapi.yml
```

#### Resolve external dependencies

If you add refs to external files in your source file, you can enable them by using `-r <source>`. The following command will resolve all your relative imports from the current directory.

```bash
$ api2html -o myapi.html -r ./ myapi.yml
```
40 changes: 23 additions & 17 deletions bin/api2html.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const icons = {
program
.version(pkg.version)
.usage("[options] <sourcePath>")
.option("-r, --resolve <source>", "resolve external dependencies, source should be a url or a path")
.option("-o, --out <outputPath>", "output path for the resulting HTML document")
.option("-t, --theme <themeName>", "theme to use (see https://highlightjs.org/static/demo/ for a list)")
.option("-c, --customLogo <logoPath>", "use custom logo at the respective path")
Expand Down Expand Up @@ -62,13 +63,18 @@ if (program.args.length === 0) {
options.headings = 2;
options.verbose = false;

if (program.resolve) {
options.resolve = true;
options.source = program.resolve;
}

if (program.includes) {
options.includes = program.includes.split(",");
}

if (program.languages) {
const temp = program.languages.split(",");
let tempLanguages = [];
let tempLanguages = [];
temp.forEach((lang) => {
if (Object.getOwnPropertyNames(languageMap).indexOf(lang.toLowerCase()) > -1) {
tempLanguages.push(lang);
Expand All @@ -87,7 +93,7 @@ if (program.args.length === 0) {
});
}
}

// Shin options
let shinOptions = {};
shinOptions.inline = true;
Expand All @@ -104,53 +110,53 @@ if (program.args.length === 0) {

// Read source file
const file = fs.readFileSync(path.resolve(program.args[0]), "utf8");

console.log(chalk.green(icons.ok) + " Read source file!");

try {
// Load source yaml

// Load source yaml
api = yaml.safeLoad(file, { json: true });

// Convert the yaml to markdown for usage with Shin
converter.convert(api, options, (err, markdownString) => {

if (err) {
console.log(chalk.red(icons.fail) + " Error during conversion via Widdershin:");
console.log(err.message);
process.exit(-1);
}

console.log(chalk.green(icons.ok) + " Converted OpenAPI docs to markdown!");

// Render the markdown as HTML and inline all the assets
shins.render(markdownString, shinOptions, (err, html) => {

if (err) {
console.log(chalk.red(icons.fail) + " Error during rendering via Shin:");
console.log(err.message);
process.exit(-1);
}

console.log(chalk.green(icons.ok) + " Rendered HTML form markdown!");

try {

// Write output file
fs.writeFileSync(path.resolve(program.out), html, "utf8");

console.log(chalk.green(icons.ok) + " Wrote output file!");

} catch (err) {
console.log(chalk.red(icons.fail) + " Failed to write output file:");
console.log(err.message);
process.exit(-1);
} finally {
console.log(chalk.green(icons.ok) + " Finished!");
}

});

});

}
Expand Down