@wordpress/docgen 是一个用于生成代码公共 API 文档的工具,支持 ES6 导出语句和 JSDoc 注释的提取,并可通过 TypeScript Babel 插件提供 TypeScript 支持。它通过解析入口文件,输出人类可读的格式,如 Markdown 文件。
// 默认导出示例
/**
* Adds two numbers.
*
* @param {number} term1 First number.
* @param {number} term2 Second number.
* @return {number} The result of adding the two numbers.
*/
export default function addition( term1, term2 ) {
// Implementation would go here.
}docgen helps you to generate the public API of your code. Given an entry point file, it outputs the ES6 export statements and their corresponding JSDoc comments in human-readable format. It also supports TypeScript via the TypeScript babel plugin.
Some characteristics:
import default from './dependency' will find dependency.js or dependency/index.jsdocgen.Install the module
npm install @wordpress/docgen --save-dev
Note: This package requires Node.js version with long-term support status (check Active LTS or Maintenance LTS releases). It is not compatible with older versions.
npx docgen <entry-point.js>
This command will generate a file named entry-point-api.md containing all the exports and their JSDoc comments.
(String): A path to a custom formatter to control the contents of the output file. It should be a CommonJS module that exports a function that takes as input:
(String): current working directory as seen by docgen.(String): path of the output document to generate.(Array): the symbols found.(RegExp): A regular expression used to ignore symbols whose name match it.(String): Output file that will contain the API documentation.(String): Append generated documentation to this section in the Markdown output. To be used by the default Markdown formatter. Depends on --output and bypasses the custom --formatter passed, if any.--output and bypasses the custom --formatter passed, if any.
<!-- START TOKEN(Autogenerated API docs) --><!-- END TOKEN(Autogenerated API docs) -->(String): This options allows you to customize the string between the tokens. For example, --use-token my-api will look up for the start token <!-- START TOKEN(my-api) --> and the end token <!-- END TOKEN(my-api) -->. Depends on --to-token.@wordpress/docgen follows the default project-wide configuration of Babel. Like Babel, it will automatically search for a babel.config.json file, or an equivalent one using the supported extensions, in the project root directory.
Without it, @wordpress/docgen runs with the default option. In other words, it cannot parse JSX or other advanced syntaxes.
Entry point index.js:
/**
* Adds two numbers.
*
* @param {number} term1 First number.
* @param {number} term2 Second number.
* @return {number} The result of adding the two numbers.
*/
export default function addition( term1, term2 ) {
// Implementation would go here.
}
Output of npx docgen index.js would be index-api.js:
# API
## default
[example.js#L8-L10](example.js#L8-L10)
Adds two numbers.
**Parameters**
- **term1** `number`: First number.
- **term2** `number`: Second number.
**Returns**
`number` The result of adding the two numbers.
Entry point index.js:
/**
* Adds two numbers.
*
* @param {number} term1 First number.
* @param {number} term2 Second number.
* @return {number} The result of adding the two numbers.
*/
function addition( term1, term2 ) {
return term1 + term2;
}
/**
* Adds two numbers.
*
* @deprecated Use `addition` instead.
*
* @param {number} term1 First number.
* @param {number} term2 Second number.
* @return {number} The result of adding the two numbers.
*/
function count( term1, term2 ) {
return term1 + term2;
}
export { count, addition };
Output of npx docgen index.js would be index-api.js:
# API
## addition
[example.js#L25-L25](example.js#L25-L25)
Adds two numbers.
**Parameters**
- **term1** `number`: First number.
- **term2** `number`: Second number.
**Returns**
`number` The result of adding the two numbers.
## count
[example.js#L25-L25](example.js#L25-L25)
> **Deprecated** Use `addition` instead.
Adds two numbers.
**Parameters**
- **term1** `number`: First number.
- **term2** `number`: Second number.
**Returns**
`number` The result of adding the two numbers.
Let the entry point be index.js:
export * from './count';
with ./count/index.js contents being:
/**
* Subtracts two numbers.
*
* @example
*
* ```js
* const result = subtraction( 5, 2 );
* console.log( result ); // Will log 3
* ```
*
* @param {number} term1 First number.
* @param {number} term2 Second number.
* @return {number} The result of subtracting the two numbers.
*/
export function subtraction( term1, term2 ) {
return term1 - term2;
}
/**
* Adds two numbers.
*
* @example
*
* ```js
* const result = addition( 5, 2 );
* console.log( result ); // Will log 7
* ```
*
* @param {number} term1 First number.
* @param {number} term2 Second number.
* @return {number} The result of adding the two numbers.
*/
export function addition( term1, term2 ) {
// Implementation would go here.
return term1 - term2;
}
Output of npx docgen index.js would be index-api.js:
# API
## addition
[example-module.js#L1-L1](example-module.js#L1-L1)
Adds two numbers.
**Usage**
```js
const result = addition( 5, 2 );
console.log( result ); // Will log 7
```
**Parameters**
- **term1** `number`: First number.
- **term2** `number`: Second number.
**Returns**
`number` The result of adding the two numbers.
## subtraction
[example-module.js#L1-L1](example-module.js#L1-L1)
Subtracts two numbers.
**Usage**
```js
const result = subtraction( 5, 2 );
console.log( result ); // Will log 3
```
**Parameters**
- **term1** `number`: First number.
- **term2** `number`: Second number.
**Returns**
`number` The result of subtracting the two numbers.
Entry point index.ts:
/**
* Adds two numbers.
*
* @param term1 First number.
* @param term2 Second number.
* @return The result of adding the two numbers.
*/
export default function addition( term1: number, term2: number ): number {
// Implementation would go here.
}
Output of npx docgen index.ts would be index-api.js:
# API
## default
[example.js#L8-L10](example.js#L8-L10)
Adds two numbers.
**Parameters**
- **term1** `number`: First number.
- **term2** `number`: Second number.
**Returns**
`number` The result of adding the two numbers.
This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.
To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.