2019-08-18 06:09:12 +00:00
|
|
|
/* global CodeMirror */
|
2019-08-16 01:37:09 +00:00
|
|
|
|
2019-08-16 15:22:58 +00:00
|
|
|
// load CM lint plugin explicitly
|
2019-08-18 06:09:12 +00:00
|
|
|
import '@hackmd/codemirror/addon/lint/lint'
|
2019-08-16 01:37:09 +00:00
|
|
|
import './lint.css'
|
|
|
|
|
2019-11-01 08:47:41 +00:00
|
|
|
window.markdownit = require('markdown-it')
|
|
|
|
// eslint-disable-next-line
|
|
|
|
require('script-loader!markdownlint');
|
|
|
|
|
2019-08-18 06:09:12 +00:00
|
|
|
(function (mod) {
|
|
|
|
mod(CodeMirror)
|
|
|
|
})(function (CodeMirror) {
|
|
|
|
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-18 06:09:12 +00:00
|
|
|
let start = 0; let end = -1
|
2019-08-16 15:27:19 +00:00
|
|
|
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) {
|
2019-11-01 08:33:58 +00:00
|
|
|
const { content: errors } = window.markdownlint.sync({
|
2019-08-16 01:37:09 +00:00
|
|
|
strings: {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return errors
|
|
|
|
}
|