Skip to content

Commit

Permalink
feat(teach): remove compatibility checks for regexp, fix #299 (#300)
Browse files Browse the repository at this point in the history
* feat(teach): remove compatibility checks for regexp, fix #299

* docs: add teach plugin database hint
  • Loading branch information
shigma committed Jul 8, 2021
1 parent 845afc0 commit b3c5d73
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 32 deletions.
4 changes: 3 additions & 1 deletion docs/plugins/teach/regexp.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ sidebarDepth: 2
- `(patt)`:匹配 patt 并捕获

::: danger 警告
MySQL 5.7 使用的正则表达式规范是 POSIX 1003.2,这是一个很落后的版本。因此,如果你使用 koishi-plugin-mysql,下面的语法都是**不被支持**的:
MySQL 5.7 使用的正则表达式规范是 POSIX 1003.2,这是一个很落后的版本。下面的语法都是**不被支持**的:

- `\d`, `\D`:转义字符集
- `\b`, `\B`:单词边界
- `+?`, `*?`:非贪婪匹配
- `(?:`, `(?=`:非捕获组

因此我们建议你使用 mariadb 10.5 以上的版本以取代 MySQL。
:::

## 正则表达式与称呼匹配问答
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-teach/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"dependencies": {
"axios": "^0.21.1",
"fastest-levenshtein": "^1.0.12",
"regexpp": "^3.1.0"
"fastest-levenshtein": "^1.0.12"
}
}
23 changes: 1 addition & 22 deletions packages/plugin-teach/src/database/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,7 @@ Database.extend('koishi-plugin-mysql', ({ Domain, tables }) => {
}
})

export default function apply(ctx: Context, config: Dialogue.Config) {
config.validateRegExp = {
onEscapeCharacterSet() {
throw new SyntaxError('unsupported escape character set')
},
onQuantifier(start, end, min, max, greedy) {
if (!greedy) throw new SyntaxError('unsupported non-greedy quantifier')
},
onWordBoundaryAssertion() {
throw new SyntaxError('unsupported word boundary assertion')
},
onLookaroundAssertionEnter() {
throw new SyntaxError('unsupported lookaround assertion')
},
onGroupEnter() {
throw new SyntaxError('unsupported non-capturing group')
},
onCapturingGroupEnter(start, name) {
if (name) throw new SyntaxError('unsupported named capturing group')
},
}

export default function apply(ctx: Context) {
ctx.on('dialogue/flag', (flag) => {
ctx.on('dialogue/mysql', (test, conditionals) => {
if (test[flag] === undefined) return
Expand Down
7 changes: 2 additions & 5 deletions packages/plugin-teach/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Context, template, defineProperty } from 'koishi-core'
import { Dialogue } from './utils'
import { create, update } from './update'
import { RegExpValidator } from 'regexpp'
import { formatQuestionAnswers } from './search'
import { distance } from 'fastest-levenshtein'

Expand Down Expand Up @@ -69,8 +68,6 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
return question.startsWith('^') || question.endsWith('$')
}

const validator = new RegExpValidator(config.validateRegExp)

ctx.before('dialogue/modify', async (argv) => {
const { options, session, target, dialogues, args } = argv
const { ignoreHint, regexp } = options
Expand Down Expand Up @@ -107,9 +104,9 @@ export default function apply(ctx: Context, config: Dialogue.Config) {

// 检测正则表达式的合法性
if (regexp || regexp !== false && question && dialogues.some(d => d.flag & Dialogue.Flag.regexp)) {
const questions = question ? [question as string] : dialogues.map(d => d.question)
const questions = question ? [question] : dialogues.map(d => d.question)
try {
questions.map(q => validator.validatePattern(q))
questions.forEach(q => new RegExp(q))
} catch (error) {
return template('teach.illegal-regexp')
}
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-teach/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Session, App, Context, Tables } from 'koishi-core'
import { difference, observe, isInteger, defineProperty, Observed, clone } from 'koishi-utils'
import { RegExpValidator } from 'regexpp'

declare module 'koishi-core' {
interface App {
Expand Down Expand Up @@ -72,7 +71,6 @@ export namespace Dialogue {
prefix?: string
authority?: AuthorityConfig
historyAge?: number
validateRegExp?: RegExpValidator.Options
}

export interface Stats {
Expand Down

0 comments on commit b3c5d73

Please sign in to comment.