|
1 |
| -/** |
2 |
| - * @param {RuleContext} context |
3 |
| - */ |
| 1 | +const toString = require('mdast-util-to-string'); |
| 2 | + |
4 | 3 | module.exports = context => {
|
5 | 4 | return {
|
| 5 | + // [context.Syntax.Header]: node => {}, |
| 6 | + |
| 7 | + [context.Syntax.Paragraph]: node => { |
| 8 | + // Checks against paragraph-based rules. |
| 9 | + |
| 10 | + // Convert a paragraph to plain text, stripping any markups |
| 11 | + const text = toString(node); |
| 12 | + |
| 13 | + enforceZenHanSpace(node, text, context); |
| 14 | + noHanPunctSpace(node, text, context); |
| 15 | + enforceQuestionSpace(node, text, context); |
| 16 | + noLineEndSpace(node, text, context); |
| 17 | + }, |
| 18 | + |
6 | 19 | [context.Syntax.Str]: node => {
|
7 |
| - const text = context.getSource(node); |
8 |
| - const match = /下さい/.exec(text); |
9 |
| - if (match) { |
| 20 | + const text = toString(node); |
| 21 | + const index = text.indexOf(' '); |
| 22 | + if (index >= 0) { |
10 | 23 | context.report(
|
11 | 24 | node,
|
12 | 25 | new context.RuleError(
|
13 |
| - `「下さい」の代わりに「ください」を使ってください。`, |
| 26 | + 'いかなる場合も全角スペースは使用しないでください。', |
| 27 | + {index}, |
14 | 28 | ),
|
15 | 29 | );
|
16 | 30 | }
|
17 | 31 | },
|
18 | 32 | };
|
19 | 33 | };
|
| 34 | + |
| 35 | +const enforceQuestionSpace = (node, text, context) => { |
| 36 | + const markReg = /[!?](\u3000|[A-Za-z0-9]|[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/; |
| 37 | + if (markReg.exec(text)) { |
| 38 | + context.report( |
| 39 | + node, |
| 40 | + new context.RuleError( |
| 41 | + '全角の「!」「?」と次の文の間には半角スペースを挿入してください。', |
| 42 | + ), |
| 43 | + ); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +const noHanPunctSpace = (node, text, context) => { |
| 48 | + if (/[、。]( |\u3000)/.exec(text)) { |
| 49 | + context.report( |
| 50 | + node, |
| 51 | + new context.RuleError( |
| 52 | + '全角の句読点の直後にスペースを入れてはいけません。', |
| 53 | + ), |
| 54 | + ); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +const enforceZenHanSpace = (node, text, context) => { |
| 59 | + const hanZen = /[A-Za-z0-9](?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/; |
| 60 | + const zenHan = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])[A-Za-z0-9]/; |
| 61 | + |
| 62 | + if (hanZen.exec(text) || zenHan.exec(text)) { |
| 63 | + context.report( |
| 64 | + node, |
| 65 | + new context.RuleError( |
| 66 | + '全角文字と半角英数字とが隣接しています。半角スペースを挿入してください。', |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +const noLineEndSpace = (node, text, context) => { |
| 73 | + // This detects a line-end space *only when* the line contains Japanese characters. |
| 74 | + const reg = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ]).?(\s|\u3000)$/; |
| 75 | + |
| 76 | + if (reg.exec(text)) { |
| 77 | + context.report( |
| 78 | + node, |
| 79 | + new context.RuleError('行末にスペース文字を入れないでください。'), |
| 80 | + ); |
| 81 | + } |
| 82 | +}; |
0 commit comments