Skip to content

Commit d351813

Browse files
committed
Fix invalid range
Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
1 parent 9213834 commit d351813

File tree

1 file changed

+28
-16
lines changed
  • public/js/lib/editor/markdown-lint

1 file changed

+28
-16
lines changed

public/js/lib/editor/markdown-lint/index.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ require('script-loader!markdownlint');
3535
to: CodeMirror.Pos(lineNumber, end),
3636
__ruleNames: ruleNames,
3737
__ruleDescription: ruleDescription,
38-
__error: error,
39-
__lineNumber: lineNumber
38+
__error: error
4039
}
4140
})
4241
}
@@ -55,34 +54,30 @@ export const linterOptions = {
5554
content: `Fix ${error.ruleDescription}`,
5655
onClick () {
5756
const doc = window.editor.doc
58-
const fixInfo = error.fixInfo
57+
const fixInfo = normalizeFixInfo(error.fixInfo, error.lineNumber)
5958
const line = fixInfo.lineNumber - 1
6059
const lineContent = doc.getLine(line) || ''
61-
const fixedText = helpers.applyFix(lineContent, error.fixInfo, '\n')
60+
const fixedText = helpers.applyFix(lineContent, fixInfo, '\n')
6261

6362
let from = { line, ch: 0 }
64-
let to = { line, ch: lineContent ? lineContent.length - 1 : 0 }
63+
let to = { line, ch: lineContent ? lineContent.length : 0 }
6564

6665
if (typeof fixedText === 'string') {
6766
doc.replaceRange(fixedText, from, to)
6867
} else {
6968
if (fixInfo.lineNumber === 1) {
70-
if (document.lineCount > 1) {
71-
const nextLine = doc.getLine(to.line + 1) || ''
72-
to = {
73-
line: nextLine,
69+
if (doc.lineCount() > 1) {
70+
const nextLineStart = doc.indexFromPos({
71+
line: to.line + 1,
7472
ch: 0
75-
}
73+
})
74+
to = doc.posFromIndex(nextLineStart)
7675
}
7776
} else {
78-
const previousLine = doc.getLine(from.line - 1) || ''
79-
from = {
80-
line: previousLine,
81-
ch: previousLine.length
82-
}
77+
const previousLineEnd = doc.indexFromPos(from) - 1
78+
from = doc.posFromIndex(previousLineEnd)
8379
}
8480

85-
// !FIXME: certain range out of bound
8681
doc.replaceRange('', from, to)
8782
}
8883
}
@@ -102,3 +97,20 @@ function lint (content) {
10297
})
10398
return errors
10499
}
100+
101+
// Taken from https://github.com/DavidAnson/markdownlint/blob/2a9274ece586514ba3e2819cec3eb74312dc1b84/helpers/helpers.js#L611
102+
/**
103+
* Normalizes the fields of a RuleOnErrorFixInfo instance.
104+
*
105+
* @param {Object} fixInfo RuleOnErrorFixInfo instance.
106+
* @param {number} [lineNumber] Line number.
107+
* @returns {Object} Normalized RuleOnErrorFixInfo instance.
108+
*/
109+
function normalizeFixInfo (fixInfo, lineNumber) {
110+
return {
111+
lineNumber: fixInfo.lineNumber || lineNumber,
112+
editColumn: fixInfo.editColumn || 1,
113+
deleteCount: fixInfo.deleteCount || 0,
114+
insertText: fixInfo.insertText || ''
115+
}
116+
}

0 commit comments

Comments
 (0)