Skip to content

Commit 8a7ab5a

Browse files
committed
feat(options): support exclusionPatterns options
1 parent 001d0a3 commit 8a7ab5a

File tree

5 files changed

+123
-8
lines changed

5 files changed

+123
-8
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Add "sentence-length" to your `.textlintrc`.
2525
- default: 100
2626
- The total number of characters allowed on each sentences.
2727
- Sentence.length > 100 and throw Error
28+
- `exclusionPatterns`: `string[]`
29+
- A strings that match the patterns is uncount of the sentence.
30+
- Set an array of RegExp-like string.
31+
- See https://github.com/textlint/regexp-string-matcher
2832

2933
```
3034
{
@@ -36,6 +40,22 @@ Add "sentence-length" to your `.textlintrc`.
3640
}
3741
```
3842

43+
Uncount `(...)` from `A sentence(...).`
44+
45+
```
46+
{
47+
"rules": {
48+
"sentence-length": {
49+
"max": 100,
50+
exclusionPatterns: [
51+
"/\\(.*\\)$\\./"
52+
]
53+
54+
}
55+
}
56+
}
57+
```
58+
3959
## Exception
4060

4161
- Except BlockQuote

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"prettier": "prettier --write **/*.js"
3333
},
3434
"dependencies": {
35+
"@textlint/regexp-string-matcher": "^1.1.0",
3536
"sentence-splitter": "^3.0.11",
3637
"textlint-rule-helper": "^2.1.1",
3738
"textlint-util-to-string": "^3.0.0"

src/sentence-length.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
44
import { StringSource } from "textlint-util-to-string";
55
import { RuleHelper } from "textlint-rule-helper";
6+
import { createRegExp } from "@textlint/regexp-string-matcher";
7+
8+
9+
function removeRangeFromString(string, regExpStrings) {
10+
const patterns = regExpStrings.map(pattern => {
11+
return createRegExp(pattern);
12+
});
13+
let result = string;
14+
patterns.forEach(pattern => {
15+
result = result.replace(pattern, "");
16+
});
17+
return result;
18+
}
619

720
const defaultOptions = {
8-
max: 100
21+
max: 100,
22+
// The strings that match following patterns is uncount of the sentence
23+
// See https://github.com/textlint/regexp-string-matcher
24+
exclusionPatterns: []
925
};
1026
module.exports = function(context, options = {}) {
1127
const maxLength = options.max || defaultOptions.max;
28+
const exclusionPatterns = options.exclusionPatterns || defaultOptions.exclusionPatterns;
1229
const helper = new RuleHelper(context);
1330
const { Syntax, RuleError, report } = context;
1431
// toPlainText
@@ -26,14 +43,20 @@ module.exports = function(context, options = {}) {
2643
const paragraph = splitAST(node);
2744
paragraph.children.filter(sentence => sentence.type === SentenceSyntax.Sentence).forEach(sentence => {
2845
const source = new StringSource(sentence);
29-
const sentenceText = source.toString();
46+
const actualText = source.toString();
47+
const sentenceText = removeRangeFromString(actualText, exclusionPatterns);
3048
// larger than > 100
49+
const actualTextLength = actualText.length;
3150
const sentenceLength = sentenceText.length;
3251
if (sentenceLength > maxLength) {
3352
const startLine = sentence.loc.start.line;
3453
report(
3554
sentence,
36-
new RuleError(`Line ${startLine} sentence length(${sentenceLength}) exceeds the maximum sentence length of ${maxLength}.
55+
new RuleError(`Line ${startLine} sentence length(${
56+
sentenceLength !== actualTextLength
57+
? `${sentenceLength}, original:${actualTextLength}`
58+
: sentenceLength
59+
}) exceeds the maximum sentence length of ${maxLength}.
3760
Over ${sentenceLength - maxLength} characters.`)
3861
);
3962
}

test/sentence-length-test.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ tester.run("textlint-rule-sentence-length", rule, {
1212
"This is a article",
1313
"Test`code`です。",
1414
{
15-
text: '"This" is code.',
15+
text: "\"This\" is code.",
1616
options: {
1717
max: 15
1818
}
@@ -35,22 +35,32 @@ tester.run("textlint-rule-sentence-length", rule, {
3535
},
3636
{
3737
// == 12345
38-
text: '[123](http://example.com "123456")45',
38+
text: "[123](http://example.com \"123456\")45",
3939
options: {
4040
max: 5
4141
}
4242
},
43+
{
44+
// ignore /()$/。
45+
text: "1234(56789)",
46+
options: {
47+
max: 5,
48+
exclusionPatterns: [
49+
"/\\(.*\\)$/"
50+
]
51+
}
52+
},
4353
{
4454
// html node
4555
// == 12345
46-
text: '<s>123</s><b>45</b>',
56+
text: "<s>123</s><b>45</b>",
4757
options: {
4858
max: 5
4959
}
5060
},
5161
{
5262
// List
53-
text: '- [abc](http://example.com "abc")de',
63+
text: "- [abc](http://example.com \"abc\")de",
5464
options: {
5565
max: 5
5666
}
@@ -136,7 +146,7 @@ Over 1 characters.`
136146
errors: [
137147
{
138148
message: `Line 1 sentence length(18) exceeds the maximum sentence length of 5.
139-
Over 13 characters.`,
149+
Over 13 characters.`
140150
}
141151
]
142152
},
@@ -190,6 +200,20 @@ Over 2 characters.`,
190200
column: 1
191201
}
192202
]
203+
},
204+
{
205+
// ignore /()$/。
206+
text: "123456789(56789)",
207+
options: {
208+
max: 5,
209+
exclusionPatterns: [
210+
"/\\(.*\\)$/"
211+
]
212+
},
213+
errors: [{
214+
message: "Line 1 sentence length(9, original:16) exceeds the maximum sentence length of 5.\n" +
215+
"Over 4 characters."
216+
}]
193217
}
194218
]
195219
});

yarn.lock

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,18 @@
798798
resolved "https://registry.yarnpkg.com/@textlint/module-interop/-/module-interop-1.0.1.tgz#765ca9ccb9b66657d65061d3546b3fe51e48c983"
799799
integrity sha512-gqx1Te+lMnXX6xyGTUdzGhm8RT7IfiSRMtWoH1FeTMg2InArRT+lTksCFc/x5dtaPN4vwOFZUvU8oTzYQzXbyg==
800800

801+
"@textlint/regexp-string-matcher@^1.1.0":
802+
version "1.1.0"
803+
resolved "https://registry.yarnpkg.com/@textlint/regexp-string-matcher/-/regexp-string-matcher-1.1.0.tgz#e19975029ce228a214d50c6a7e9dbcbef29ad8cd"
804+
integrity sha512-uTPnE1Dw1j+9clXPn61ZUdtg+WyhbgeXHwCTfBev7quHjeCP9PS8NdRkR6wEgmjuLg+xZlI4r/e1r6Bd0xyusQ==
805+
dependencies:
806+
escape-string-regexp "^1.0.5"
807+
execall "^1.0.0"
808+
lodash.sortby "^4.7.0"
809+
lodash.uniq "^4.5.0"
810+
lodash.uniqwith "^4.5.0"
811+
to-regex "^3.0.2"
812+
801813
"@textlint/text-to-ast@^3.1.5":
802814
version "3.1.5"
803815
resolved "https://registry.yarnpkg.com/@textlint/text-to-ast/-/text-to-ast-3.1.5.tgz#927d971712ac24d6056a0a178cc154c177427e4c"
@@ -1224,6 +1236,14 @@ cli-truncate@^0.2.1:
12241236
slice-ansi "0.0.4"
12251237
string-width "^1.0.1"
12261238

1239+
clone-regexp@^1.0.0:
1240+
version "1.0.1"
1241+
resolved "https://registry.yarnpkg.com/clone-regexp/-/clone-regexp-1.0.1.tgz#051805cd33173375d82118fc0918606da39fd60f"
1242+
integrity sha512-Fcij9IwRW27XedRIJnSOEupS7RVcXtObJXbcUOX93UCLqqOdRpkvzKywOOSizmEK/Is3S/RHX9dLdfo6R1Q1mw==
1243+
dependencies:
1244+
is-regexp "^1.0.0"
1245+
is-supported-regexp-flag "^1.0.0"
1246+
12271247
12281248
version "3.1.0"
12291249
resolved "https://registry.yarnpkg.com/co/-/co-3.1.0.tgz#4ea54ea5a08938153185e15210c68d9092bc1b78"
@@ -1587,6 +1607,13 @@ execa@^2.0.3:
15871607
signal-exit "^3.0.2"
15881608
strip-final-newline "^2.0.0"
15891609

1610+
execall@^1.0.0:
1611+
version "1.0.0"
1612+
resolved "https://registry.yarnpkg.com/execall/-/execall-1.0.0.tgz#73d0904e395b3cab0658b08d09ec25307f29bb73"
1613+
integrity sha1-c9CQTjlbPKsGWLCNCewlMH8pu3M=
1614+
dependencies:
1615+
clone-regexp "^1.0.0"
1616+
15901617
expand-brackets@^2.1.4:
15911618
version "2.1.4"
15921619
resolved "https://registry.yarnpkg.com/expand-brackets/-/expand-brackets-2.1.4.tgz#b77735e315ce30f6b6eff0f83b04151a22449622"
@@ -2281,6 +2308,11 @@ is-stream@^2.0.0:
22812308
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.0.tgz#bde9c32680d6fae04129d6ac9d921ce7815f78e3"
22822309
integrity sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==
22832310

2311+
is-supported-regexp-flag@^1.0.0:
2312+
version "1.0.1"
2313+
resolved "https://registry.yarnpkg.com/is-supported-regexp-flag/-/is-supported-regexp-flag-1.0.1.tgz#21ee16518d2c1dd3edd3e9a0d57e50207ac364ca"
2314+
integrity sha512-3vcJecUUrpgCqc/ca0aWeNu64UGgxcvO60K/Fkr1N6RSvfGCTU60UKN68JDmKokgba0rFFJs12EnzOQa14ubKQ==
2315+
22842316
is-symbol@^1.0.1:
22852317
version "1.0.1"
22862318
resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.1.tgz#3cc59f00025194b6ab2e38dbae6689256b660572"
@@ -2502,6 +2534,21 @@ locate-path@^3.0.0:
25022534
p-locate "^3.0.0"
25032535
path-exists "^3.0.0"
25042536

2537+
lodash.sortby@^4.7.0:
2538+
version "4.7.0"
2539+
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
2540+
integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=
2541+
2542+
lodash.uniq@^4.5.0:
2543+
version "4.5.0"
2544+
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
2545+
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
2546+
2547+
lodash.uniqwith@^4.5.0:
2548+
version "4.5.0"
2549+
resolved "https://registry.yarnpkg.com/lodash.uniqwith/-/lodash.uniqwith-4.5.0.tgz#7a0cbf65f43b5928625a9d4d0dc54b18cadc7ef3"
2550+
integrity sha1-egy/ZfQ7WShiWp1NDcVLGMrcfvM=
2551+
25052552
lodash@^4.0.0:
25062553
version "4.17.4"
25072554
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

0 commit comments

Comments
 (0)