Typedoc: Include custom Handlebars helpers

Created on 23 Oct 2015  路  8Comments  路  Source: TypeStrong/typedoc

It would be convenient to be able to specify a js file on the command line which contains handlebars helpers. Much like is included in the typedoc/src/td/output/plugins/MarkedPlugin.ts file on lines 90-95.

            Handlebars.registerHelper('markdown', function(arg:any) { return that.parseMarkdown(arg.fn(this), this); });
            Handlebars.registerHelper('compact',  function(arg:any) { return that.getCompact(arg.fn(this)); });
            Handlebars.registerHelper('relativeURL', (url:string) => this.getRelativeUrl(url));
            Handlebars.registerHelper('wbr', (str:string) => this.getWordBreaks(str));
            Handlebars.registerHelper('ifCond', function(v1, operator, v2, options) { return that.getIfCond(v1, operator, v2, options, this) });
            Handlebars.registerHelper('ifSignature', function(obj:any, arg:any) { return obj instanceof models.SignatureReflection ? arg.fn(this) : arg.inverse(this) });

This would allow for further customization of the themes. I would be able to pass in my own helpers.

enhancement

Most helpful comment

I figured this out.

  1. Create a new helpers directory under your desired theme, e.g. default
  2. Each helper can be in a single file or you can have multiple helpers

To export one or more helpers in one file you can do:

default/helpers/ifEquals.js

module.exports = {
  ifEquals: function ( value1, value2, options ) {
    return value1 === value2 ? options.fn(this) : "";
  }
}

All 8 comments

Hi Mike, nice to hear from you. We have already changed this for the next version of TypeDoc. v0.4 will automatically load all helpers stored in the /helpers/ directory of a theme.

Thanks, Sebastian!

I figured this out.

  1. Create a new helpers directory under your desired theme, e.g. default
  2. Each helper can be in a single file or you can have multiple helpers

To export one or more helpers in one file you can do:

default/helpers/ifEquals.js

module.exports = {
  ifEquals: function ( value1, value2, options ) {
    return value1 === value2 ? options.fn(this) : "";
  }
}

Unfortunately, this nifty detail is missing in the documentation on the website http://typedoc.org/guides/themes/#custom-themes, leading to confusion in at least two plugin projects:
thepalebluedot/typedoc-md-theme and kimamula/typedoc-markdown-theme

@sclausen True. If anyone wants to take a crack at updating the docs you can submit a PR to TypeStrong/typedoc-site

See #846 for an alternate way of adding helpers in a plugin.

this.application.renderer.theme.resources.deactivate();
this.application.renderer.theme.resources.helpers.addOrigin('custom-helper', `<path-to-my-custom-helper-function>`));
this.application.renderer.theme.resources.activate();

Resolved by TypeStrong/typedoc-site#23

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0815fox picture 0815fox  路  3Comments

Bibliofile picture Bibliofile  路  3Comments

woppa684 picture woppa684  路  3Comments

mcmath picture mcmath  路  3Comments

cfischer picture cfischer  路  4Comments