从JSDOC评论中导出结构化JS/JSON的任何标准方法

发布于 2025-02-08 11:01:17 字数 143 浏览 0 评论 0原文

我想在交互式组件中使用JavaScript文件中的JSDOC注释(例如出现在Hover上的属性定义),但是我找不到这样做的方法,因为JSDOC主要打算导出HTML文件。

是否有一种直接的方法可以将文件从文件中解析为结构化数据,然后我可以在UI中消耗这些数据?

I want to use jsdoc comments from a javascript file inside an interactive component (like property definitions appearing on hover), but I can’t find a way to do that as jsdoc is primarily intended to export html files.

Is there a straightforward way to parse jsdoc comments from a file as structured data that I can then consume in my UI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2025-02-15 11:01:17

[…],因为JSDOC主要打算导出HTML文件。

可能是最常见的用例,但绝对不是唯一的用例。

我个人从不使用JSDOC本身生成文档。我使用-x选项,然后 jq 改变解析树。

示例:index.js

/**
 * The answer to everything
 * @return {number}
 */
function asnwer() {
  return 42;
}
npx jsdoc -X index.js
[
    {
        "comment": "/**\n * The answer to everything\n * @return {number}\n */",
        "meta": {
            "range": [
                56,
                90
            ],
            "filename": "index.js",
            "lineno": 5,
            "columnno": 0,
            "path": "/workspaces/dev",
            "code": {
                "id": "astnode100000002",
                "name": "asnwer",
                "type": "FunctionDeclaration",
                "paramnames": []
            }
        },
        "description": "The answer to everything",
        "returns": [
            {
                "type": {
                    "names": [
                        "number"
                    ]
                }
            }
        ],
        "name": "asnwer",
        "longname": "asnwer",
        "kind": "function",
        "scope": "global",
        "params": []
    },
    {
        "kind": "package",
        "longname": "package:undefined",
        "files": [
            "/workspaces/dev/index.js"
        ]
    }
]

[…]as jsdoc is primarily intended to export html files.

Probably the most common use case but definitely not the only one.

I personally never use JSDoc itself to generate documentation. I use the -X option then to transform the parse tree.

Example: index.js

/**
 * The answer to everything
 * @return {number}
 */
function asnwer() {
  return 42;
}
npx jsdoc -X index.js
[
    {
        "comment": "/**\n * The answer to everything\n * @return {number}\n */",
        "meta": {
            "range": [
                56,
                90
            ],
            "filename": "index.js",
            "lineno": 5,
            "columnno": 0,
            "path": "/workspaces/dev",
            "code": {
                "id": "astnode100000002",
                "name": "asnwer",
                "type": "FunctionDeclaration",
                "paramnames": []
            }
        },
        "description": "The answer to everything",
        "returns": [
            {
                "type": {
                    "names": [
                        "number"
                    ]
                }
            }
        ],
        "name": "asnwer",
        "longname": "asnwer",
        "kind": "function",
        "scope": "global",
        "params": []
    },
    {
        "kind": "package",
        "longname": "package:undefined",
        "files": [
            "/workspaces/dev/index.js"
        ]
    }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文