Skip to content

Commit

Permalink
Fix invalid range
Browse files Browse the repository at this point in the history
Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
  • Loading branch information
Yukaii committed Jan 25, 2021
1 parent 9213834 commit d351813
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions public/js/lib/editor/markdown-lint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ require('script-loader!markdownlint');
to: CodeMirror.Pos(lineNumber, end),
__ruleNames: ruleNames,
__ruleDescription: ruleDescription,
__error: error,
__lineNumber: lineNumber
__error: error
}
})
}
Expand All @@ -55,34 +54,30 @@ export const linterOptions = {
content: `Fix ${error.ruleDescription}`,
onClick () {
const doc = window.editor.doc
const fixInfo = error.fixInfo
const fixInfo = normalizeFixInfo(error.fixInfo, error.lineNumber)
const line = fixInfo.lineNumber - 1
const lineContent = doc.getLine(line) || ''
const fixedText = helpers.applyFix(lineContent, error.fixInfo, '\n')
const fixedText = helpers.applyFix(lineContent, fixInfo, '\n')

let from = { line, ch: 0 }
let to = { line, ch: lineContent ? lineContent.length - 1 : 0 }
let to = { line, ch: lineContent ? lineContent.length : 0 }

if (typeof fixedText === 'string') {
doc.replaceRange(fixedText, from, to)
} else {
if (fixInfo.lineNumber === 1) {
if (document.lineCount > 1) {
const nextLine = doc.getLine(to.line + 1) || ''
to = {
line: nextLine,
if (doc.lineCount() > 1) {
const nextLineStart = doc.indexFromPos({
line: to.line + 1,
ch: 0
}
})
to = doc.posFromIndex(nextLineStart)
}
} else {
const previousLine = doc.getLine(from.line - 1) || ''
from = {
line: previousLine,
ch: previousLine.length
}
const previousLineEnd = doc.indexFromPos(from) - 1
from = doc.posFromIndex(previousLineEnd)
}

// !FIXME: certain range out of bound
doc.replaceRange('', from, to)
}
}
Expand All @@ -102,3 +97,20 @@ function lint (content) {
})
return errors
}

// Taken from https://github.com/DavidAnson/markdownlint/blob/2a9274ece586514ba3e2819cec3eb74312dc1b84/helpers/helpers.js#L611
/**
* Normalizes the fields of a RuleOnErrorFixInfo instance.
*
* @param {Object} fixInfo RuleOnErrorFixInfo instance.
* @param {number} [lineNumber] Line number.
* @returns {Object} Normalized RuleOnErrorFixInfo instance.
*/
function normalizeFixInfo (fixInfo, lineNumber) {
return {
lineNumber: fixInfo.lineNumber || lineNumber,
editColumn: fixInfo.editColumn || 1,
deleteCount: fixInfo.deleteCount || 0,
insertText: fixInfo.insertText || ''
}
}

0 comments on commit d351813

Please sign in to comment.