-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (49 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use babel';
import { documentParser, MJMLValidator } from 'mjml'
import find from 'lodash/find'
const getMJBody = (root) => find(root.children, ['tagName', 'mj-body'])
export default {
activate() {
require('atom-package-deps').install('linter-mjml');
window.mjml_disable_jquery = true
},
deactivate() {
window.mjml_disable_jquery = false
},
provideLinter() {
return {
name: 'MJML',
scope: 'file',
grammarScopes: ['text.mjml.basic'],
lintsOnChange: false,
lint: (TextEditor) => {
return new Promise((resolve, reject) => {
let MJMLDocument
const filePath = TextEditor.getPath();
const fileText = TextEditor.getText()
try {
MJMLDocument = documentParser(fileText)
} catch (e) {
reject(e)
}
const body = getMJBody(MJMLDocument)
if (!body || !body.children || body.children.length == 0) {
reject()
}
const report = MJMLValidator(body.children[0])
const formattedError = report.map(e => {
const line = e.line - 1
const currentLine = TextEditor.getBuffer().lineForRow(e.line - 1)
return {
filePath,
range: [[line, currentLine.indexOf('<')], [line, TextEditor.getBuffer().lineLengthForRow(line)]],
type: 'Error',
text: e.message
}
})
resolve(formattedError)
})
}
}
}
}