注释It - 注释提取库
从您的文件中提取注释块 .
特征
- 简单使用
- 提取单行和多行评论块
- 支持范围 语言 覆盖 GitHub (+多) 中使用的前25种语言
- 将字符串和注释分开:字符串文字中的注释符号不是注释,引用
在注释中( 如在
// the file's header(没有启动字符串) - 静曰:
isSupported()返回时false用于一个未知语言的文件,而不记录任何信息
基本用途
import { extractComments } from "@dev-build-deploy/comment-it";
const file = "README.md";
// Check if the file is supported by CommentIt
if (isSupported(file)) {
const config = {
/** Only consider comments in the first ..n lines */
maxLines: 20,
/** Group sequential singleline comments into a comment block */
groupSingleline: true,
};
// Retrieve each comment block using an iterator
for await (const comment of extractComments(file), /* OPTIONAL */ config) {
console.log(JSON.stringify(comment, null, 2));
}
}
上述例子将产生:
{
"type": "multiline",
"format": { "start": "<!--", "end": "-->"},
"contents": [
{
"line": 1,
"column": { "start": 0, "end": 4 },
"raw": "<!--",
"value": ""
},
{
"line": 2,
"column": { "start": 0, "end": 64 },
"raw": "SPDX-FileCopyrightText: 2023 Kevin de Jong <[email protected]>",
"value": "SPDX-FileCopyrightText: 2023 Kevin de Jong <[email protected]>"
},
{
"line": 3,
"column": { "start": 0, "end": 41 },
"raw": "SPDX-License-Identifier: MIT",
"value": "SPDX-License-Identifier: MIT"
},
{
"line": 4,
"column": { "start": 0, "end": 3 },
"raw": "-->",
"value": ""
}
]
}
{
"type": "multiline",
"format": { "start": "<!--", "end": "-->" },
"contents": [
{
"line": 16,
"column": { "start": 0, "end": 45 },
"raw": "<!-- Hee hee, hid a comment block in here -->",
"value": "Hee hee, hid a comment block in here"
}
]
}
自定义语言
你可以用这个 addLanguage() 函数在验证集中添加新语言:
import { addLanguage } from "@dev-build-deploy/comment-it";
addLanguage({
name: "Pingu Language",
filenames: [".pingu"],
extensions: [".noot"],
singleline: "{%NOOTNOOT%}",
});
同样的函数也可以被用来覆盖一个(默认)配置. 譬如说,
以下代码片段取代注释前缀 ; 改为 # 用于文件
页:1.ini 文件扩展名 :
import { addLanguage } from "@dev-build-deploy/comment-it";
addLanguage({
name: "Custom ini",
extensions: [".ini"],
singleline: "#",
});
捐款
如果您对如何改进评论有建议, 或者想要报告错误, 请打开一个问题 ! 我们很乐意所有的贡献.
更多,请检查 贡献指南。 。 。 .
许可证
- 麻省理工学院 2023 凯文·德·钟 <[email protected]>