@q78kg/koishi-plugin-text-censor 1.0.4-beta.3 → 1.0.4-beta.5
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 +40 -22
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
@@ -38,41 +38,59 @@ 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().role("text"),
|
49
|
+
import_koishi.Schema.string().default("data/Censor.txt")
|
50
|
+
])
|
51
|
+
).description("敏感词库的文件路径。").default([["", "data/Censor.txt"]])
|
52
|
+
}),
|
53
|
+
import_koishi.Schema.object({
|
54
|
+
removeWords: import_koishi.Schema.boolean().description("是否直接删除敏感词。").default(false),
|
55
|
+
// 默认不删除敏感词
|
56
|
+
transformToUpper: import_koishi.Schema.boolean().description("是否将字符转换为大写。").default(false)
|
57
|
+
// 默认不转换字符为大写
|
58
|
+
})
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
60
|
+
]);
|
51
61
|
function apply(ctx, config) {
|
52
|
-
|
53
|
-
|
54
|
-
ctx.
|
62
|
+
let words = [];
|
63
|
+
for (const [_, file] of config.textDatabase) {
|
64
|
+
const filePath = (0, import_node_path.resolve)(ctx.baseDir, file);
|
65
|
+
if (!(0, import_node_fs.existsSync)(filePath)) {
|
66
|
+
ctx.logger.warn(`dictionary file not found: ${filePath}`);
|
67
|
+
continue;
|
68
|
+
}
|
69
|
+
const source = (0, import_node_fs.readFileSync)(filePath, "utf8");
|
70
|
+
const fileWords = source.split("\n").map((word) => word.trim()).filter(
|
71
|
+
(word) => word && !word.startsWith("//") && !word.startsWith("#")
|
72
|
+
);
|
73
|
+
words = words.concat(fileWords);
|
74
|
+
}
|
75
|
+
if (words.length === 0) {
|
76
|
+
ctx.logger.warn("no sensitive words found");
|
55
77
|
return;
|
56
78
|
}
|
57
|
-
const
|
58
|
-
const words = source.split("\n").map((word) => word.trim()).filter((word) => word && !word.startsWith("//") && !word.startsWith("#"));
|
59
|
-
const mintOptions = {
|
79
|
+
const MintOptions = {
|
60
80
|
transform: config.transformToUpper ? "capital" : "none"
|
61
81
|
// 这里我们将值设置为可能的类型
|
62
82
|
};
|
63
|
-
const filter = new import_mint_filter.default(words,
|
83
|
+
const filter = new import_mint_filter.default(words, MintOptions);
|
64
84
|
ctx.plugin(import_censor.default);
|
65
|
-
ctx.get("
|
85
|
+
ctx.get("Censor").intercept({
|
66
86
|
async text(attrs) {
|
67
|
-
const
|
87
|
+
const originalText = attrs.content;
|
88
|
+
const result = await filter.filter(originalText);
|
68
89
|
if (typeof result.text !== "string")
|
69
90
|
return [];
|
70
91
|
if (config.removeWords) {
|
71
|
-
const
|
72
|
-
|
73
|
-
return text.replace(regex, "");
|
74
|
-
}, result.text);
|
75
|
-
return [filteredText.trim()];
|
92
|
+
const cleanedText = result.text.replace(/\*/g, "");
|
93
|
+
return [cleanedText.trim()];
|
76
94
|
} else {
|
77
95
|
return [result.text];
|
78
96
|
}
|