1
1
// LICENSE : MIT
2
2
"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
+
9
7
const defaultOptions = {
10
8
max : 100
11
9
} ;
12
10
export default function ( context , options = { } ) {
13
11
const maxLength = options . max || defaultOptions . max ;
14
12
const helper = new RuleHelper ( context ) ;
15
- const { Syntax, RuleError, report} = context ;
13
+ const { Syntax, RuleError, report } = context ;
16
14
// toPlainText
17
15
return {
18
- [ Syntax . Paragraph ] ( node ) {
16
+ [ Syntax . Paragraph ] ( node ) {
19
17
if ( helper . isChildNode ( node , [ Syntax . BlockQuote ] ) ) {
20
18
return ;
21
19
}
@@ -24,24 +22,23 @@ export default function(context, options = {}) {
24
22
if ( isChildrenSingleLinkNode ) {
25
23
return ;
26
24
}
27
- const text = toString ( node ) ;
25
+ const source = new StringSource ( node ) ;
26
+ const text = source . toString ( ) ;
28
27
// empty break line == split sentence
29
28
const sentences = split ( text , {
30
29
newLineCharacters : "\n\n"
31
30
} ) ;
32
31
sentences . forEach ( sentence => {
33
32
// TODO: should trim()?
34
- let sentenceText = sentence . value ;
33
+ const sentenceText = sentence . value ;
35
34
// larger than > 100
36
35
if ( sentenceText . length > maxLength ) {
37
36
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
45
42
} ) ) ;
46
43
}
47
44
} ) ;
0 commit comments