-
Notifications
You must be signed in to change notification settings - Fork 152
How to use with gulp
- Install jsdoc2md in your project.
npm install --save-dev jsdoc-to-markdown
# substitute npm install --save-dev with yarn add -D when using yarn
- Add a task to your
gulpfile.js
- consult the API docs for instructions how to use
jsdoc2md.render
const jsdoc2md = require('jsdoc-to-markdown')
gulp.task('docs', (done) => {
jsdoc2md.render({ files: 'lib/*.js' }).then(output => fs.writeFileSync('api.md', output));
return done();
})
- Or, the synchronous equivalent:
const fs = require('fs')
const jsdoc2md = require('jsdoc-to-markdown')
gulp.task('docs', (done) => {
const output = jsdoc2md.renderSync({ files: 'lib/*.js' });
fs.writeFileSync('api.md', output);
return done();
})
jsdoc2md.render()
and jsdoc2md.renderSync()
take an options object as specified in the API docs. Through the options you can specify a template file, the example language and other properties. Example options:
const jsdocOptions = {
files: './lib/*.js', // specify where your files are
template: fs.readFileSync('./docs/template.hbs', 'utf8'), // read a template file
'example-lang': 'nginx', // specify the "@example" code block language
noCache: true, // Bypass caching
}
Using jsdoc-to-markdown with TypeScript is very similar to How to document TypeScript. It is currently (11th of January 2019) not possible to use the API to document TypeScript (jsdoc-babel has issues properly parsing TypeScript which ends up throwing JSDOC_ERROR: There are no input files to process
) directly so we need to apply some extra magic:
-
The example below primarily uses ES5
Import
statement but the equivalentrequire()
statements are also included. If you want to use theimport
variants please rename yourgulpfile.js
togulpfile.babel.js
and to yourpackage.json
add:"babel": {"presets": ["@babel/preset-env"]}
. -
To mirror a similar setup you need these packages:
npm install --save-dev typescript jsdoc-babel @babel/cli @babel/core @babel/preset-env @babel/preset-typescript jsdoc-babel jsdoc-to-markdown @babel/plugin-proposal-class-properties @babel/plugin-proposal-object-rest-spread common-tags
# substitute "npm install --save-dev" with "yarn add -D" when using yarn
- Example:
import gulp from 'gulp'; // const gulp = require('gulp');
import { exec } from 'child_process'; // const { exec } = require('child_process');
import { oneLine } from 'common-tags'; // const { oneLine } = require('common-tags');
gulp.task('docs', () => {
const opts = {
template: './docs/template.hbs',
exampleLang: 'ts',
inputFiles: './src/*.ts',
configFile: './config/jsdoc2md.json',
outFile: './docs/index.md',
};
const docsCommand = oneLine`node ./node_modules/jsdoc-to-markdown/bin/cli.js
--files ${opts.inputFiles}
--template ${opts.template}
--example-lang ${opts.exampleLang}
--configure ${opts.configFile}
> ${opts.outFile}`;
return exec(docsCommand);
});
What does this task do?
- We're importing
exec
from NodeJS.exec
spawns a child process with whatever input we feed it, in this case we execute./node_modules/jsdoc-to-markdown/bin/cli.js
withnode
-
We're importing
oneLinefrom
common-tags` to make the command more human readable - To use this template you can copy it and modify the
opts
to your project- Create a
jsdoc2md.json
file as described in How to document TypeScript - set
inputFiles
to the path where your TypeScript files are - set
template
to your*.hbs
jsdoc2md template, if you have none remove it from thedocsCommand
- set
exampleLang
to the language you want your@example
tags to be shown as - set
configFile
to the path to yourjsdoc2md.json
configuration file
- Create a
- 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