Skip to content

Cherry picking which documentation appears in output

Errorbot1122 edited this page Oct 29, 2021 · 3 revisions

1. If you're writing a custom output template (for use with --template) it's possible to pick and choose exactly which class/function/module/etc identifiers appear in the output. To achieve this there are several block helpers you can use, the "selector helpers":

  • {{#globals}} - Iterates over each identifier in global scope
  • {{#modules}} - Iterates over each module
  • {{#classes}} - Iterates over each class
  • {{#functions}} - Iterates over each function
  • {{#identifiers}} - Iterates over all identifiers
  • {{#orphans}} - Iterates over all identifiers with no parent (modules and globals)
  • {{#module}} - Picks a single module
  • {{#class}} - Picks a single class
  • {{#function}} - Picks a single function
  • {{#namespace}} - Picks a single namespace
  • {{#enum}} - Picks a single enum
  • {{#identifier}} - Picks a single identifier

2. For example, create this source code (in a file named 'example.js'):

/**
 * Addition
 */
function add () {}

/**
 * Subtraction
 */
function subtract () {}

/**
 * Multiplication
 */
function multiply () {}

3. If you inspect the template data (using jsdoc2md --json example.js) you will see this:

[
  {
    "id": "add",
    "longname": "add",
    "name": "add",
    "kind": "function",
    "scope": "global",
    "description": "Addition",
    "params": [],
    "meta": {
      "lineno": 4,
      "filename": "0-src.js",
      "path": "/Users/lloydb/Documents/jsdoc2md/testbed/build/jsdoc2md/templating/selector-helpers/functions"
    },
    "order": 0
  },
  {
    "id": "subtract",
    "longname": "subtract",
    "name": "subtract",
    "kind": "function",
    "scope": "global",
    "description": "Subtraction",
    "params": [],
    "meta": {
      "lineno": 9,
      "filename": "0-src.js",
      "path": "/Users/lloydb/Documents/jsdoc2md/testbed/build/jsdoc2md/templating/selector-helpers/functions"
    },
    "order": 1
  },
  {
    "id": "multiply",
    "longname": "multiply",
    "name": "multiply",
    "kind": "function",
    "scope": "global",
    "description": "Multiplication",
    "params": [],
    "meta": {
      "lineno": 14,
      "filename": "0-src.js",
      "path": "/Users/lloydb/Documents/jsdoc2md/testbed/build/jsdoc2md/templating/selector-helpers/functions"
    },
    "order": 2
  }
]

4. You could write a custom template (e.g. 'template.hbs') which uses selector helpers to list the names and descriptions of each function:

{{#functions}}
* {{name}} - {{description}}
{{/functions}}

5. Now, running the command jsdoc2md --template template.hbs --files example.js would output:

* add - Addition
* subtract - Subtraction
* multiply - Multiplication

6. You can filter which identifiers are cherry-picked by adding attributes to the helper, which act as queries. For example, this helper will output docs for the jsdoc-to-markdown module only:

{{#module name="jsdoc-to-markdown"}}
{{>docs}}
{{/module}}
Clone this wiki locally