3
3
// load CM lint plugin explicitly
4
4
import '@hackmd/codemirror/addon/lint/lint'
5
5
6
+ import '@hackmd/codemirror/addon/hint/show-hint.css'
7
+ import helpers from 'markdownlint-rule-helpers'
8
+
6
9
window . markdownit = require ( 'markdown-it' )
7
10
// eslint-disable-next-line
8
11
require ( 'script-loader!markdownlint' ) ;
@@ -26,22 +29,76 @@ require('script-loader!markdownlint');
26
29
}
27
30
28
31
return {
29
- messageHTML : `${ ruleNames . join ( '/' ) } : ${ ruleDescription } ` ,
32
+ messageHTML : `${ ruleNames . slice ( 0 , 1 ) } : ${ ruleDescription } ` ,
30
33
severity : 'error' ,
31
34
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
33
40
}
34
41
} )
35
42
}
36
43
37
44
CodeMirror . registerHelper ( 'lint' , 'markdown' , validator )
38
45
} )
39
46
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
+
40
96
function lint ( content ) {
41
97
const { content : errors } = window . markdownlint . sync ( {
42
98
strings : {
43
99
content
44
- }
100
+ } ,
101
+ resultVersion : 3
45
102
} )
46
103
return errors
47
104
}
0 commit comments