-
Notifications
You must be signed in to change notification settings - Fork 151
How to create one output file per class
Lloyd Brookes edited this page Oct 19, 2016
·
6 revisions
1. Say you have several documented classes in a file named example.js
:
/**
* Animal.
*/
class Animal {
constructor () {
/**
* Favourite food.
*/
this.food = null
}
}
/**
* Habitat.
*/
class Habitat {
constructor () {
/**
* Contains wood.
*/
this.wood = null
}
}
2. This example script will generate one output file per documented class found in the input (see the jsdoc2md API docs for more details on the getTemplateDataSync
and renderSync
methods).
'use strict'
const jsdoc2md = require('jsdoc-to-markdown')
const fs = require('fs')
const path = require('path')
/* input and output paths */
const inputFile = 'example.js'
const outputDir = __dirname
/* get template data */
const templateData = jsdoc2md.getTemplateDataSync({ files: inputFile })
/* reduce templateData to an array of class names */
const classNames = templateData.reduce((classNames, identifier) => {
if (identifier.kind === 'class') classNames.push(identifier.name)
return classNames
}, [])
/* create a documentation file for each class */
for (const className of classNames) {
const template = `{{#class name="${className}"}}{{>docs}}{{/class}}`
console.log(`rendering ${className}, template: ${template}`)
const output = jsdoc2md.renderSync({ data: templateData, template: template })
fs.writeFileSync(path.resolve(outputDir, `${className}.md`), output)
}
3. Two output files are created, the first Animal.md:
Animal.
Favourite food.
Kind: instance property of Animal
4. The second output file, Habitat.md:
Habitat.
Contains wood.
Kind: instance property of Habitat
- Home
- How jsdoc2md works
- Additional jsdoc tags supported
- Cherry picking which documentation appears in output
- Showcase ...
- Create ...
- How To ...
- How to use with npm run
- How to use with gulp
- How to create one output file per class
- How to document a AMD module
- How to document a CommonJS module (exports)
- How to document a CommonJS module (module.exports)
- How to document an ES2015 module (multiple named exports)
- How to document an ES2015 module (single default export)
- How to document Promises (using custom tags)
- How to document a ToDo list
- How to document ES2017 features
- How to document TypeScript
- The @typicalname tag
- Linking to external resources
- Param list format options
- Listing namepaths
- Troubleshooting