npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ruoyi-eggjs-sensitive

v1.1.0

Published

Egg plugin for sensitive word detection

Readme

ruoyi-eggjs-sensitive

Egg.js 敏感词检测插件,基于 sensitive-word-tool 实现。

词库来源:Sensitive-lexicon

安装

npm install ruoyi-eggjs-sensitive --save

使用

开启插件

// config/plugin.js
exports.sensitive = {
  enable: true,
  package: 'ruoyi-eggjs-sensitive',
};

配置

// config/config.default.js
const path = require('path');

config.sensitive = {
  enable: true, // 是否启用敏感词检测
  vocabularyPath: path.join(appInfo.baseDir, 'Vocabulary'), // 敏感词库路径
  replace: '*', // 替换字符
};

配置说明

  • enable: 是否启用敏感词检测功能
  • vocabularyPath: 敏感词库文件夹路径,支持多个 txt 文件
  • replace: 敏感词替换字符,默认为 *

敏感词库

插件会自动加载 vocabularyPath 目录下的所有 .txt 文件作为敏感词库。

词库格式:每行一个敏感词

敏感词1
敏感词2
敏感词3

# 开头的行会被忽略(可用于注释):

# 这是注释
敏感词1
敏感词2

使用方式

手动调用 Context 方法

在 Controller 或 Service 中手动检测:

// app/controller/post.js
class PostController extends Controller {
  async create() {
    const { ctx } = this;
    const { content } = ctx.request.body;

    // 检测是否包含敏感词
    const result = ctx.checkSensitive(content);
    if (result.hasSensitiveWord) {
      ctx.body = {
        code: 400,
        message: '内容包含敏感词',
        data: result.sensitiveWords,
      };
      return;
    }

    // 或者直接替换敏感词
    const cleanContent = ctx.replaceSensitive(content);

    // 或者获取敏感词列表
    const words = ctx.getSensitiveWords(content);

    // 保存内容...
    ctx.body = { code: 0, message: 'success' };
  }
}

直接使用 app.sensitive

// app/service/content.js
class ContentService extends Service {
  async filter(text) {
    const { app } = this;

    // 验证是否包含敏感词
    const hasSensitiveWord = app.sensitive.verify(text);
    
    // 获取所有敏感词
    const sensitiveWords = app.sensitive.match(text);
    console.log(sensitiveWords); // ['敏感词1', '敏感词2']

    // 替换敏感词
    const cleanText = app.sensitive.filter(text, '*');

    return cleanText;
  }
}

API

Context 扩展方法

ctx.checkSensitive(text)

检测文本是否包含敏感词

  • 参数:
    • text {String} - 待检测的文本
  • 返回:{Object}
    • hasSensitiveWord {Boolean} - 是否包含敏感词
    • sensitiveWords {Array} - 敏感词数组
ctx.replaceSensitive(text, [replaceChar])

替换文本中的敏感词

  • 参数:
    • text {String} - 待处理的文本
    • replaceChar {String} - 替换字符,默认使用配置中的 replace
  • 返回:{String} - 处理后的文本
ctx.getSensitiveWords(text)

获取文本中的所有敏感词

  • 参数:
    • text {String} - 待检测的文本
  • 返回:{Array} - 敏感词数组

Application 属性

app.sensitive

敏感词检测工具实例,基于 sensitive-word-tool

方法说明:

  • verify(text) - 检测文本是否包含敏感词,返回 boolean
  • match(text) - 获取文本中的所有敏感词,返回数组
  • filter(text, replaceChar) - 替换文本中的敏感词,返回处理后的文本
  • addWords(words) - 添加敏感词到词库,参数为字符串数组
// 检测
const hasSensitive = app.sensitive.verify(text);

// 获取敏感词
const words = app.sensitive.match(text);

// 替换
const cleanText = app.sensitive.filter(text, '*');

// 添加词库
app.sensitive.addWords(['词1', '词2']);

示例

完整配置示例

// config/plugin.js
exports.sensitive = {
  enable: true,
  package: 'ruoyi-eggjs-sensitive',
};

// config/config.default.js
const path = require('path');

config.sensitive = {
  enable: true,
  vocabularyPath: path.join(appInfo.baseDir, 'app/sensitive-words'),
  replace: '***',
};

Controller 使用示例

// app/controller/comment.js
class CommentController extends Controller {
  async create() {
    const { ctx } = this;
    const { content } = ctx.request.body;

    // 手动检测
    const result = ctx.checkSensitive(content);
    
    if (result.hasSensitiveWord) {
      ctx.body = {
        code: 400,
        message: `内容包含敏感词: ${result.sensitiveWords.join(', ')}`,
      };
      return;
    }

    // 保存评论
    const comment = await ctx.service.comment.create({ content });
    
    ctx.body = { code: 0, data: comment };
  }

  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { content } = ctx.request.body;

    // 直接替换敏感词
    const cleanContent = ctx.replaceSensitive(content, '**');

    // 更新评论
    await ctx.service.comment.update(id, { content: cleanContent });
    
    ctx.body = { code: 0, message: 'success' };
  }
}

许可证

MIT

作者

姜彦汐 https://www.undsky.com