@q78kg/koishi-plugin-text-censor 1.0.4-beta.1 → 1.0.4-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +2 -2
 - package/lib/index.js +39 -24
 - package/package.json +2 -3
 
    
        package/lib/index.d.ts
    CHANGED
    
    | 
         @@ -1,9 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            import { Context, Schema } from 'koishi';
         
     | 
| 
       2 
2 
     | 
    
         
             
            export declare const name = "text-censor";
         
     | 
| 
       3 
3 
     | 
    
         
             
            export interface Config {
         
     | 
| 
       4 
     | 
    
         
            -
                 
     | 
| 
      
 4 
     | 
    
         
            +
                textDatabase: [string][];
         
     | 
| 
       5 
5 
     | 
    
         
             
                removeWords: boolean;
         
     | 
| 
       6 
     | 
    
         
            -
                 
     | 
| 
      
 6 
     | 
    
         
            +
                caseStrategy: 'capital' | 'none' | 'lower';
         
     | 
| 
       7 
7 
     | 
    
         
             
            }
         
     | 
| 
       8 
8 
     | 
    
         
             
            export declare const Config: Schema<Config>;
         
     | 
| 
       9 
9 
     | 
    
         
             
            export declare function apply(ctx: Context, config: Config): void;
         
     | 
    
        package/lib/index.js
    CHANGED
    
    | 
         @@ -38,27 +38,50 @@ 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 
     | 
    
         
            +
                caseStrategy: import_koishi.Schema.union(["none", "lower", "capital"]).description("敏感词处理时的大小写策略。").default("none")
         
     | 
| 
      
 55 
     | 
    
         
            +
                // 默认不处理大小写
         
     | 
| 
      
 56 
     | 
    
         
            +
              })
         
     | 
| 
      
 57 
     | 
    
         
            +
              // eslint-disable-next-line @typescript-eslint/no-explicit-any
         
     | 
| 
      
 58 
     | 
    
         
            +
            ]);
         
     | 
| 
       51 
59 
     | 
    
         
             
            function apply(ctx, config) {
         
     | 
| 
       52 
     | 
    
         
            -
               
     | 
| 
       53 
     | 
    
         
            -
               
     | 
| 
       54 
     | 
    
         
            -
                ctx. 
     | 
| 
      
 60 
     | 
    
         
            +
              let words = [];
         
     | 
| 
      
 61 
     | 
    
         
            +
              for (const [file] of config.textDatabase) {
         
     | 
| 
      
 62 
     | 
    
         
            +
                const filePath = (0, import_node_path.resolve)(ctx.baseDir, file);
         
     | 
| 
      
 63 
     | 
    
         
            +
                if (!(0, import_node_fs.existsSync)(filePath)) {
         
     | 
| 
      
 64 
     | 
    
         
            +
                  ctx.logger.warn(
         
     | 
| 
      
 65 
     | 
    
         
            +
                    `dictionary file not found: ${filePath}, creating a new one.`
         
     | 
| 
      
 66 
     | 
    
         
            +
                  );
         
     | 
| 
      
 67 
     | 
    
         
            +
                  const dirPath = (0, import_node_path.dirname)(filePath);
         
     | 
| 
      
 68 
     | 
    
         
            +
                  if (!(0, import_node_fs.existsSync)(dirPath)) {
         
     | 
| 
      
 69 
     | 
    
         
            +
                    (0, import_node_fs.mkdirSync)(dirPath, { recursive: true });
         
     | 
| 
      
 70 
     | 
    
         
            +
                  }
         
     | 
| 
      
 71 
     | 
    
         
            +
                  (0, import_node_fs.writeFileSync)(filePath, "");
         
     | 
| 
      
 72 
     | 
    
         
            +
                }
         
     | 
| 
      
 73 
     | 
    
         
            +
                const source = (0, import_node_fs.readFileSync)(filePath, "utf8");
         
     | 
| 
      
 74 
     | 
    
         
            +
                const fileWords = source.split("\n").map((word) => word.trim()).filter(
         
     | 
| 
      
 75 
     | 
    
         
            +
                  (word) => word && !word.startsWith("//") && !word.startsWith("#")
         
     | 
| 
      
 76 
     | 
    
         
            +
                );
         
     | 
| 
      
 77 
     | 
    
         
            +
                words = words.concat(fileWords);
         
     | 
| 
      
 78 
     | 
    
         
            +
              }
         
     | 
| 
      
 79 
     | 
    
         
            +
              if (words.length === 0) {
         
     | 
| 
      
 80 
     | 
    
         
            +
                ctx.logger.warn("no sensitive words found");
         
     | 
| 
       55 
81 
     | 
    
         
             
                return;
         
     | 
| 
       56 
82 
     | 
    
         
             
              }
         
     | 
| 
       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 
83 
     | 
    
         
             
              const mintOptions = {
         
     | 
| 
       60 
     | 
    
         
            -
                transform: config. 
     | 
| 
       61 
     | 
    
         
            -
                // 这里我们将值设置为可能的类型
         
     | 
| 
      
 84 
     | 
    
         
            +
                transform: config.caseStrategy
         
     | 
| 
       62 
85 
     | 
    
         
             
              };
         
     | 
| 
       63 
86 
     | 
    
         
             
              const filter = new import_mint_filter.default(words, mintOptions);
         
     | 
| 
       64 
87 
     | 
    
         
             
              ctx.plugin(import_censor.default);
         
     | 
| 
         @@ -69,15 +92,7 @@ function apply(ctx, config) { 
     | 
|
| 
       69 
92 
     | 
    
         
             
                  if (typeof result.text !== "string")
         
     | 
| 
       70 
93 
     | 
    
         
             
                    return [];
         
     | 
| 
       71 
94 
     | 
    
         
             
                  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("");
         
     | 
| 
      
 95 
     | 
    
         
            +
                    const cleanedText = result.text.replace(/\*/g, "");
         
     | 
| 
       81 
96 
     | 
    
         
             
                    return [cleanedText.trim()];
         
     | 
| 
       82 
97 
     | 
    
         
             
                  } else {
         
     | 
| 
       83 
98 
     | 
    
         
             
                    return [result.text];
         
     | 
    
        package/package.json
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            {
         
     | 
| 
       2 
2 
     | 
    
         
             
              "name": "@q78kg/koishi-plugin-text-censor",
         
     | 
| 
       3 
3 
     | 
    
         
             
              "description": "A text censor provider for Koishi",
         
     | 
| 
       4 
     | 
    
         
            -
              "version": "1.0.4-beta. 
     | 
| 
      
 4 
     | 
    
         
            +
              "version": "1.0.4-beta.11",
         
     | 
| 
       5 
5 
     | 
    
         
             
              "main": "lib/index.js",
         
     | 
| 
       6 
6 
     | 
    
         
             
              "typings": "lib/index.d.ts",
         
     | 
| 
       7 
7 
     | 
    
         
             
              "files": [
         
     | 
| 
         @@ -33,8 +33,7 @@ 
     | 
|
| 
       33 
33 
     | 
    
         
             
                  ]
         
     | 
| 
       34 
34 
     | 
    
         
             
                },
         
     | 
| 
       35 
35 
     | 
    
         
             
                "description": {
         
     | 
| 
       36 
     | 
    
         
            -
                  " 
     | 
| 
       37 
     | 
    
         
            -
                  "zh": "文本敏感词过滤"
         
     | 
| 
      
 36 
     | 
    
         
            +
                  "zh": "文本敏感词过滤服务,支持多敏感词库"
         
     | 
| 
       38 
37 
     | 
    
         
             
                }
         
     | 
| 
       39 
38 
     | 
    
         
             
              },
         
     | 
| 
       40 
39 
     | 
    
         
             
              "peerDependencies": {
         
     |