@q78kg/koishi-plugin-text-censor 1.0.4-beta.9 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-present Shigma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,8 +1,8 @@
1
1
  # @q78kg/koishi-plugin-text-censor
2
-
2
+
3
3
  [![npm](https://img.shields.io/npm/v/@q78kg/koishi-plugin-text-censor?style=flat-square)](https://www.npmjs.com/package/@q78kg/koishi-plugin-text-censor)
4
4
 
5
- 此插件基于 Aho–Corasick 算法,对输入的文本内容进行过滤,并将所有的敏感词替换为 `*`。
5
+ 此插件基于 Aho–Corasick 算法,对输入的文本内容进行过滤,并将所有的敏感词替换为 `*`。并支持基于上下文匹配的敏感词删除模式。
6
6
 
7
7
  ## 文档
8
8
 
package/lib/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ var x=Object.create;var l=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var v=(t,e)=>{for(var n in e)l(t,n,{get:e[n],enumerable:!0})},m=(t,e,n,c)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of S(e))!h.call(t,s)&&s!==n&&l(t,s,{get:()=>e[s],enumerable:!(c=y(e,s))||c.enumerable});return t};var w=(t,e,n)=>(n=t!=null?x(b(t)):{},m(e||!t||!t.__esModule?l(n,"default",{value:t,enumerable:!0}):n,t)),C=t=>m(l({},"__esModule",{value:!0}),t);var O={};v(O,{Config:()=>D,apply:()=>M,name:()=>W});module.exports=C(O);var o=require("koishi"),r=require("fs"),f=require("path"),d=require("@q78kg/mint-filter"),u=w(require("@koishijs/censor")),W="text-censor",D=o.Schema.intersect([o.Schema.object({textDatabase:o.Schema.array(o.Schema.tuple([o.Schema.string().default("data/text-censor/censor.txt")])).description("\u654F\u611F\u8BCD\u5E93\u7684\u6587\u4EF6\u8DEF\u5F84\u3002").default([["data/text-censor/censor.txt"]])}),o.Schema.object({removeWords:o.Schema.boolean().description("\u662F\u5426\u76F4\u63A5\u5220\u9664\u654F\u611F\u8BCD\u3002").default(!1),caseStrategy:o.Schema.union(["none","lower","capital"]).description("\u654F\u611F\u8BCD\u5904\u7406\u65F6\u7684\u5927\u5C0F\u5199\u7B56\u7565\u3002").default("none")})]);function M(t,e){let n=[];for(let[p]of e.textDatabase){let a=(0,f.resolve)(t.baseDir,p);if(!(0,r.existsSync)(a)){t.logger.warn(`dictionary file not found: ${a}, creating a new one.`);let i=(0,f.dirname)(a);(0,r.existsSync)(i)||(0,r.mkdirSync)(i,{recursive:!0}),(0,r.writeFileSync)(a,"")}let g=(0,r.readFileSync)(a,"utf8").split(`
2
+ `).map(i=>i.trim()).filter(i=>i&&!i.startsWith("//")&&!i.startsWith("#"));n=n.concat(g)}if(n.length===0){t.logger.warn("no sensitive words found");return}let c={transform:e.caseStrategy,remove:e.removeWords},s=new d.Mint(n,c);t.plugin(u.default),t.get("censor").intercept({async text(p){return[s.filter(p.content).text]}})}0&&(module.exports={Config,apply,name});
package/lib/index.d.ts CHANGED
@@ -1,9 +1,12 @@
1
- import { Context, Schema } from 'koishi';
2
- export declare const name = "text-censor";
3
- export interface Config {
1
+ import { Schema, Context } from 'koishi';
2
+
3
+ declare const name = "text-censor";
4
+ interface Config {
4
5
  textDatabase: [string][];
5
6
  removeWords: boolean;
6
- transformToUpper: boolean;
7
+ caseStrategy: 'capital' | 'none' | 'lower';
7
8
  }
8
- export declare const Config: Schema<Config>;
9
- export declare function apply(ctx: Context, config: Config): void;
9
+ declare const Config: Schema<Config>;
10
+ declare function apply(ctx: Context, config: Config): void;
11
+
12
+ export { Config, apply, name };
package/lib/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import{Schema as e}from"koishi";import{existsSync as a,mkdirSync as p,readFileSync as m,writeFileSync as d}from"fs";import{dirname as u,resolve as g}from"path";import{Mint as x}from"@q78kg/mint-filter";import y from"@koishijs/censor";var M="text-censor",O=e.intersect([e.object({textDatabase:e.array(e.tuple([e.string().default("data/text-censor/censor.txt")])).description("\u654F\u611F\u8BCD\u5E93\u7684\u6587\u4EF6\u8DEF\u5F84\u3002").default([["data/text-censor/censor.txt"]])}),e.object({removeWords:e.boolean().description("\u662F\u5426\u76F4\u63A5\u5220\u9664\u654F\u611F\u8BCD\u3002").default(!1),caseStrategy:e.union(["none","lower","capital"]).description("\u654F\u611F\u8BCD\u5904\u7406\u65F6\u7684\u5927\u5C0F\u5199\u7B56\u7565\u3002").default("none")})]);function j(o,i){let r=[];for(let[s]of i.textDatabase){let n=g(o.baseDir,s);if(!a(n)){o.logger.warn(`dictionary file not found: ${n}, creating a new one.`);let t=u(n);a(t)||p(t,{recursive:!0}),d(n,"")}let f=m(n,"utf8").split(`
2
+ `).map(t=>t.trim()).filter(t=>t&&!t.startsWith("//")&&!t.startsWith("#"));r=r.concat(f)}if(r.length===0){o.logger.warn("no sensitive words found");return}let c={transform:i.caseStrategy,remove:i.removeWords},l=new x(r,c);o.plugin(y),o.get("censor").intercept({async text(s){return[l.filter(s.content).text]}})}export{O as Config,j as apply,M as name};
package/package.json CHANGED
@@ -1,13 +1,17 @@
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.9",
4
+ "version": "1.0.9",
5
5
  "main": "lib/index.js",
6
- "typings": "lib/index.d.ts",
7
6
  "files": [
8
7
  "lib"
9
8
  ],
10
9
  "license": "MIT",
10
+ "scripts": {
11
+ "build": "node esbuild.config.mjs && tsc --emitDeclarationOnly && rollup -c rollup.config.mjs",
12
+ "lint": "eslint src",
13
+ "lint-fix": "eslint src --fix"
14
+ },
11
15
  "repository": {
12
16
  "type": "git",
13
17
  "url": "git+https://github.com/Hoshino-Yumetsuki/koishi-plugin-text-censor.git"
@@ -15,7 +19,7 @@
15
19
  "bugs": {
16
20
  "url": "https://github.com/Hoshino-Yumetsuki/koishi-plugin-text-censor/issues"
17
21
  },
18
- "homepage": "https://censor.koishi.chat/plugins/text.html",
22
+ "homepage": "https://github.com/Hoshino-Yumetsuki/koishi-plugin-text-censor",
19
23
  "keywords": [
20
24
  "bot",
21
25
  "chatbot",
@@ -33,18 +37,34 @@
33
37
  ]
34
38
  },
35
39
  "description": {
36
- "en": "Filter out sensitive words in text messages",
37
- "zh": "文本敏感词过滤"
40
+ "zh": "文本敏感词过滤服务,支持多敏感词库"
38
41
  }
39
42
  },
40
43
  "peerDependencies": {
41
44
  "koishi": "^4.17.9"
42
45
  },
43
46
  "devDependencies": {
44
- "koishi": "^4.17.9"
47
+ "@rollup/plugin-multi-entry": "^6.0.1",
48
+ "@types/node": "^20.11.30",
49
+ "esbuild": "^0.18.20",
50
+ "esbuild-register": "^3.5.0",
51
+ "eslint": "^9.12.0",
52
+ "eslint-config-prettier": "^9.1.0",
53
+ "eslint-config-standard": "^17.1.0",
54
+ "eslint-plugin-import": "^2.30.0",
55
+ "eslint-plugin-n": "^16.6.2",
56
+ "eslint-plugin-prettier": "^5.2.1",
57
+ "eslint-plugin-promise": "^7.1.0",
58
+ "koishi": "^4.17.9",
59
+ "prettier": "^3.3.3",
60
+ "rollup": "^4.28.1",
61
+ "rollup-plugin-delete": "^2.1.0",
62
+ "rollup-plugin-dts": "^6.1.1",
63
+ "typescript": "^5.5.3",
64
+ "yml-register": "^1.2.5"
45
65
  },
46
66
  "dependencies": {
47
67
  "@koishijs/censor": "^1.0.1",
48
- "mint-filter": "^3.0.1"
68
+ "@q78kg/mint-filter": "^4.0.6"
49
69
  }
50
70
  }
package/lib/index.js DELETED
@@ -1,107 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
- var __export = (target, all) => {
9
- for (var name2 in all)
10
- __defProp(target, name2, { get: all[name2], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- Config: () => Config,
34
- apply: () => apply,
35
- name: () => name
36
- });
37
- module.exports = __toCommonJS(src_exports);
38
- var import_koishi = require("koishi");
39
- var import_node_fs = require("node:fs");
40
- var import_node_path = require("node:path");
41
- var import_mint_filter = __toESM(require("mint-filter"));
42
- var import_censor = __toESM(require("@koishijs/censor"));
43
- var name = "text-censor";
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
- // 默认不删除敏感词
55
- transformToUpper: import_koishi.Schema.boolean().description("是否将字符转换为大写。").default(false)
56
- // 默认不转换字符为大写
57
- })
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- ]);
60
- function apply(ctx, config) {
61
- let words = [];
62
- for (const [file] of config.textDatabase) {
63
- const filePath = (0, import_node_path.resolve)(ctx.baseDir, file);
64
- if (!(0, import_node_fs.existsSync)(filePath)) {
65
- ctx.logger.warn(
66
- `dictionary file not found: ${filePath}, creating a new one.`
67
- );
68
- (0, import_node_fs.writeFileSync)(filePath, "");
69
- }
70
- const source = (0, import_node_fs.readFileSync)(filePath, "utf8");
71
- const fileWords = source.split("\n").map((word) => word.trim()).filter(
72
- (word) => word && !word.startsWith("//") && !word.startsWith("#")
73
- );
74
- words = words.concat(fileWords);
75
- }
76
- if (words.length === 0) {
77
- ctx.logger.warn("no sensitive words found");
78
- return;
79
- }
80
- const mintOptions = {
81
- transform: config.transformToUpper ? "capital" : "none"
82
- // 这里我们将值设置为可能的类型
83
- };
84
- const filter = new import_mint_filter.default(words, mintOptions);
85
- ctx.plugin(import_censor.default);
86
- ctx.get("censor").intercept({
87
- async text(attrs) {
88
- const originalText = attrs.content;
89
- const result = await filter.filter(originalText);
90
- if (typeof result.text !== "string")
91
- return [];
92
- if (config.removeWords) {
93
- const cleanedText = result.text.replace(/\*/g, "");
94
- return [cleanedText.trim()];
95
- } else {
96
- return [result.text];
97
- }
98
- }
99
- });
100
- }
101
- __name(apply, "apply");
102
- // Annotate the CommonJS export names for ESM import in node:
103
- 0 && (module.exports = {
104
- Config,
105
- apply,
106
- name
107
- });