Skip to content

Commit cf953e6

Browse files
committed
feat: 正規表現パターンのサポートを追加し、READMEとテストを更新
1 parent 0bc3a91 commit cf953e6

File tree

7 files changed

+116
-16
lines changed

7 files changed

+116
-16
lines changed

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ Via `.textlintrc`(Recommended)
8383
"rules": {
8484
"preset-ai-writing": {
8585
"no-ai-list-formatting": {
86-
"allows": ["許可したいテキスト"],
86+
"allows": ["許可したいテキスト", "/正規表現パターン/i"],
8787
"disableBoldListItems": false,
8888
"disableEmojiListItems": false
8989
},
9090
"no-ai-formal-expressions": {
91-
"allows": ["許可したいテキスト"]
91+
"allows": ["許可したいテキスト", "/正規表現パターン/"]
9292
}
9393
}
9494
}
@@ -98,12 +98,44 @@ Via `.textlintrc`(Recommended)
9898
### Options説明
9999

100100
#### no-ai-list-formatting
101-
- `allows`: 指定したテキストを含む場合、エラーを報告しません
101+
- `allows`: 指定したパターンにマッチする場合、エラーを報告しません
102+
- 文字列: `"許可したいテキスト"`
103+
- 正規表現: `"/パターン/フラグ"` (例: `"/重要.*/i"`)
102104
- `disableBoldListItems`: `true`にすると強調リストアイテムの検出を無効にします
103105
- `disableEmojiListItems`: `true`にすると絵文字リストアイテムの検出を無効にします
104106

105107
#### no-ai-formal-expressions
106-
- `allows`: 指定したテキストを含む場合、エラーを報告しません
108+
- `allows`: 指定したパターンにマッチする場合、エラーを報告しません
109+
- 文字列: `"許可したいテキスト"`
110+
- 正規表現: `"/パターン/フラグ"` (例: `"/以下のような.*/"`)
111+
112+
### 正規表現パターンの使用例
113+
114+
`allows`オプションでは、[regexp-string-matcher](https://github.com/textlint/regexp-string-matcher)の形式で正規表現パターンを指定できます。
115+
116+
#### 基本的な使い方
117+
```json
118+
{
119+
"allows": [
120+
"特定の文字列", // 完全一致
121+
"/パターン/", // 正規表現(基本)
122+
"/パターン/i", // 大文字小文字を無視
123+
"/パターン/m" // 複数行マッチ
124+
]
125+
}
126+
```
127+
128+
#### 実用例
129+
```json
130+
{
131+
"allows": [
132+
"/重要.*/", // 「重要」で始まる任意の文字列
133+
"/\\*\\*注意\\*\\*/", // **注意** (特殊文字をエスケープ)
134+
"/TODO.*/i", // TODO で始まる文字列(大文字小文字無視)
135+
"/\\d{4}-\\d{2}-\\d{2}/" // 日付形式 (YYYY-MM-DD)
136+
]
137+
}
138+
```
107139

108140
Via CLI
109141

package-lock.json

Lines changed: 35 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "textlintプリセット:AIっぽい記述パターンを検出し、より自然な日本語表現を促すルール集",
55
"keywords": [
66
"textlintrule",
7-
"textlint-rule-preset",
7+
"textlint-rule-preset",
88
"japanese",
99
"ai-writing",
1010
"natural-writing",
@@ -62,5 +62,8 @@
6262
"packageManager": "yarn@1.22.21",
6363
"publishConfig": {
6464
"access": "public"
65+
},
66+
"dependencies": {
67+
"@textlint/regexp-string-matcher": "^2.0.2"
6568
}
6669
}

src/rules/no-ai-formal-expressions.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { TextlintRuleModule } from "@textlint/types";
2+
import { matchPatterns } from "@textlint/regexp-string-matcher";
23

34
export interface Options {
4-
// If node's text includes allowed text, does not report.
5+
// If node's text includes allowed patterns, does not report.
6+
// Can be string or RegExp-like string ("/pattern/flags")
57
allows?: string[];
68
}
79

@@ -23,8 +25,12 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
2325
[Syntax.Str](node) {
2426
const text = getSource(node);
2527

26-
if (allows.some((allow) => text.includes(allow))) {
27-
return;
28+
// Check if text matches any allowed patterns
29+
if (allows.length > 0) {
30+
const matches = matchPatterns(text, allows);
31+
if (matches.length > 0) {
32+
return;
33+
}
2834
}
2935

3036
for (const pattern of formalPatterns) {

src/rules/no-ai-list-formatting.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { TextlintRuleModule } from "@textlint/types";
2+
import { matchPatterns } from "@textlint/regexp-string-matcher";
23

34
export interface Options {
4-
// If node's text includes allowed text, does not report.
5+
// If node's text includes allowed patterns, does not report.
6+
// Can be string or RegExp-like string ("/pattern/flags")
57
allows?: string[];
68
// Disable specific pattern checks
79
disableBoldListItems?: boolean;
@@ -42,8 +44,12 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
4244
[Syntax.ListItem](node) {
4345
const text = getSource(node);
4446

45-
if (allows.some((allow) => text.includes(allow))) {
46-
return;
47+
// Check if text matches any allowed patterns
48+
if (allows.length > 0) {
49+
const matches = matchPatterns(text, allows);
50+
if (matches.length > 0) {
51+
return;
52+
}
4753
}
4854

4955
// Check for bold list item pattern: - **text**: description

test/rules/no-ai-formal-expressions.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ tester.run("no-ai-formal-expressions", noAiFormalExpressions, {
88
// Normal text
99
"これは通常の文章です。",
1010
"問題のない表現を使用しています。",
11-
// Allowed patterns
11+
// Allowed patterns (string)
1212
{
1313
text: "以下のような形で進めます。",
1414
options: {
1515
allows: ["以下のような形で"]
1616
}
17+
},
18+
// Allowed patterns (RegExp-like string)
19+
{
20+
text: "次のような点について説明します。",
21+
options: {
22+
allows: ["/次のような.*/"]
23+
}
1724
}
1825
],
1926
invalid: [

test/rules/no-ai-list-formatting.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@ tester.run("no-ai-list-formatting", noAiListFormatting, {
1010
"- これは問題ない記述です",
1111
// Bold text not in list format
1212
"**強調テキスト**は問題ありません",
13-
// Allowed patterns
13+
// Allowed patterns (string)
1414
{
1515
text: "- **許可された**: 説明",
1616
options: {
1717
allows: ["許可された"]
1818
}
1919
},
20+
// Allowed patterns (RegExp-like string)
21+
{
22+
text: "- **重要事項**: これは重要な説明です",
23+
options: {
24+
allows: ["/重要.*/"]
25+
}
26+
},
27+
// Allowed patterns (case insensitive)
28+
{
29+
text: "- **IMPORTANT**: This is important",
30+
options: {
31+
allows: ["/important/i"]
32+
}
33+
},
2034
// Disabled bold list items
2135
{
2236
text: "- **強調**: 説明",

0 commit comments

Comments
 (0)