I’ve written a JSDoc-based document generator that supports powerful configurations to build your Markdown documents the same way WebPack builds WebApps
Because this tool is a tool I prepared to develop a development pipeline, which was designed for engineering purposes at the beginning of the design, the config file supports the introduction of preset, which makes it easy to maintain uniform configuration in the team
Resources:
- There are problems with markdow hash redirect support. If you want to jump through hyperlinks, you can go to the warehouse page to view the documents
- NPM address
- Warehouse group address, you can find some simple sample applications in this group project, easy to understand
- Description of configuration file type definitions
Below is its own document generated by the document generator, which should be easy for those familiar with jSDoc-to-Markdown to understand
Unfortunately, MY narrative ability is not strong, can only directly dump the document ðŸ˜
@agds/cli-plugin-doc
Version: 1.0.14
General comments to Markdown document generator, which aims to support all types of files
Quick start
The installation
npm i -D @agds/cli-plugin-doc
Copy the code
Command line use documentation
Usage: AGDS-doc [options] AGDS system doc Document generator options: <files... > jsdoc entry file glob format path description (need to be enclosed in quotes to avoid parsing failure), relative to CWD directory -o,--output <output> doc document render export file name path, Relative to the CWD directory -c,--config <config> Configuration file path. Relative to the CWD directory, only the JS file type is supported. (default: "Agds.doc.config. js") -t,--template <template> EjS render template relative to CWD path or absolute path -- CD,--codes-dir <codesDir> glob format path, The path to the corresponding folder of a specific example --cf,--codes-files <codesFiles... > glob format path, relative to codesDir code demo folder file path description --no-default Disallow default configuration, default configuration is relatively common, most cases do not need to disable, -v,--version Check the version number -h, --help Check the help information Note: Each path containing wildcards must be enclosed in quotation marks, otherwise it will be parsed in advance, resulting in unexpected errors. https://gitee.com/agile-development-system/cli-plugin-doc @ agds/[email protected] /Users/jinyang/code/ads/cli-plugin-doc/node_modules/@agds/cli-plugin-docCopy the code
The configuration file
The default is agds.doc.config.js in the current directory. The default configuration is automatically merged
You can specify the configuration file name using either the -c –config
command line argument or the Options. config of the Node API
You can use the –no-default command line argument or the Options. default=false of the Node API to disable the default configuration. The default configuration is relatively common and you do not need to disable it in most cases
The export type of the configuration file is 👉RenderOptions, which supports all RenderOptions. For details about helpers provided by the default template, see 👉 and 👉
Code demo
const GenDoc = require('@agds/cli-plugin-doc');
/** * render render@param {object} [options = {}] options *@param {boolean} [options.needDirError] Whether file path errors need to be triggered *@param {boolean} [options.noFiles] Whether to remove the files option *@param {boolean} [options.noDefault] Whether to cancel the default configuration *@param {boolean} [options.noCodes] Whether to remove cods-related configuration *@returns {import('.. /.. /src/index').RenderOptions}* /
module.exports = async ({ needDirError, noFiles, noDefault, noCodes } = {}) => {
const [template, defaultConfig, cliUsages] = (await Promise.all([
GenDoc.getFilesCode({ dir: './src/template'.files: [The '*'] }),
GenDoc.getFilesCode({ dir: './src/utils'.files: ['config.js'] }),
GenDoc.getCliUsages(),
]));
return {
files: noFiles ? null : ['./src/**/*.js'],... (noCodes ? {}, {codesDir: needDirError ? './aaa' : './exa'.codesFiles: [The '*'],}).config: './agds.doc.conf.js'.default: !noDefault,
jsdocEngineOptions: noDefault && {
plugins: [
require.resolve('jsdoc-tsimport-plugin')]},helpers: {
template,
defaultConfig,
cliUsages,
},
};
};
Copy the code
const { expect, test } = require('@jest/globals');
const GenDoc = require('@agds/cli-plugin-doc');
const config = require('.. /__mock__/index');
const path = require('path');
test('GenDoc render'.async() = > {const res = await GenDoc.render(await config());
expect(typeof res === 'string').toBe(true);
});
test('GenDoc render output & use agds.doc.config.js'.async() = > {const res = await GenDoc.render({
output: path.resolve(__dirname, '.. /.. /.temp/README.md')}); expect(typeof res === 'undefined').toBe(true);
});
test('GenDoc render no files'.async() = > {const res = await GenDoc.render(await config({ noFiles: true }));
expect(typeof res === 'string').toBe(true);
});
test('GenDoc render no codes'.async() = > {const res = await GenDoc.render(await config({ noCodes: true }));
expect(typeof res === 'string').toBe(true);
});
test('GenDoc render error'.async() = > {try {
await GenDoc.render(await config({ needDirError: true }));
} catch (error) {
expect(error.message).toMatch('No directory was matched, please confirm input path'); }}); test('GenDoc getRenderData nodefault'.async() = > {const res = await GenDoc.getRenderData(await config({ noDefault: true }));
expect(typeof res === 'object').toBe(true);
});
test('GenDoc getRenderData'.async() = > {const res = await GenDoc.getRenderData(await config(), false);
expect(typeof res === 'object').toBe(true);
});
test('GenDoc getFileContent'.() = > {
const res = GenDoc.getFileContent('./README.md');
expect(typeof res === 'string').toBe(true);
});
Copy the code
The API documentation
GenDoc
GenDoc’s powerful utility classes automatically generate documentation based on annotations and runnable sample code
The introduction of
const GenDoc = require('@agds/cli-plugin-doc');
Copy the code
Properties: class
⇒ GenDoc. Render (options)Promise.<string>
Based on EJS, render documents with templates
Property: GenDoc static method return value: Promise.
– Asynchronously returns document text rendered based on EJS templates
parameter | type | describe |
---|---|---|
options | RenderOptions |
Gets the data used to render the template |
GenDoc. GetRenderData ⇒ (options, [needMergeConfig])Promise.<GetRenderDataResult>
Get the data used to render the template (contents of jsDoc generated documentation and sample code)
Property: GenDoc static method
parameter | type | The default value | describe |
---|---|---|---|
options | RenderOptions |
Configuration parameters | |
[needMergeConfig] | boolean |
true |
Whether to call_mergeToDefaultConfig Options are already processed by merge, so you do not need to call options. Otherwise, you are not recommended to pass in optionsfalse The alias is not supported |
⇒ GenDoc. GetFilesCode (options)Promise.<Array.<GetFilesCodeResult>>
Glob based file traversal function, returns an array of the corresponding contents of the file, returns the file content object by folder, key is the extname of the file
Property: GenDoc static method
parameter | type | describe |
---|---|---|
options | GetFilesCodeOptions |
Gets the file path configuration parameter for the source code |
⇒ GenDoc. GetCliUsages ()Promise.<Array.<string>>
The command line help documentation recommends making sure that the latest script function is used globally as an asynchronous function in advance, and that it is not passed in as an EJS helper function. Instead, you can get the return value and pass it in as a variable of helpers
Property: GenDoc static method
⇒ GenDoc. GetFileContent (filename)string
Reading file contents
Property: GenDoc static method
parameter | type | describe |
---|---|---|
filename | string |
The file path relative to the running directory |
GenDoc. RenderCode (codes, [extSort], [extTrans]) ⇒string
Render codes as MD snippets
Property: GenDoc static method
parameter | type | The default value | describe |
---|---|---|---|
codes | Array.<GetFilesCodeResult> |
GenDoc.getFilesCode The codes array obtained by the |
|
[extSort] | Array.<string> |
['md', 'vue', 'jsx', 'js'] |
Take precedence and followextSort Array order obtains traversal codes |
[extTrans] | Object.<string, string> |
{vue:'html'} |
Simple example of mapping map for ext transformation{vue:'html'} |
GetRenderDataResult : object
The return value of the gendoc. getRenderData function
Nature: Type declaration
attribute
attribute | type | describe |
---|---|---|
docs | string |
The source code uses JSDoc rendered Markdown text |
codes | Array.<GetFilesCodeResult> |
Get the content of the code |
RenderOptions : object
Configuration parameters for the render function
Nature: Type declaration
attribute
attribute | type | The default value | describe |
---|---|---|---|
files | Array.<string> |
jsdoc2mdOptions.files The alias |
|
output | fs.PathLike |
Doc Document rendering export file name path, relative to CWD directory | |
template | string |
Ejs renders the template relative to the CWD path or absolute path | |
[codesDir] | string |
codesOptions.dir The alias |
|
[codesFiles] | Array.<string> |
codesOptions.codesFiles The alias |
|
[conifg] | fs.PathLike |
agds.doc.config.js |
Path to the configuration file. The default path is in the running directoryagds.doc.config.js , only supportjs The file type |
[default] | boolean |
Whether to merge the default configuration? Generally, we believe that you need the default configuration. If the default configuration conflicts with your requirements, you can set it tofalse |
|
[jsdoc2mdOptions] | Jsdoc2mdOptions |
JsdocToMarkdown Configuration parameter | |
[codesOptions] | GetFilesCodeOptions |
Gets the file path configuration parameter for the source code | |
[jsdocEngineOptions] | object |
The configuration of the JSDoc parsing engine is actuallyjsdoc.conf.js Integration can also be usedRenderOptions.jsdoc2mdOptions.configure Field to specify local JSDOC configuration configuration options👉 Reference Documents |
|
[helpers] | DefaultHelpers |
Injected into ejS templateshelpers Object that provides helper functions and variables used by the template to work with the template |
|
[presets] | Array.<RenderOptions> |
Based on the Preset mechanism, the configuration is implemented to support preset features, specifically👉 Reference DocumentsPresetUtils.getDeepPresetMerge |
|
[modify] | module:@agds/node-utils~ConfigModify |
Set the default configuration and preset to the hook concrete to be processed again by the merged generated config👉 Reference Documents |
GetFilesCodeResult : Object.<string, string>
Gets the return value type for the contents of the file. Key is the extname of the file
Nature: Type declaration
DefaultHelpers : Object
Helpers property supported by the default template
Nature: Type declaration
attribute
attribute | type | describe |
---|---|---|
[installCode] | string |
Installation script, bash script, defaultnpm i ${pkg.name} If not, you can use this field to modify |
[devInstall] | boolean |
Is it downloaded as a development dependency,true When, the default download code automatically splices NPM-D parameter |
[importCode] | string |
Introduce code example, JS string |
[exportCode] | string |
Export code, js string |
[cliUsages] | Array.<string> |
The help document for cli command lines is similar in formatagds-doc -h Output content of |
[remark] | string |
The value of md is a character string |
[renderCode] | renderCode |
willGenDoc.getFileCodes Render the return value of |
[postfixes] | Array.<Postfix> |
Suffix content array |
[logo] | string |
logo |
[bradges] | Array.<Badge> |
Logo array |
Badge : Object
Logo objects
Nature: Type declaration
attribute
attribute | type | describe |
---|---|---|
url | string |
Image links |
[name] | string |
Image name |
[link] | string |
Skip links |
Postfix : Object
Postfix content type
Nature: Type declaration
attribute
attribute | type | describe |
---|---|---|
[id] | string |
The name of the anchor point can be supported after filling inhref="#${id}" The anchor point positioning |
[title] | string |
Title of content |
[desc] | string |
Description of content |
[content] | string |
Body of content |
GetFilesCodeOptions : object
Gets the file path configuration parameter for the source code
Nature: Type declaration
attribute
attribute | type | describe |
---|---|---|
dir | string |
Glob path |
files | Array.<string> |
Glob array of file names |
Jsdoc2mdOptions : object
JsdocToMarkdown configuration parameters. For details, see 👉
Nature: Type declaration
Default document rendering template
<% const {docs, codes, helpers, pkg} = locals %><% if(helpers.logo) { %><p align="center"> <img src="<%- helpers.logo %>" alt="logo"> </p> <% } if(pkg.name){ %># <%- pkg.name %><% } %><% if (helpers.badges && helpers.badges.length > 0) { %> <%- helpers.badges.map(item => { let badge = `! [${item.name}](${item.url})`; if (item.link) { badge = `[${badge}](${item.link})`; } return badge; }).join(' '); <% if(pkg.version){%> ** Version ** : <%- pkg.version %><% } %><% if(pkg.description){ %> <%- pkg.description %><% } %><% If (PKG. Name | | helpers. ImportCode | | helpers. ExportCode) {% > # # quickly start} < % % > < % if (PKG. Name) {% > # # # < % % > installation ` ` ` bash < % - helpers.installCode || 'npm i '+ (helpers.devInstall? '-D ' : ':') + PKG. Name % > < % % > ` ` `} < % % > < % if (helpers. ImportCode) introduced {% > # # # < % % > ` ` ` js < % - helpers. ImportCode % > < % % > ` ` ` < % } % > < % if (helpers. ExportCode) {% > # # # < % % > derived ` ` ` js < % - helpers. ExportCode % > < % % > ` ` `} < % % > < % If (helpers. CliUsages && helpers. CliUsages. Length) {% > # # # command USES the document < % helpers. CliUsages. ForEach (usage = > {% > < % % > ` ` ` < % - usage %> <% %>```<% }) %><% } %><% if(helpers.remark) { %> <%- helpers.remark %> <% } %><% if(codes&&codes.length) { %> # # code demo < % - helpers. RenderCode && helpers. RenderCode (codes) % > < %} % > < % if (docs) {% > # # API documentation - docs < % % > < %} % > < % If (helpers. Postfixes && helpers. Postfixes. Length) {/ / suffix content % > < % helpers. The postfixes. ForEach (postfix = > {% > < % if (postfix. Id) { %> <a name="<%- postfix.id %>"></a><% } %><% if(postfix.title) { %> ## <%- postfix.title %> <% } %><% if(postfix.desc) { %> > <%- postfix.desc %> <% } %><% if(postfix.content) { %> <%- postfix.content %><% } }) } %>Copy the code
Default document rendering configuration
The current __dirname is @agds/cli-plugin-doc/lib/utils
const path = require('path');
const defaultTemplate = path.resolve(__dirname, '.. /template/template.ejs');
const renderCode = require('./renderCode');
const defaultConfig = {
template: defaultTemplate,
jsdoc2mdOptions: {
// 'no-cache': true,
partial: [path.resolve(__dirname, '.. /dmdRewrite/partials/*.hbs')].helper: [path.resolve(__dirname, '.. /dmdRewrite/helpers/*.js')].'heading-depth': 3,},// The default 'jsdocEngineOptions' configuration can only be added, but cannot be deleted.
// However, noDefault can be configured to remove the default configuration
jsdocEngineOptions: {
plugins: [
require.resolve('jsdoc-tsimport-plugin'),
require.resolve('./jsdoc-plugin-arrowfn'),
require.resolve('jsdoc-typeof-plugin')]},helpers: {
renderCode,
},
};
module.exports = defaultConfig;
Copy the code
license
MIT License Copyright (c) 2021 Jinyang