@q78kg/koishi-plugin-text-censor 1.0.16 → 1.1.0-beta.2
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.cjs +2 -2
- package/lib/index.d.ts +1 -2
- package/lib/index.mjs +2 -2
- package/package.json +3 -1
package/lib/index.cjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
var
|
2
|
-
`).map(
|
1
|
+
var W=Object.create;var u=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var E=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var M=(t,r)=>{for(var o in r)u(t,o,{get:r[o],enumerable:!0})},w=(t,r,o,g)=>{if(r&&typeof r=="object"||typeof r=="function")for(let f of D(r))!j.call(t,f)&&f!==o&&u(t,f,{get:()=>r[f],enumerable:!(g=S(r,f))||g.enumerable});return t};var $=(t,r,o)=>(o=t!=null?W(E(t)):{},w(r||!t||!t.__esModule?u(o,"default",{value:t,enumerable:!0}):o,t)),A=t=>w(u({},"__esModule",{value:!0}),t);var T={};M(T,{Config:()=>I,apply:()=>R,name:()=>F});module.exports=A(T);var c=require("koishi"),v=require("node:fs"),m=require("node:fs/promises"),x=require("node:path"),C=require("@q78kg/mint-filter"),b=$(require("@koishijs/censor"),1),F="text-censor",I=c.Schema.intersect([c.Schema.object({textDatabase:c.Schema.array(c.Schema.string()).description("\u654F\u611F\u8BCD\u5E93\u7684\u6587\u4EF6\u8DEF\u5F84").default(["data/text-censor/censor.txt"]),regexPatterns:c.Schema.array(c.Schema.string()).description("\u6B63\u5219\u8868\u8FBE\u5F0F\u5339\u914D\u6A21\u5F0F\u5217\u8868").default([])}),c.Schema.object({removeWords:c.Schema.boolean().description("\u662F\u5426\u76F4\u63A5\u5220\u9664\u654F\u611F\u8BCD").default(!1)})]);function R(t,r){let o=new Set,g=async()=>{await Promise.all(r.textDatabase.map(async s=>{let e=(0,x.resolve)(t.baseDir,s);try{(0,v.existsSync)(e)||(t.logger.warn(`dictionary file not found: ${e}, creating a new one.`),await(0,m.mkdir)((0,x.dirname)(e),{recursive:!0}),await(0,m.writeFile)(e,"")),(await(0,m.readFile)(e,"utf8")).split(`
|
2
|
+
`).map(n=>n.trim()).filter(n=>n&&!/^[/#]/.test(n)).forEach(n=>o.add(n))}catch(a){t.logger.error(`Error loading dictionary ${e}:`,a)}}))},f=r.regexPatterns.map(s=>{try{let e=s.lastIndexOf("/");if(s.startsWith("/")&&e>0){let a=s.slice(e+1),i=s.slice(1,e);return new RegExp(i,a)}return new RegExp(s,"gs")}catch(e){return t.logger.warn(`Invalid regex pattern: ${s}, error: ${e.message}`),null}}).filter(s=>s!==null),h=new Map,P=s=>{if(h.has(s))return h.get(s);let e=s,a=[];for(let i of f)for(let n of e.matchAll(i))a.push({start:n.index,end:n.index+n[0].length});if(a.length>0){a.sort((l,d)=>l.start-d.start);let i=a.reduce((l,d)=>{let p=l[l.length-1];return!p||p.end<d.start?l.push(d):p.end=Math.max(p.end,d.end),l},[]),n=[],y=0;for(let{start:l,end:d}of i)n.push(e.slice(y,l)),n.push(r.removeWords?"":"*".repeat(d-l)),y=d;n.push(e.slice(y)),e=n.join("")}return h.set(s,e),e};t.plugin(b.default.default??b.default),t.get("censor").intercept({async text(s){let e=P(s.content);if(o.size>0){let i=(0,C.createMintFilter)([...o],{customCharacter:"*"}).filter(e,{replace:!r.removeWords});if(!i||typeof i.text!="string")return[];r.removeWords&&(e=i.text.split("").filter(n=>n!=="*").join(""))}return[e.trim()]}}),g()}0&&(module.exports={Config,apply,name});
|
package/lib/index.d.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
import { Context, Schema } from 'koishi';
|
2
2
|
export declare const name = "text-censor";
|
3
3
|
export interface Config {
|
4
|
-
textDatabase:
|
4
|
+
textDatabase: string[];
|
5
5
|
removeWords: boolean;
|
6
|
-
caseStrategy: 'capital' | 'none' | 'lower';
|
7
6
|
regexPatterns: string[];
|
8
7
|
}
|
9
8
|
export declare const Config: Schema<Config>;
|
package/lib/index.mjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import{Schema as
|
2
|
-
`).map(r=>r.trim()).filter(r=>r
|
1
|
+
import{Schema as i}from"koishi";import{existsSync as y}from"node:fs";import{mkdir as b,writeFile as w,readFile as v}from"node:fs/promises";import{dirname as C,resolve as P}from"node:path";import{createMintFilter as W}from"@q78kg/mint-filter";import p from"@koishijs/censor";var F="text-censor",I=i.intersect([i.object({textDatabase:i.array(i.string()).description("\u654F\u611F\u8BCD\u5E93\u7684\u6587\u4EF6\u8DEF\u5F84").default(["data/text-censor/censor.txt"]),regexPatterns:i.array(i.string()).description("\u6B63\u5219\u8868\u8FBE\u5F0F\u5339\u914D\u6A21\u5F0F\u5217\u8868").default([])}),i.object({removeWords:i.boolean().description("\u662F\u5426\u76F4\u63A5\u5220\u9664\u654F\u611F\u8BCD").default(!1)})]);function R(l,c){let f=new Set,u=async()=>{await Promise.all(c.textDatabase.map(async t=>{let e=P(l.baseDir,t);try{y(e)||(l.logger.warn(`dictionary file not found: ${e}, creating a new one.`),await b(C(e),{recursive:!0}),await w(e,"")),(await v(e,"utf8")).split(`
|
2
|
+
`).map(r=>r.trim()).filter(r=>r&&!/^[/#]/.test(r)).forEach(r=>f.add(r))}catch(s){l.logger.error(`Error loading dictionary ${e}:`,s)}}))},x=c.regexPatterns.map(t=>{try{let e=t.lastIndexOf("/");if(t.startsWith("/")&&e>0){let s=t.slice(e+1),n=t.slice(1,e);return new RegExp(n,s)}return new RegExp(t,"gs")}catch(e){return l.logger.warn(`Invalid regex pattern: ${t}, error: ${e.message}`),null}}).filter(t=>t!==null),m=new Map,h=t=>{if(m.has(t))return m.get(t);let e=t,s=[];for(let n of x)for(let r of e.matchAll(n))s.push({start:r.index,end:r.index+r[0].length});if(s.length>0){s.sort((o,a)=>o.start-a.start);let n=s.reduce((o,a)=>{let d=o[o.length-1];return!d||d.end<a.start?o.push(a):d.end=Math.max(d.end,a.end),o},[]),r=[],g=0;for(let{start:o,end:a}of n)r.push(e.slice(g,o)),r.push(c.removeWords?"":"*".repeat(a-o)),g=a;r.push(e.slice(g)),e=r.join("")}return m.set(t,e),e};l.plugin(p.default??p),l.get("censor").intercept({async text(t){let e=h(t.content);if(f.size>0){let n=W([...f],{customCharacter:"*"}).filter(e,{replace:!c.removeWords});if(!n||typeof n.text!="string")return[];c.removeWords&&(e=n.text.split("").filter(r=>r!=="*").join(""))}return[e.trim()]}}),u()}export{I as Config,R as apply,F as name};
|
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
|
+
"version": "1.1.0-beta.2",
|
5
5
|
"main": "lib/index.cjs",
|
6
6
|
"module": "lib/index.mjs",
|
7
7
|
"types": "lib/index.d.ts",
|
@@ -56,6 +56,8 @@
|
|
56
56
|
},
|
57
57
|
"devDependencies": {
|
58
58
|
"@types/node": "^20.11.30",
|
59
|
+
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
60
|
+
"@typescript-eslint/parser": "^8.22.0",
|
59
61
|
"esbuild": "^0.18.20",
|
60
62
|
"esbuild-register": "^3.5.0",
|
61
63
|
"eslint": "^9.12.0",
|