Skip to content

Commit

Permalink
feat: add skipPatterns options (#17)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: skip to count of url string link by default

- Deprecated `exclusionPatterns` and use `skipPatterns` instead of it
- Add `skipUrlStringLink` and Enable it by default
  • Loading branch information
azu authored May 9, 2021
1 parent cd13e03 commit 6ece8cd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ Add "sentence-length" to your `.textlintrc`.
- default: 100
- The total number of characters allowed on each sentences.
- Sentence.length > 100 and throw Error
- `exclusionPatterns`: `string[]`
- `skipPatterns`: `string[]`
- A strings that match the patterns is uncount of the sentence.
- Set an array of RegExp-like string.
- See https://github.com/textlint/regexp-string-matcher
- `skipUrlStringLink`: `boolean`
- Default: `true`
- If it is `true`, skip url string link node like `<https:example.com>` or [https://example.com](https://example.com)
- url string link is has the text which is same of url.

```
{
Expand All @@ -47,7 +51,7 @@ Uncount `(...)` from `A sentence(...).`
"rules": {
"sentence-length": {
"max": 100,
"exclusionPatterns": [
"skipPattern": [
"/\\(.*\\)$\\./"
]
Expand Down
53 changes: 46 additions & 7 deletions src/sentence-length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StringSource } from "textlint-util-to-string";
import { RuleHelper } from "textlint-rule-helper";
import { createRegExp } from "@textlint/regexp-string-matcher";
import { TextlintRuleReporter } from "@textlint/types";
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types";

function removeRangeFromString(text: string, regExpStrings: string[]) {
const patterns = regExpStrings.map((pattern) => {
Expand All @@ -17,20 +18,50 @@ function removeRangeFromString(text: string, regExpStrings: string[]) {

export type Options = {
max?: number;
/**
* The strings that match following patterns is un-count of the sentence
* See https://github.com/textlint/regexp-string-matcher
*/
skipPatterns?: string[];
/**
* If it is true, skip the count of following link node.
*
* [https://example.com](https://example.com)
* <https://example.com>
*
* UrlStringLink is has the title which is same of href.
*/
skipUrlStringLink?: boolean;
/**
* @deprecated use skipPatterns
*/
exclusionPatterns?: string[];
};
const defaultOptions: Required<Options> = {
max: 100,
// The strings that match following patterns is uncount of the sentence
// See https://github.com/textlint/regexp-string-matcher
skipPatterns: [],
skipUrlStringLink: true,
/**
* @deprecated
*/
exclusionPatterns: []
};

const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
const maxLength = options.max ?? defaultOptions.max;
const exclusionPatterns = options.exclusionPatterns ?? defaultOptions.exclusionPatterns;
const skipPattern = options.skipPatterns ?? options.exclusionPatterns ?? defaultOptions.skipPatterns;
const skipUrlStringLink = options.skipUrlStringLink ?? defaultOptions.skipUrlStringLink;
const helper = new RuleHelper(context);
const { Syntax, RuleError, report } = context;
const isUrlStringLink = (node: TxtNode | TxtParentNode): boolean => {
if (node.type !== Syntax.Link) {
return false;
}
const linkNode = node as TxtParentNode;
const nodeText = new StringSource(linkNode).toString();
return node.url === nodeText;
};

// toPlainText
return {
[Syntax.Paragraph](node) {
Expand All @@ -47,17 +78,25 @@ const reporter: TextlintRuleReporter<Options> = (context, options = {}) => {
paragraph.children
.filter((sentence) => sentence.type === SentenceSyntax.Sentence)
.forEach((sentence) => {
const filteredSentence = skipUrlStringLink
? {
...sentence,
children: sentence.children.filter((sentenceChildNode: TxtNode | TxtParentNode) => {
return !isUrlStringLink(sentenceChildNode);
})
}
: sentence;
// @ts-expect-error: wrong types
const source = new StringSource(sentence);
const source = new StringSource(filteredSentence);
const actualText = source.toString();
const sentenceText = removeRangeFromString(actualText, exclusionPatterns);
const sentenceText = removeRangeFromString(actualText, skipPattern);
// larger than > 100
const actualTextLength = actualText.length;
const sentenceLength = sentenceText.length;
if (sentenceLength > maxLength) {
const startLine = sentence.loc.start.line;
const startLine = filteredSentence.loc.start.line;
report(
sentence,
filteredSentence,
new RuleError(`Line ${startLine} sentence length(${
sentenceLength !== actualTextLength
? `${sentenceLength}, original:${actualTextLength}`
Expand Down
30 changes: 28 additions & 2 deletions test/sentence-length-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import TextLintTester from "textlint-tester";
import rule from "../src/sentence-length";
// @ts-expect-error: no types
import htmlPlugin from "textlint-plugin-html";

const tester = new TextLintTester();

tester.run("textlint-rule-sentence-length", rule, {
Expand Down Expand Up @@ -42,7 +43,7 @@ tester.run("textlint-rule-sentence-length", rule, {
text: "1234(56789)",
options: {
max: 5,
exclusionPatterns: ["/\\(.*\\)$/"]
skipPatterns: ["/\\(.*\\)$/"]
}
},
{
Expand All @@ -53,6 +54,13 @@ tester.run("textlint-rule-sentence-length", rule, {
max: 5
}
},
{
// url string link
text: "[http://example.com](http://example.com)",
options: {
max: 5
}
},
{
// List
text: '- [abc](http://example.com "abc")de',
Expand Down Expand Up @@ -202,7 +210,7 @@ Over 2 characters.`,
text: "123456789(56789)",
options: {
max: 5,
exclusionPatterns: ["/\\(.*\\)$/"]
skipPatterns: ["/\\(.*\\)$/"]
},
errors: [
{
Expand All @@ -211,6 +219,20 @@ Over 2 characters.`,
"Over 4 characters."
}
]
},
{
// url string link
text: "TEST [http://example.com](http://example.com)",
errors: [
{
message: `Line 1 sentence length(23) exceeds the maximum sentence length of 5.
Over 18 characters.`
}
],
options: {
max: 5,
skipUrlStringLink: false
}
}
]
});
Expand Down Expand Up @@ -239,6 +261,10 @@ tester.run(
{
text: "<p>this is a test.</p>",
ext: ".html"
},
{
text: "<p>TEST is <a href='https://example.com'>https://example.com</a></p>",
ext: ".html"
}
],
invalid: [
Expand Down
10 changes: 10 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"include": [
"**/*",
"../src/**/*"
]
}

0 comments on commit 6ece8cd

Please sign in to comment.