@q78kg/koishi-plugin-text-censor 1.0.4-beta.1 → 1.0.4-beta.10
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.d.ts +1 -1
- package/lib/index.js +37 -23
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
@@ -38,27 +38,49 @@ module.exports = __toCommonJS(src_exports);
|
|
38
38
|
var import_koishi = require("koishi");
|
39
39
|
var import_node_fs = require("node:fs");
|
40
40
|
var import_node_path = require("node:path");
|
41
|
-
var import_censor = __toESM(require("@koishijs/censor"));
|
42
41
|
var import_mint_filter = __toESM(require("mint-filter"));
|
42
|
+
var import_censor = __toESM(require("@koishijs/censor"));
|
43
43
|
var name = "text-censor";
|
44
|
-
var Config = import_koishi.Schema.
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
var Config = import_koishi.Schema.intersect([
|
45
|
+
import_koishi.Schema.object({
|
46
|
+
textDatabase: import_koishi.Schema.array(
|
47
|
+
import_koishi.Schema.tuple([
|
48
|
+
import_koishi.Schema.string().default("data/text-censor/censor.txt")
|
49
|
+
])
|
50
|
+
).description("敏感词库的文件路径。").default([["data/text-censor/censor.txt"]])
|
51
|
+
}),
|
52
|
+
import_koishi.Schema.object({
|
53
|
+
removeWords: import_koishi.Schema.boolean().description("是否直接删除敏感词。").default(false),
|
54
|
+
transformToUpper: import_koishi.Schema.boolean().description("是否将字符转换为大写。").default(false)
|
55
|
+
})
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
57
|
+
]);
|
51
58
|
function apply(ctx, config) {
|
52
|
-
|
53
|
-
|
54
|
-
ctx.
|
59
|
+
let words = [];
|
60
|
+
for (const [file] of config.textDatabase) {
|
61
|
+
const filePath = (0, import_node_path.resolve)(ctx.baseDir, file);
|
62
|
+
if (!(0, import_node_fs.existsSync)(filePath)) {
|
63
|
+
ctx.logger.warn(
|
64
|
+
`dictionary file not found: ${filePath}, creating a new one.`
|
65
|
+
);
|
66
|
+
const dirPath = (0, import_node_path.dirname)(filePath);
|
67
|
+
if (!(0, import_node_fs.existsSync)(dirPath)) {
|
68
|
+
(0, import_node_fs.mkdirSync)(dirPath, { recursive: true });
|
69
|
+
}
|
70
|
+
(0, import_node_fs.writeFileSync)(filePath, "");
|
71
|
+
}
|
72
|
+
const source = (0, import_node_fs.readFileSync)(filePath, "utf8");
|
73
|
+
const fileWords = source.split("\n").map((word) => word.trim()).filter(
|
74
|
+
(word) => word && !word.startsWith("//") && !word.startsWith("#")
|
75
|
+
);
|
76
|
+
words = words.concat(fileWords);
|
77
|
+
}
|
78
|
+
if (words.length === 0) {
|
79
|
+
ctx.logger.warn("no sensitive words found");
|
55
80
|
return;
|
56
81
|
}
|
57
|
-
const source = (0, import_node_fs.readFileSync)(filename, "utf8");
|
58
|
-
const words = source.split("\n").map((word) => word.trim()).filter((word) => word && !word.startsWith("//") && !word.startsWith("#"));
|
59
82
|
const mintOptions = {
|
60
83
|
transform: config.transformToUpper ? "capital" : "none"
|
61
|
-
// 这里我们将值设置为可能的类型
|
62
84
|
};
|
63
85
|
const filter = new import_mint_filter.default(words, mintOptions);
|
64
86
|
ctx.plugin(import_censor.default);
|
@@ -69,15 +91,7 @@ function apply(ctx, config) {
|
|
69
91
|
if (typeof result.text !== "string")
|
70
92
|
return [];
|
71
93
|
if (config.removeWords) {
|
72
|
-
const
|
73
|
-
const removedCharacters = Array.from(originalText).filter(
|
74
|
-
(char, index) => filteredText[index] !== char
|
75
|
-
// 如果对应位置的字符不相同,则为被过滤的字符
|
76
|
-
).join("");
|
77
|
-
const cleanedText = originalText.split("").filter(
|
78
|
-
(char) => !removedCharacters.includes(char)
|
79
|
-
// 过滤掉被移除的字符
|
80
|
-
).join("");
|
94
|
+
const cleanedText = result.text.replace(/\*/g, "");
|
81
95
|
return [cleanedText.trim()];
|
82
96
|
} else {
|
83
97
|
return [result.text];
|