Skip to content

Commit

Permalink
feat(cli): Add templateOptions param to pass additional data to custo…
Browse files Browse the repository at this point in the history
…m template (#792)

* Add templateOptions param to pass additional data to custom template
* Update README for ReDoc cli
  • Loading branch information
bautrukevich authored and RomanHotsiy committed Jan 31, 2019
1 parent 889cbe3 commit 4e8ee03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ Some examples:
- Bundle with main color changed to `orange`: <br> `$ redoc-cli bundle [spec] --options.theme.colors.primary.main=orange`
- Serve with `nativeScrollbars` option set to true: <br> `$ redoc-cli serve [spec] --options.nativeScrollbars`
- Bundle using custom template (check [default template](https://github.com/Rebilly/ReDoc/blob/master/cli/template.hbs) for reference): <br> `$ redoc-cli bundle [spec] -t custom.hbs`
- Bundle using custom template and add custom `templateOptions`: <br> `$ redoc-cli bundle [spec] -t custom.hbs --templateOptions.metaDescription "Page meta description"`

For more details run `redoc-cli --help`.
15 changes: 11 additions & 4 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Options {
output?: string;
title?: string;
templateFileName?: string;
templateOptions?: any;
redocOptions?: any;
}

Expand Down Expand Up @@ -65,6 +66,7 @@ YargsParser.command(
ssr: argv.ssr,
watch: argv.watch,
templateFileName: argv.template,
templateOptions: argv.templateOptions || {},
redocOptions: argv.options || {},
};

Expand Down Expand Up @@ -112,6 +114,7 @@ YargsParser.command(
cdn: argv.cdn,
title: argv.title,
templateFileName: argv.template,
templateOptions: argv.templateOptions || {},
redocOptions: argv.options || {},
};

Expand All @@ -128,6 +131,9 @@ YargsParser.command(
describe: 'Path to handlebars page template, see https://git.io/vh8fP for the example ',
type: 'string',
})
.options('templateOptions', {
describe: 'Additional options that you want pass to template. Use dot notation, e.g. templateOptions.metaDescription',
})
.options('options', {
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
}).argv;
Expand Down Expand Up @@ -207,7 +213,7 @@ async function bundle(pathToSpec, options: Options = {}) {
async function getPageHTML(
spec: any,
pathToSpec: string,
{ ssr, cdn, title, templateFileName, redocOptions = {} }: Options,
{ ssr, cdn, title, templateFileName, templateOptions, redocOptions = {} }: Options,
) {
let html;
let css;
Expand Down Expand Up @@ -241,15 +247,16 @@ async function getPageHTML(
ssr
? 'hydrate(__redoc_state, container);'
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
};
};
</script>`,
redocHead: ssr
? (cdn
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
: `<script>${redocStandaloneSrc}</script>`) + css
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
: `<script>${redocStandaloneSrc}</script>`) + css
: '<script src="redoc.standalone.js"></script>',
title,
templateOptions,
});
}

Expand Down

0 comments on commit 4e8ee03

Please sign in to comment.