33// load CM lint plugin explicitly
44import '@hackmd/codemirror/addon/lint/lint'
55
6+ import '@hackmd/codemirror/addon/hint/show-hint.css'
7+ import helpers from 'markdownlint-rule-helpers'
8+
69window . markdownit = require ( 'markdown-it' )
710// eslint-disable-next-line
811require ( 'script-loader!markdownlint' ) ;
@@ -26,22 +29,76 @@ require('script-loader!markdownlint');
2629 }
2730
2831 return {
29- messageHTML : `${ ruleNames . join ( '/' ) } : ${ ruleDescription } ` ,
32+ messageHTML : `${ ruleNames . slice ( 0 , 1 ) } : ${ ruleDescription } ` ,
3033 severity : 'error' ,
3134 from : CodeMirror . Pos ( lineNumber , start ) ,
32- to : CodeMirror . Pos ( lineNumber , end )
35+ to : CodeMirror . Pos ( lineNumber , end ) ,
36+ __ruleNames : ruleNames ,
37+ __ruleDescription : ruleDescription ,
38+ __error : error ,
39+ __lineNumber : lineNumber
3340 }
3441 } )
3542 }
3643
3744 CodeMirror . registerHelper ( 'lint' , 'markdown' , validator )
3845} )
3946
47+ export const linterOptions = {
48+ fixedTooltip : true ,
49+ contextmenu : annotations => {
50+ const singleFixMenus = annotations
51+ . filter ( ann => ann . __error . fixInfo )
52+ . map ( annotation => {
53+ const error = annotation . __error
54+ return {
55+ content : `Fix ${ error . ruleDescription } ` ,
56+ onClick ( ) {
57+ const doc = window . editor . doc
58+ const fixInfo = error . fixInfo
59+ const line = fixInfo . lineNumber - 1
60+ const lineContent = doc . getLine ( line ) || ''
61+ const fixedText = helpers . applyFix ( lineContent , error . fixInfo , '\n' )
62+
63+ let from = { line, ch : 0 }
64+ let to = { line, ch : lineContent ? lineContent . length - 1 : 0 }
65+
66+ if ( typeof fixedText === 'string' ) {
67+ doc . replaceRange ( fixedText , from , to )
68+ } else {
69+ if ( fixInfo . lineNumber === 1 ) {
70+ if ( document . lineCount > 1 ) {
71+ const nextLine = doc . getLine ( to . line + 1 ) || ''
72+ to = {
73+ line : nextLine ,
74+ ch : 0
75+ }
76+ }
77+ } else {
78+ const previousLine = doc . getLine ( from . line - 1 ) || ''
79+ from = {
80+ line : previousLine ,
81+ ch : previousLine . length
82+ }
83+ }
84+
85+ // !FIXME: certain range out of bound
86+ doc . replaceRange ( '' , from , to )
87+ }
88+ }
89+ }
90+ } )
91+
92+ return singleFixMenus
93+ }
94+ }
95+
4096function lint ( content ) {
4197 const { content : errors } = window . markdownlint . sync ( {
4298 strings : {
4399 content
44- }
100+ } ,
101+ resultVersion : 3
45102 } )
46103 return errors
47104}
0 commit comments