|
| 1 | +import type { TextlintRuleModule } from "@textlint/types"; |
| 2 | +import { matchPatterns } from "@textlint/regexp-string-matcher"; |
| 3 | + |
| 4 | +export interface Options { |
| 5 | + // 指定したパターンにマッチする場合、エラーを報告しません |
| 6 | + // 文字列または正規表現パターン ("/pattern/flags") で指定可能 |
| 7 | + allows?: string[]; |
| 8 | + // 絵文字と太字の組み合わせパターンの検出を無効にする |
| 9 | + disableEmojiEmphasisPatterns?: boolean; |
| 10 | + // 情報系プレフィックスパターンの検出を無効にする |
| 11 | + disableInfoPatterns?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +const rule: TextlintRuleModule<Options> = (context, options = {}) => { |
| 15 | + const { Syntax, RuleError, report, getSource, locator } = context; |
| 16 | + const allows = options.allows ?? []; |
| 17 | + const disableEmojiEmphasisPatterns = options.disableEmojiEmphasisPatterns ?? false; |
| 18 | + const disableInfoPatterns = options.disableInfoPatterns ?? false; |
| 19 | + |
| 20 | + // 機械的な情報系プレフィックス |
| 21 | + const infoPatterns = [ |
| 22 | + "注意", |
| 23 | + "重要", |
| 24 | + "ポイント", |
| 25 | + "メモ", |
| 26 | + "参考", |
| 27 | + "補足", |
| 28 | + "確認", |
| 29 | + "チェック", |
| 30 | + "推奨", |
| 31 | + "おすすめ", |
| 32 | + "検出される例", |
| 33 | + "推奨される表現", |
| 34 | + "良い例", |
| 35 | + "悪い例", |
| 36 | + "例", |
| 37 | + "サンプル", |
| 38 | + "使用例", |
| 39 | + "設定例" |
| 40 | + ]; |
| 41 | + |
| 42 | + return { |
| 43 | + [Syntax.Paragraph](node) { |
| 44 | + const text = getSource(node); |
| 45 | + |
| 46 | + // allowsパターンにマッチする場合はスキップ |
| 47 | + if (allows.length > 0) { |
| 48 | + const matchedResults = matchPatterns(text, allows); |
| 49 | + if (matchedResults.length > 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // リストアイテムの場合はListItemのハンドラーで処理するのでスキップ |
| 55 | + if (node.parent?.type === "ListItem") { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + let emojiEmphasizeMatches: RegExpExecArray[] = []; |
| 60 | + |
| 61 | + // 絵文字 + 太字の組み合わせパターンを検出 |
| 62 | + if (!disableEmojiEmphasisPatterns) { |
| 63 | + // 絵文字の正規表現を修正(サロゲートペア対応) |
| 64 | + const emojiEmphasizePattern = /([ℹ️🔍✅❌⚠️💡📝📋📌🔗🎯🚀⭐✨💯🔥📊📈])\s*\*\*([^*]+)\*\*/g; |
| 65 | + |
| 66 | + let match; |
| 67 | + while ((match = emojiEmphasizePattern.exec(text)) !== null) { |
| 68 | + const matchStart = match.index; |
| 69 | + const matchEnd = match.index + match[0].length; |
| 70 | + |
| 71 | + emojiEmphasizeMatches.push(match); |
| 72 | + |
| 73 | + report( |
| 74 | + node, |
| 75 | + new RuleError( |
| 76 | + `絵文字と太字の組み合わせは機械的な印象を与える可能性があります。より自然な表現を検討してください。`, |
| 77 | + { |
| 78 | + padding: locator.range([matchStart, matchEnd]) |
| 79 | + } |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // 情報系プレフィックスの太字パターンを検出(絵文字+太字と重複しない場合のみ) |
| 86 | + if (!disableInfoPatterns) { |
| 87 | + const infoPrefixPattern = new RegExp(`\\*\\*(${infoPatterns.join("|")})([::].*?)?\\*\\*`, "g"); |
| 88 | + |
| 89 | + let match; |
| 90 | + while ((match = infoPrefixPattern.exec(text)) !== null) { |
| 91 | + const matchStart = match.index; |
| 92 | + const matchEnd = match.index + match[0].length; |
| 93 | + const prefixText = match[1]; |
| 94 | + |
| 95 | + // 絵文字+太字のマッチと重複していないかチェック |
| 96 | + const isOverlapping = emojiEmphasizeMatches.some((emojiMatch) => { |
| 97 | + const emojiStart = emojiMatch.index!; |
| 98 | + const emojiEnd = emojiMatch.index! + emojiMatch[0].length; |
| 99 | + return matchStart < emojiEnd && matchEnd > emojiStart; |
| 100 | + }); |
| 101 | + |
| 102 | + if (!isOverlapping) { |
| 103 | + report( |
| 104 | + node, |
| 105 | + new RuleError( |
| 106 | + `「**${prefixText}**」のような太字の情報プレフィックスは機械的な印象を与える可能性があります。より自然な表現を検討してください。`, |
| 107 | + { |
| 108 | + padding: locator.range([matchStart, matchEnd]) |
| 109 | + } |
| 110 | + ) |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + |
| 117 | + [Syntax.ListItem](node) { |
| 118 | + const text = getSource(node); |
| 119 | + |
| 120 | + // allowsパターンにマッチする場合はスキップ |
| 121 | + if (allows.length > 0) { |
| 122 | + const matchedResults = matchPatterns(text, allows); |
| 123 | + if (matchedResults.length > 0) { |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + let emojiEmphasizeMatches: RegExpExecArray[] = []; |
| 129 | + |
| 130 | + // リストアイテム内での絵文字 + 太字パターンを検出 |
| 131 | + if (!disableEmojiEmphasisPatterns) { |
| 132 | + const emojiEmphasizePattern = /([ℹ️🔍✅❌⚠️💡📝📋📌🔗🎯🚀⭐✨💯🔥📊📈])\s*\*\*([^*]+)\*\*/g; |
| 133 | + |
| 134 | + let match; |
| 135 | + while ((match = emojiEmphasizePattern.exec(text)) !== null) { |
| 136 | + const matchStart = match.index; |
| 137 | + const matchEnd = match.index + match[0].length; |
| 138 | + |
| 139 | + emojiEmphasizeMatches.push(match); |
| 140 | + |
| 141 | + report( |
| 142 | + node, |
| 143 | + new RuleError( |
| 144 | + `リストアイテムで絵文字と太字の組み合わせは機械的な印象を与える可能性があります。より自然な表現を検討してください。`, |
| 145 | + { |
| 146 | + padding: locator.range([matchStart, matchEnd]) |
| 147 | + } |
| 148 | + ) |
| 149 | + ); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // リストアイテム内での情報系プレフィックスの太字パターンを検出 |
| 154 | + if (!disableInfoPatterns) { |
| 155 | + const infoPrefixPattern = new RegExp(`\\*\\*(${infoPatterns.join("|")})([::].*?)?\\*\\*`, "g"); |
| 156 | + |
| 157 | + let match; |
| 158 | + while ((match = infoPrefixPattern.exec(text)) !== null) { |
| 159 | + const matchStart = match.index; |
| 160 | + const matchEnd = match.index + match[0].length; |
| 161 | + const prefixText = match[1]; |
| 162 | + |
| 163 | + // 絵文字+太字のマッチと重複していないかチェック |
| 164 | + const isOverlapping = emojiEmphasizeMatches.some((emojiMatch) => { |
| 165 | + const emojiStart = emojiMatch.index!; |
| 166 | + const emojiEnd = emojiMatch.index! + emojiMatch[0].length; |
| 167 | + return matchStart < emojiEnd && matchEnd > emojiStart; |
| 168 | + }); |
| 169 | + |
| 170 | + if (!isOverlapping) { |
| 171 | + report( |
| 172 | + node, |
| 173 | + new RuleError( |
| 174 | + `リストアイテムで「**${prefixText}**」のような太字の情報プレフィックスは機械的な印象を与える可能性があります。より自然な表現を検討してください。`, |
| 175 | + { |
| 176 | + padding: locator.range([matchStart, matchEnd]) |
| 177 | + } |
| 178 | + ) |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + }; |
| 185 | +}; |
| 186 | + |
| 187 | +export default rule; |
0 commit comments