JSDoc CLI 有一个有用的-X
标志,您可以使用它来检查(和处理)注释中的元数据:
/**
* @return {number}
*/
function answer() {
return 42;
}
$ jsdoc -X tmp/example.js
[
{
"comment": "/**\n * @return {number}\n */",
"meta": {
"range": [
28,
62
],
"filename": "example.js",
"lineno": 4,
"columnno": 0,
"path": "/workspace/dev/tmp",
"code": {
"id": "astnode100000002",
"name": "answer",
"type": "FunctionDeclaration",
"paramnames": []
}
},
"returns": [
{
"type": {
"names": [
"number"
]
}
}
],
"name": "answer",
"longname": "answer",
"kind": "function",
"scope": "global",
"params": []
},
{
"kind": "package",
"longname": "package:undefined",
"files": [
"/workspace/dev/tmp/example.js"
]
}
]
但是自定义标签呢?该-X
标志也将处理它们:
/**
* @answer 42
*/
function answer() {
return 42;
}
[
{
"comment": "/**\n * @answer 42\n */",
"meta": {
"range": [
22,
56
],
"filename": "example.js",
"lineno": 4,
"columnno": 0,
"path": "/workspace/dev/tmp",
"code": {
"id": "astnode100000002",
"name": "answer",
"type": "FunctionDeclaration",
"paramnames": []
}
},
"tags": [
{
"originalTitle": "answer",
"title": "answer",
"text": "42",
"value": "42"
}
],
"name": "answer",
"longname": "answer",
"kind": "function",
"scope": "global",
"params": []
},
{
"kind": "package",
"longname": "package:undefined",
"files": [
"/workspace/dev/tmp/example.js"
]
}
]
如您所见,输出是纯 JSON,这使得通过 JavaScript 或任何体面的模板系统进行处理变得非常容易。我已经放弃 JSDoc 模板很久了。我自己使用jsdoc -X
和处理注释。
最近我一直在玩 JQ 和 EJS,并将结果提供给 Slate 以生成文档网站:https ://customcommander.github.io/project-blueprint/#project-blueprint-fake-api - 这是文档网站一个虚假的 API。
你想要达到的目标肯定是可行的。希望这足以让您入门。如果您想了解更多:https ://github.com/customcommander/project-blueprint