2019-08-16 01:37:09 +00:00
|
|
|
import markdownlint from 'markdownlint'
|
|
|
|
|
2019-08-16 15:22:58 +00:00
|
|
|
// load CM lint plugin explicitly
|
2019-08-16 01:37:09 +00:00
|
|
|
import 'script-loader!@hackmd/codemirror/addon/lint/lint'
|
|
|
|
import './lint.css'
|
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
2019-08-16 15:27:19 +00:00
|
|
|
function validator(text) {
|
2019-08-16 01:37:09 +00:00
|
|
|
return lint(text).map(error => {
|
2019-08-16 15:27:19 +00:00
|
|
|
const {
|
|
|
|
ruleNames,
|
|
|
|
ruleDescription,
|
|
|
|
lineNumber: ln,
|
|
|
|
errorRange
|
|
|
|
} = error
|
|
|
|
const lineNumber = ln - 1
|
2019-08-16 01:37:09 +00:00
|
|
|
|
2019-08-16 15:27:19 +00:00
|
|
|
let start = 0, end = -1
|
|
|
|
if (errorRange) {
|
|
|
|
[start, end] = errorRange.map(r => r - 1)
|
2019-08-16 01:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-08-16 15:27:19 +00:00
|
|
|
messageHTML: `${ruleNames.join('/')}: ${ruleDescription}`,
|
2019-08-16 01:37:09 +00:00
|
|
|
severity: 'error',
|
|
|
|
from: CodeMirror.Pos(lineNumber, start),
|
|
|
|
to: CodeMirror.Pos(lineNumber, end)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-16 15:27:19 +00:00
|
|
|
CodeMirror.registerHelper('lint', 'markdown', validator)
|
|
|
|
})
|
2019-08-16 01:37:09 +00:00
|
|
|
|
|
|
|
function lint (content) {
|
|
|
|
const { content: errors } = markdownlint.sync({
|
|
|
|
strings: {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return errors
|
|
|
|
}
|