Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use custom helpers within custom partial #38

Closed
jennifer-sevenval opened this issue Mar 3, 2017 · 6 comments
Closed

cannot use custom helpers within custom partial #38

jennifer-sevenval opened this issue Mar 3, 2017 · 6 comments

Comments

@jennifer-sevenval
Copy link

Somehow I am not able to use my own registered handlebars helpers within my custom partial to render the docs.

I always get the following error:

node_modules/jsdoc2md-stats/lib/jsdoc2md-stats.js:96
        throw err
        ^
Error: Missing helper: "$"

where $ is actually a helper I wrote to enable all common logical operators.

How can I achieve a synergy between the jsdoc2md helper and my own helpers?

@75lb
Copy link
Member

75lb commented Mar 3, 2017

please send me a simple test case (including code) to help me reproduce this, thanks.

@jennifer-sevenval
Copy link
Author

  {{#if ($ xy '==' 'abc')}}
       id: {{id}}
      longname: {{longname}}
      name: {{name}}
  {{/if}}

/**
         * Function to handle generic expressions,
         * can handle arbitrarily many arguments for math, but only one conditional (use subexpressions for nesting)
         *
         * example calls:
         * {{123 '+' 456}}
         * {{#if (7 '-' 3 '===' 4)}}
         * {{false '||' true}}
         * {{#if (2 '+' 3 '>' 4 '||' (7 '/' 3 '<' 6 '&&' (1 '!=' 2)))}}
         * {{> global/svg-icon icon=('graph' + 'line')}}
         *
         * @returns {mixed} returns a result or a boolean, dependant on the call
         */
        $: function () {
          if (arguments.length === 1) {
            return false;
          }
          var options = arguments[arguments.length - 1];
          var equation = Object.keys(arguments).map(x => arguments[x]).slice(0, -1); // ecma 6 transform to array
          // solve simple math
          for (var i = 0; i < equation.length-1 && equation.length >= 3;) {
            if (equation[i] === '+') {
              if (equation[i-1] && equation[i+1]) {
                equation.splice(i-1, 3, (equation[i-1]) + (equation[i+1]));
              } else if (equation[i-1]) {
                equation.splice(i-1, 3, (equation[i-1]));
              } else if (equation[i+1]) {
                equation.splice(i-1, 3, (equation[i+1]));
              } else {
                throw 'Invalid call to function.';
              }
            } else if (equation[i] === '-') {
              equation.splice(i-1, 3, (equation[i-1] || 0) - (equation[i+1] || 0));
            } else if (equation[i] === '*') {
              equation.splice(i-1, 3, (equation[i-1] || 0) * (equation[i+1] || 0));
            } else if (equation[i] === '/') {
              equation.splice(i-1, 3, (equation[i-1] || 0) / (equation[i+1] || 0));
            } else {
              ++i;
            }
          }
          if (equation.length === 3) {
            // compare (remaining) arguments
            if (equation[1] === '===') {
              return equation[0] === equation[2];
            } else if (equation[1] === '==') {
              return equation[0] == equation[2];
            } else if (equation[1] === '!=') {
              return equation[0] != equation[2];
            } else if (equation[1] === '!==') {
              return equation[0] !== equation[2];
            } else if (equation[1] === '<') {
              return equation[0] < equation[2];
            } else if (equation[1] === '<=') {
              return equation[0] <= equation[2];
            } else if (equation[1] === '>') {
              return equation[0] > equation[2];
            } else if (equation[1] === '>=') {
              return equation[0] >= equation[2];
            } else if (equation[1] === '&&') {
              return equation[0] && equation[2];
            } else if (equation[1] === '||') {
              return equation[0] || equation[2];
            } else {
              throw 'Invalid call to function.';
            }
          } else if (equation.length === 2) {
            if (equation[0] === '!') {
              return !equation[1];
            } else if (equation[0] === '-') {
              return -equation[1];
            } else {
              throw 'Invalid call to function.';
            }
          } else if (equation.length === 1) {
            // return value
            return equation[0];
          }
          throw 'Invalid call to function.';
        }

@75lb
Copy link
Member

75lb commented Mar 3, 2017

and how are you calling jsdoc2md? Can you send details of the command you are running.. thanks.

@jennifer-sevenval
Copy link
Author

I use it as a gulp-task:

/**
 * API
 * Generates API files out of JSDOC within .js files
 */
gulp.task('build:api', function () {
  const jsdoc2md = require('jsdoc-to-markdown');
  const files = [];

  return gulp.src('./src/_global/production/scripts/**/*.js')
  .pipe(through(function(file, encoding, done) {
    files.push(file.path);
    done(null, file);
  }))
  .on('end', function() {
    files.forEach(function(file) {
      fs.writeFileSync(
        './src/_global/documentation/partials/_doc-helper/api/' + path.basename(file) + '.hbs',
        jsdoc2md.renderSync({
          files: file,
          partial: './src/_global/documentation/partials/_doc-helper/api/jsdocOverrides/*.hbs',
          lang: 'js',
          'no-gfm': true,
          separators: true,
          moduleIndexFormat: 'dl',
          noCache: true,
          configure: './jsdoc.json',
        })
      );
    });
  });
});

@75lb
Copy link
Member

75lb commented Mar 3, 2017

where are you passing in the custom helper? The custom helper module should be passed into options.helper.

@jennifer-sevenval
Copy link
Author

jennifer-sevenval commented Mar 3, 2017

At the moment I do not passing it into something, as it's registered within my own handlebars config, but nevertheless it's a good advice - I'm going to test it with also passing it into options.helper

// Edit: passing my helpers also to the plugin worked like a charm - thanks a lot! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants