2019-08-18 14:09:12 +08:00
|
|
|
/* global CodeMirror */
|
2019-08-16 09:37:09 +08:00
|
|
|
|
2019-08-16 23:22:58 +08:00
|
|
|
// load CM lint plugin explicitly
|
2019-08-18 14:09:12 +08:00
|
|
|
import '@hackmd/codemirror/addon/lint/lint'
|
2019-08-16 09:37:09 +08:00
|
|
|
|
2021-01-25 18:05:35 +08:00
|
|
|
import '@hackmd/codemirror/addon/hint/show-hint.css'
|
|
|
|
import helpers from 'markdownlint-rule-helpers'
|
|
|
|
|
2019-11-01 16:47:41 +08:00
|
|
|
window.markdownit = require('markdown-it')
|
|
|
|
// eslint-disable-next-line
|
|
|
|
require('script-loader!markdownlint');
|
|
|
|
|
2019-08-18 14:09:12 +08:00
|
|
|
(function (mod) {
|
|
|
|
mod(CodeMirror)
|
|
|
|
})(function (CodeMirror) {
|
|
|
|
function validator (text) {
|
2019-08-16 09:37:09 +08:00
|
|
|
return lint(text).map(error => {
|
2019-08-16 23:27:19 +08:00
|
|
|
const {
|
|
|
|
ruleNames,
|
|
|
|
ruleDescription,
|
|
|
|
lineNumber: ln,
|
|
|
|
errorRange
|
|
|
|
} = error
|
|
|
|
const lineNumber = ln - 1
|
2019-08-16 09:37:09 +08:00
|
|
|
|
2019-08-18 14:09:12 +08:00
|
|
|
let start = 0; let end = -1
|
2019-08-16 23:27:19 +08:00
|
|
|
if (errorRange) {
|
|
|
|
[start, end] = errorRange.map(r => r - 1)
|
2019-08-16 09:37:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-01-26 12:52:48 +08:00
|
|
|
messageHTML: `${ruleNames.join('/')}: ${ruleDescription} <small>markdownlint(${ruleNames[0]})</small>`,
|
2019-08-16 09:37:09 +08:00
|
|
|
severity: 'error',
|
|
|
|
from: CodeMirror.Pos(lineNumber, start),
|
2021-01-25 18:05:35 +08:00
|
|
|
to: CodeMirror.Pos(lineNumber, end),
|
2021-01-26 01:52:25 +08:00
|
|
|
__error: error
|
2019-08-16 09:37:09 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-16 23:27:19 +08:00
|
|
|
CodeMirror.registerHelper('lint', 'markdown', validator)
|
|
|
|
})
|
2019-08-16 09:37:09 +08:00
|
|
|
|
2021-01-25 18:05:35 +08:00
|
|
|
export const linterOptions = {
|
|
|
|
fixedTooltip: true,
|
|
|
|
contextmenu: annotations => {
|
|
|
|
const singleFixMenus = annotations
|
|
|
|
.map(annotation => {
|
|
|
|
const error = annotation.__error
|
2021-01-26 12:52:48 +08:00
|
|
|
const ruleNameAlias = error.ruleNames.join('/')
|
2021-01-25 18:05:35 +08:00
|
|
|
|
2021-01-26 12:52:48 +08:00
|
|
|
if (annotation.__error.fixInfo) {
|
|
|
|
return {
|
|
|
|
content: `Click to fix this violoation of ${ruleNameAlias}`,
|
|
|
|
onClick () {
|
|
|
|
const doc = window.editor.doc
|
|
|
|
const fixInfo = normalizeFixInfo(error.fixInfo, error.lineNumber)
|
|
|
|
const line = fixInfo.lineNumber - 1
|
|
|
|
const lineContent = doc.getLine(line) || ''
|
|
|
|
const fixedText = helpers.applyFix(lineContent, fixInfo, '\n')
|
2021-01-25 18:05:35 +08:00
|
|
|
|
2021-01-26 12:52:48 +08:00
|
|
|
let from = { line, ch: 0 }
|
|
|
|
let to = { line, ch: lineContent ? lineContent.length : 0 }
|
|
|
|
|
|
|
|
if (typeof fixedText === 'string') {
|
|
|
|
doc.replaceRange(fixedText, from, to)
|
2021-01-25 18:05:35 +08:00
|
|
|
} else {
|
2021-01-26 12:52:48 +08:00
|
|
|
if (fixInfo.lineNumber === 1) {
|
|
|
|
if (doc.lineCount() > 1) {
|
|
|
|
const nextLineStart = doc.indexFromPos({
|
|
|
|
line: to.line + 1,
|
|
|
|
ch: 0
|
|
|
|
})
|
|
|
|
to = doc.posFromIndex(nextLineStart)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const previousLineEnd = doc.indexFromPos(from) - 1
|
|
|
|
from = doc.posFromIndex(previousLineEnd)
|
|
|
|
}
|
2021-01-25 18:05:35 +08:00
|
|
|
|
2021-01-26 12:52:48 +08:00
|
|
|
doc.replaceRange('', from, to)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
content: `Click for more information about ${ruleNameAlias}`,
|
|
|
|
onClick () {
|
|
|
|
window.open(error.ruleInformation, '_blank')
|
2021-01-25 18:05:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return singleFixMenus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:37:09 +08:00
|
|
|
function lint (content) {
|
2019-11-01 16:33:58 +08:00
|
|
|
const { content: errors } = window.markdownlint.sync({
|
2019-08-16 09:37:09 +08:00
|
|
|
strings: {
|
|
|
|
content
|
2021-01-25 18:05:35 +08:00
|
|
|
},
|
|
|
|
resultVersion: 3
|
2019-08-16 09:37:09 +08:00
|
|
|
})
|
|
|
|
return errors
|
|
|
|
}
|
2021-01-26 01:52:25 +08:00
|
|
|
|
|
|
|
// 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 || ''
|
|
|
|
}
|
|
|
|
}
|