Yukai Huang 31765a9d34
Make message more verbose & tweak style
Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
2019-08-18 21:57:32 +08:00

43 lines
959 B
JavaScript

import markdownlint from 'markdownlint'
// load CM lint plugin explicitly
import 'script-loader!@hackmd/codemirror/addon/lint/lint'
import './lint.css'
(function(mod) {
mod(CodeMirror);
})(function(CodeMirror) {
function validator(text, options) {
return lint(text).map(error => {
const lineNumber = error.lineNumber - 1
let start, end
if (error.errorRange) {
start = error.errorRange[0] - 1
end = error.errorRange[1] - 1
} else {
start = 0
end = -1
}
return {
messageHTML: `${error.ruleNames.join('/')}: ${error.ruleDescription}`,
severity: 'error',
from: CodeMirror.Pos(lineNumber, start),
to: CodeMirror.Pos(lineNumber, end)
}
})
}
CodeMirror.registerHelper('lint', 'markdown', validator);
});
function lint (content) {
const { content: errors } = markdownlint.sync({
strings: {
content
}
})
return errors
}