-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathhtml-worker.js
More file actions
100 lines (93 loc) · 3.98 KB
/
html-worker.js
File metadata and controls
100 lines (93 loc) · 3.98 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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global WorkerComm, HTMLLanguageService*/
(function () {
let htmlValidator = HTMLLanguageService.createHTMLValidator({
extends: ["html-validate:standard"]
});
WorkerComm.triggerPeer("html_lint_extension_Loaded", {});
let isUsingCustomConfig = false, currentConfigID;
function setupValidator(config, configID) {
try{
if(!config && isUsingCustomConfig) {
// reset the config
htmlValidator = HTMLLanguageService.createHTMLValidator({
extends: ["html-validate:standard"]
});
isUsingCustomConfig = false;
currentConfigID = null;
} else if(config && currentConfigID !== configID) {
htmlValidator = HTMLLanguageService.createHTMLValidator(config);
isUsingCustomConfig = true;
currentConfigID = configID;
}
return null;
} catch (e) {
return e.message;
}
}
async function htmlLint(params) {
let errorMessage = setupValidator(params.config, params.configID);
if(errorMessage) {
return [{
start: 0,
end: 0,
severity: 2, // 1 warning and 2 is error
message: "Invalid config file `.htmlvalidate.json`"+ errorMessage,
ruleId: "INVALID_CONFIG"
}];
}
const validatorResult = await htmlValidator.validateString(params.text, params.filePath);
if(!validatorResult || !validatorResult.results || !validatorResult.results.length){
return [];
}
const errors = [];
for(let result of validatorResult.results){
if(result.messages && result.messages.length) {
for(let message of result.messages){
errors.push({
// The starting position of the error (0-based line and column index)
startPos: {line: (message.line || 1) - 1, ch: (message.column || 1) - 1},
// The length (in characters) of the error from the starting position. this is an offset
// than can span multiple lines.
highlightOffset: (message.size || 1),
start: message.offset,
end: message.offset + (message.size || 1) - 1,
severity: message.severity,
message: message.message,
ruleId: message.ruleId,
ruleUrl: message.ruleUrl // this is a doc link for the ruleId config, not good to show
// to a user who doesnt know about html validator config is.
});
}
}
}
return errors;
}
async function updateHTMLLintConfig(params) {
if(params.config){
console.error("HTML Lint worker updateHTMLLintConfig received null config", params);
return;
}
htmlValidator = HTMLLanguageService.createHTMLValidator(params.config);
}
WorkerComm.setExecHandler("htmlLint", htmlLint);
WorkerComm.setExecHandler("updateHTMLLintConfig", updateHTMLLintConfig);
}());