Skip to content

Commit 471ef7e

Browse files
authored
fix(rule): Fix to ignore link's title (#7)
1 parent af40eea commit 471ef7e

File tree

4 files changed

+2720
-21
lines changed

4 files changed

+2720
-21
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434
"dependencies": {
3535
"mdast-util-to-string": "^1.0.0",
3636
"sentence-splitter": "^2.0.0",
37-
"textlint-rule-helper": "^2.0.0"
37+
"textlint-rule-helper": "^2.0.0",
38+
"textlint-util-to-string": "^2.1.1"
3839
},
3940
"devDependencies": {
4041
"babel-cli": "^6.1.18",
4142
"babel-plugin-add-module-exports": "^0.2.1",
4243
"babel-preset-es2015": "^6.1.18",
4344
"espower-babel": "^4.0.0",
44-
"mocha": "^3.2.0",
45+
"mocha": "^4.0.1",
4546
"power-assert": "^1.2.0",
4647
"textlint-tester": "^2.1.0"
4748
}

src/sentence-length.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
// LICENSE : MIT
22
"use strict";
3-
import {split} from "sentence-splitter";
4-
import toString from 'mdast-util-to-string';
5-
import {RuleHelper} from "textlint-rule-helper";
6-
const isStartWithNewLine = (text) => {
7-
return text && text.charAt(0) === "\n";
8-
};
3+
import { split } from "sentence-splitter";
4+
import StringSource from 'textlint-util-to-string';
5+
import { RuleHelper } from "textlint-rule-helper";
6+
97
const defaultOptions = {
108
max: 100
119
};
1210
export default function(context, options = {}) {
1311
const maxLength = options.max || defaultOptions.max;
1412
const helper = new RuleHelper(context);
15-
const {Syntax, RuleError, report} = context;
13+
const { Syntax, RuleError, report } = context;
1614
// toPlainText
1715
return {
18-
[Syntax.Paragraph](node){
16+
[Syntax.Paragraph](node) {
1917
if (helper.isChildNode(node, [Syntax.BlockQuote])) {
2018
return;
2119
}
@@ -24,24 +22,23 @@ export default function(context, options = {}) {
2422
if (isChildrenSingleLinkNode) {
2523
return;
2624
}
27-
const text = toString(node);
25+
const source = new StringSource(node);
26+
const text = source.toString();
2827
// empty break line == split sentence
2928
const sentences = split(text, {
3029
newLineCharacters: "\n\n"
3130
});
3231
sentences.forEach(sentence => {
3332
// TODO: should trim()?
34-
let sentenceText = sentence.value;
33+
const sentenceText = sentence.value;
3534
// larger than > 100
3635
if (sentenceText.length > maxLength) {
3736
const currentLine = node.loc.start.line;
38-
const addedLine = isStartWithNewLine(sentenceText)
39-
? sentence.loc.start.line // \n string
40-
: sentence.loc.start.line - 1; // string
41-
let paddingLine = Math.max(addedLine, 0);
42-
let paddingIndex = sentence.range[0];
43-
report(node, new RuleError(`Line ${currentLine + paddingLine} exceeds the maximum line length of ${maxLength}.`, {
44-
index: paddingIndex
37+
const start = source.originalPositionFromPosition(sentence.loc.start);
38+
const startLine = start.line - 1 + currentLine;
39+
const index = source.originalIndexFromPosition(sentence.loc.start);
40+
report(node, new RuleError(`Line ${startLine} exceeds the maximum line length of ${maxLength}.`, {
41+
index: index
4542
}));
4643
}
4744
});

test/sentence-length-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tester.run("textlint-rule-sentence-length", rule, {
2525
},
2626
{
2727
// == 12345
28-
text: "[123](http://example.com)45",
28+
text: '[123](http://example.com "123456")45',
2929
options: {
3030
max: 5
3131
}
@@ -94,7 +94,7 @@ Reduxの _Middleware_ は扱える範囲がdispatchからReducerまでと線引
9494
},
9595
errors: [
9696
{
97-
message: `Line 5 exceeds the maximum line length of 100.`
97+
message: `Line 4 exceeds the maximum line length of 100.`
9898
}
9999
]
100100
}

0 commit comments

Comments
 (0)