-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathworker.js
More file actions
118 lines (111 loc) · 2.27 KB
/
Copy pathworker.js
File metadata and controls
118 lines (111 loc) · 2.27 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* global Parser */
const onmessage = require( '../common.js' );
require( 'wikiparser-node/bundle/bundle-es8.min.js' );
// Rules to be treated as "info" severity in CodeMirrorLint.
const infoRules = [
'bold-header',
'format-leakage',
'table-layout',
'unknown-page',
'unclosed-table',
'unterminated-url',
'var-anchor'
];
const rules = {
'arg-in-ext': 0,
'blank-alt': 0,
'fostered-content': [
1,
{ transclusion: 0 }
],
h1: 1,
'illegal-attr': [
2,
{
tabindex: 1,
unknown: 2,
value: 2
}
],
'insecure-style': 0,
'invalid-url': 0,
'lonely-apos': [
1,
{
word: 0
}
],
'lonely-bracket': [
1,
{
extLink: 2,
single: 0
}
],
'lonely-http': 0,
'no-arg': 0,
'no-duplicate': [
2,
{
category: 1,
id: 1,
unknownImageParameter: 0
}
],
'required-attr': 0,
'syntax-like': 0,
'unmatched-tag': 0
};
// HACK: Customize severity of some rules to "info" which is not supported by wikiparser-node.
for ( const rule of infoRules ) {
rules[ rule ] = 1;
}
Parser.lintConfig = {
fix: false,
rules
};
const last = {};
const setLintConfig = ( config ) => {
Parser.lintConfig = Object.assign( config, {
// Rule customization should override the default configuration
rules: Object.assign( {}, rules, config.rules )
} );
};
const getLintConfig = () => Object.assign( {}, Parser.lintConfig, {
rules: Object.assign( {}, Parser.lintConfig.rules )
} );
const setConfig = ( config ) => {
if ( 'rules' in config ) {
// In case of wrong method call
setLintConfig( config );
} else {
Parser.config = config;
}
};
const getConfig = () => Parser.getConfig();
const setI18N = ( i18n ) => {
const obj = {};
for ( const key in i18n ) {
if ( key.startsWith( 'codemirror-wikilint-' ) ) {
obj[ key.slice( 20 ) ] = i18n[ key ];
}
}
Parser.i18n = obj;
};
const getI18N = () => Parser.i18n;
const lint = ( wikitext ) => {
if ( last.wikitext === wikitext ) {
return last.diagnostics;
}
const diagnostics = Parser.lint( wikitext )
.map( ( diag ) => {
if ( infoRules.includes( diag.rule ) && diag.severity === 'warning' ) {
diag.severity = 'info';
}
return diag;
} );
last.wikitext = wikitext;
last.diagnostics = diagnostics;
return diagnostics;
};
onmessage( setConfig, getConfig, lint, setI18N, getI18N, setLintConfig, getLintConfig );