尽管@Remi TypeDoc 表示更适合 TypeScript,但我使用 JSDoc 来更好地进行 jsdoc 到 markdown 的转换(在撰写本文时)。
方法一:
编译 TypeScript 并将 JSDoc 与编译后的代码一起使用
> tsc && nodemon node_modules/.bin/jsdoc -c jsdoc.json dist/**/*
方法二:
我正在使用下面的方法和 jsdoc2markdown(它使用 jsdoc)。使用jsdoc-babel、@babel/cli、@babel/core、@babel/preset-env、@babel/preset-typescript 和以下 jsdoc 配置:
{
"source": {
"includePattern": ".+\\.ts(doc|x)?$",
"excludePattern": ".+\\.(test|spec).ts"
},
"plugins": [
"plugins/markdown",
"node_modules/jsdoc-babel"
],
"babel": {
"extensions": ["ts", "tsx"],
"ignore": ["**/*.(test|spec).ts"],
"babelrc": false,
"presets": [["@babel/preset-env", { "targets": { "node": true } }], "@babel/preset-typescript"],
"plugins": ["@babel/proposal-class-properties", "@babel/proposal-object-rest-spread"]
}
}
这个配置:
- 从编译和文档中排除 your-file.test.ts、your-file.spec.ts 和 your-file.js 文件。
- 包括 your-file.ts、your-file.tsdoc 和 your-file.tsx
- 使用
@babel/preset-env,以当前节点版本为目标进行转换。 (您可以根据需要更改和测试它)=> 参见 preset-env 文档
- 使用
@babel/preset-typescript 使 babel 能够解析 TypeScript
提示和技巧
JSDoc 注释消失
Babel 和 TypeScript 在编译过程中删除了一些 JSDoc cmets:
通过添加如下所示的 STUB 代码可以解决此问题:
let STUB = 1;
/**
* Some description
* @typedef {Object} Config
* @property {string} name - Name of the config.
* @property {string} color - Color of choice.
*/
STUB = 1;
export type Config = {
name: string;
color: string;
};
我很久以前使用 jsdoc-babel 编写了一个 wiki 页面,用于将 TypeScript 与 jsdoc2md 结合使用。
可能有帮助:https://github.com/jsdoc2md/jsdoc-to-markdown/wiki/How-to-document-TypeScript
方法 3(TypeDoc)
这不是直接询问的内容,而是与之密切相关。以下工作流程可能有用:
- 使用 TypeDoc 生成 HTML 页面。
- 使用typedoc-plugin-markdown 生成多个 MarkDown 页面。
- 使用concat-md 从多个 MarkDown 页面生成单个 MarkDown 页面。 (这是我最近开发的一个 npm 包,用于解决我的 JSDoc/TypeScript 单页 MarkDown 需求。)
示例
$ typedoc --plugin typedoc-plugin-markdown --mode file --out docs
$ npx concat-md --decrease-title-levels --dir-name-as-title docs > README.md