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

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/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.
package/lib/index.js CHANGED
@@ -1,107 +1,2 @@
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
- });
1
+ var C=Object.create;var d=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var r in e)d(t,r,{get:e[r],enumerable:!0})},g=(t,e,r,p)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of W(e))!P.call(t,a)&&a!==r&&d(t,a,{get:()=>e[a],enumerable:!(p=v(e,a))||p.enumerable});return t};var y=(t,e,r)=>(r=t!=null?C(D(t)):{},g(e||!t||!t.__esModule?d(r,"default",{value:t,enumerable:!0}):r,t)),k=t=>g(d({},"__esModule",{value:!0}),t);var M={};j(M,{Config:()=>T,apply:()=>I,name:()=>F});module.exports=k(M);var o=require("koishi"),s=require("node:fs"),u=require("node:path"),h=y(require("mint-filter")),b=y(require("@koishijs/censor")),F="text-censor",T=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 I(t,e){let r=[];for(let[x]of e.textDatabase){let i=(0,u.resolve)(t.baseDir,x);if(!(0,s.existsSync)(i)){t.logger.warn(`dictionary file not found: ${i}, creating a new one.`);let n=(0,u.dirname)(i);(0,s.existsSync)(n)||(0,s.mkdirSync)(n,{recursive:!0}),(0,s.writeFileSync)(i,"")}let l=(0,s.readFileSync)(i,"utf8").split(`
2
+ `).map(n=>n.trim()).filter(n=>n&&!n.startsWith("//")&&!n.startsWith("#"));r=r.concat(l)}if(r.length===0){t.logger.warn("no sensitive words found");return}let p={transform:e.caseStrategy},a=new h.default(r,p);t.plugin(b.default),t.get("censor").intercept({async text(x){let i=x.content,c=await a.filter(i);if(typeof c.text!="string")return[];if(e.removeWords){let l=i,n=0;for(let f=0;f<c.text.length;f++)if(c.text[f]==="*"){let m=0;for(;f+m<c.text.length&&c.text[f+m]==="*";)m++;let w=l.slice(0,n),S=l.slice(n+m);l=w+S,f+=m-1}else n++;return[l.trim()]}else return[c.text]}})}0&&(module.exports={Config,apply,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.5",
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",
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"
@@ -33,15 +37,27 @@
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
+ "@types/node": "^20.11.30",
48
+ "esbuild": "^0.18.20",
49
+ "esbuild-register": "^3.5.0",
50
+ "eslint": "^9.12.0",
51
+ "eslint-config-prettier": "^9.1.0",
52
+ "eslint-config-standard": "^17.1.0",
53
+ "eslint-plugin-import": "^2.30.0",
54
+ "eslint-plugin-n": "^16.6.2",
55
+ "eslint-plugin-prettier": "^5.2.1",
56
+ "eslint-plugin-promise": "^7.1.0",
57
+ "koishi": "^4.17.9",
58
+ "prettier": "^3.3.3",
59
+ "typescript": "^5.5.3",
60
+ "yml-register": "^1.2.5"
45
61
  },
46
62
  "dependencies": {
47
63
  "@koishijs/censor": "^1.0.1",
package/readme.md CHANGED
@@ -1,5 +1,5 @@
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
5
  此插件基于 Aho–Corasick 算法,对输入的文本内容进行过滤,并将所有的敏感词替换为 `*`。
package/lib/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import { Context, Schema } from 'koishi';
2
- export declare const name = "text-censor";
3
- export interface Config {
4
- textDatabase: [string][];
5
- removeWords: boolean;
6
- transformToUpper: boolean;
7
- }
8
- export declare const Config: Schema<Config>;
9
- export declare function apply(ctx: Context, config: Config): void;