UNPKG

8.14 kBJavaScriptView Raw
1const jsdocApi = require('jsdoc-api')
2const dmd = require('dmd')
3const DmdOptions = require('./lib/dmd-options')
4const JsdocOptions = require('./lib/jsdoc-options')
5
6/**
7 * @module jsdoc-to-markdown
8 * @example
9 * const jsdoc2md = require('jsdoc-to-markdown')
10 */
11
12/**
13 * @alias module:jsdoc-to-markdown
14 * @typicalname jsdoc2md
15*/
16class JsdocToMarkdown {
17 /**
18 * Returns markdown documentation from jsdoc-annoted source code.
19 *
20 * @param [options] {object} - Accepts all {@link module:jsdoc-to-markdown#getJsdocData} options plus the following:
21 * @param [options.data] {object[]} - Raw template data to use. Useful when you already have template data, obtained from `.getTemplateData`. Either `files`, `source` or `data` must be supplied.
22 * @param [options.template] {string} - The template the supplied documentation will be rendered into. Use the default or supply your own template for full control over the output.
23 * @param [options.heading-depth] {number} - The initial heading depth. For example, with a value of `2` the top-level markdown headings look like `"## The heading"`.
24 * @param [options.example-lang] {string} - Specifies the default language used in @example blocks (for [syntax-highlighting](https://help.github.com/articles/github-flavored-markdown/#syntax-highlighting) purposes). In gfm mode, each @example is wrapped in a fenced-code block. Example usage: `--example-lang js`. Use the special value `none` for no specific language. While using this option, you can override the supplied language for any @example by specifying the `@lang` subtag, e.g `@example @lang hbs`. Specifying `@example @lang off` will disable code blocks for that example.
25 * @param [options.plugin] {string|string[]} - Use an installed package containing helper and/or partial overrides.
26 * @param [options.helper] {string|string[]} - handlebars helper files to override or extend the default set.
27 * @param [options.partial] {string|string[]} - handlebars partial files to override or extend the default set.
28 * @param [options.name-format] {string} - Format identifier names as code (i.e. wrap function/property/class etc names in backticks).
29 * @param [options.no-gfm] {boolean} - By default, dmd generates github-flavoured markdown. Not all markdown parsers render gfm correctly. If your generated docs look incorrect on sites other than Github (e.g. npmjs.org) try enabling this option to disable Github-specific syntax.
30 * @param [options.separators] {boolean} - Put `<hr>` breaks between identifiers. Improves readability on bulky docs.
31 * @param [options.module-index-format] {string} - none, grouped, table, dl.
32 * @param [options.global-index-format] {} - none, grouped, table, dl.
33 * @param [options.param-list-format] {} - Two options to render parameter lists: 'list' or 'table' (default). Table format works well in most cases but switch to list if things begin to look crowded / squashed.
34 * @param [options.property-list-format] {} - list, table.
35 * @param [options.member-index-format] {} - grouped, list
36 * @return {Promise}
37 * @fulfil {string} - the rendered docs
38 * @category async
39 * @example
40 * Pass in filepaths (`**` glob matching supported) of javascript source files:
41 * ```js
42 * > jsdoc2md.render({ files: 'lib/*.js' }).then(console.log)
43 * ```
44 */
45 render (options) {
46 options = options || {}
47 const dmdOptions = new DmdOptions(options)
48 if (options.data) {
49 return dmd.async(options.data, dmdOptions)
50 } else {
51 return this.getTemplateData(options)
52 .then(templateData => dmd.async(templateData, dmdOptions))
53 }
54 }
55
56 /**
57 * Sync version of {@link module:jsdoc-to-markdown#render}.
58 *
59 * @param [options] {object} - Identical options to {@link module:jsdoc-to-markdown#render}.
60 * @return {string}
61 * @engine nodejs >= 0.12
62 * @category sync
63 * @example
64 * const docs = jsdoc2md.renderSync({ files: 'lib/*.js' })
65 */
66 renderSync (options) {
67 options = options || {}
68 const dmdOptions = new DmdOptions(options)
69 if (options.data) {
70 return dmd(options.data, dmdOptions)
71 } else {
72 return dmd(this.getTemplateDataSync(options), dmdOptions)
73 }
74 }
75
76 /**
77 * Returns the template data (jsdoc-parse output) which is fed into the output template (dmd).
78 *
79 * @param [options] {object} - Identical options to {@link module:jsdoc-to-markdown#getJsdocData}.
80 * @return {Promise}
81 * @fulfil {object[]} - the json data
82 * @category async
83 */
84 getTemplateData (options) {
85 options = options || {}
86 const jsdocParse = require('jsdoc-parse')
87 return this.getJsdocData(options)
88 .then(jsdocParse)
89 }
90
91 /**
92 * Sync version of {@link module:jsdoc-to-markdown#getTemplateData}.
93 *
94 * @param [options] {object} - Identical options to {@link module:jsdoc-to-markdown#getJsdocData}.
95 * @return {object[]}
96 * @category sync
97 */
98 getTemplateDataSync (options) {
99 options = options || {}
100 const jsdocParse = require('jsdoc-parse')
101 const jsdocData = this.getJsdocDataSync(options)
102 return jsdocParse(jsdocData, options)
103 }
104
105 /**
106 * Returns raw data direct from the underlying [jsdoc3](https://github.com/jsdoc3/jsdoc).
107 *
108 * @param [options] {object} - the options
109 * @param [options.no-cache] {boolean} - By default results are cached to speed up repeat invocations. Set to true to disable this.
110 * @param [options.files] {string|string[]} - One or more filenames to process. Accepts globs (e.g. `*.js`). Either `files`, `source` or `data` must be supplied.
111 * @param [options.source] {string} - A string containing source code to process. Either `files`, `source` or `data` must be supplied.
112 * @param [options.configure] {string} - The path to the [jsdoc configuration file](http://usejsdoc.org/about-configuring-jsdoc.html). Default: path/to/jsdoc/conf.json.
113 * @return {Promise}
114 * @fulfil {object[]}
115 * @category async
116 */
117 getJsdocData (options) {
118 const jsdocOptions = new JsdocOptions(options)
119 return jsdocApi.explain(jsdocOptions)
120 }
121
122 /**
123 * Sync version of {@link module:jsdoc-to-markdown#getJsdocData}.
124 *
125 * @param [options] {object} - Identical options to {@link module:jsdoc-to-markdown#getJsdocData}.
126 * @return {object[]}
127 * @category sync
128 */
129 getJsdocDataSync (options) {
130 const jsdocOptions = new JsdocOptions(options)
131 return jsdocApi.explainSync(jsdocOptions)
132 }
133
134 /**
135 * By default, the output of each invocation of the main generation methods (`render`, `getTemplateData` etc) is stored in the cache (your system's [temporary directory](https://nodejs.org/dist/latest-v6.x/docs/api/os.html#os_os_tmpdir)). Future jsdoc2md invocations with the same input options and source code will return the output immediately from cache, making the tool much faster/cheaper. If the input options or source code changes, fresh output will be generated. This method clears the cache, which you should never need to do unless the cache is failing for some reason. On Mac OSX, the system tmpdir clears itself every few days meaning your jsdoc2md cache will also be routinely cleared.
136 * @returns {Promise}
137 * @category async
138 */
139 clear () {
140 return jsdocApi.cache.clear().then(() => dmd.cache.clear())
141 }
142
143 /**
144 * Returns all [jsdoc namepaths](http://usejsdoc.org/about-namepaths.html) found in the supplied source code.
145 * @param {object} - options to pass to {@link module:jsdoc-to-markdown#getTemplateData}
146 * @returns {object}
147 * @category async
148 */
149 getNamepaths (options) {
150 return this.getTemplateData(options)
151 .then(data => {
152 const namepaths = {}
153 const kinds = [
154 'module', 'class', 'constructor', 'mixin', 'member',
155 'namespace', 'constant', 'function', 'event', 'typedef', 'external'
156 ]
157 kinds.forEach(kind => {
158 namepaths[kind] = data
159 .filter(identifier => {
160 return identifier.kind === kind
161 })
162 .map(identifier => identifier.longname)
163 })
164 return namepaths
165 })
166 }
167}
168
169module.exports = new JsdocToMarkdown()