1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const config = require('config');
|
5 | const htmllint = require('htmllint');
|
6 | const textTable = require('text-table');
|
7 |
|
8 | function getHtmllintOptions(isSnippet) {
|
9 | const configPath = '.htmllintrc';
|
10 | let htmllintOptions = {};
|
11 | if (fs.existsSync(configPath)) {
|
12 | htmllintOptions = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
13 | }
|
14 | if (isSnippet) {
|
15 | htmllintOptions['doctype-first'] = false;
|
16 | }
|
17 |
|
18 | return htmllintOptions;
|
19 | }
|
20 |
|
21 | function htmllintReporter(filepath, issues) {
|
22 | if (issues.length > 0) {
|
23 |
|
24 | const filePath = filepath.toString().replace(config.get('nitro.basePath'), '');
|
25 | const tableData = [];
|
26 |
|
27 | issues.forEach((issue) => {
|
28 | issue.msg = issue.msg || htmllint.messages.renderIssue(issue);
|
29 | issue.cell = `${(` ${issue.line}`).slice(-4)}:${issue.column}`;
|
30 | tableData.push([
|
31 | issue.cell,
|
32 | issue.msg,
|
33 | issue.rule,
|
34 | ]);
|
35 | });
|
36 |
|
37 | const table = textTable(tableData);
|
38 |
|
39 |
|
40 | console.log(`\n[htmllint] ${filePath}`);
|
41 | console.log(table);
|
42 | console.log('\n');
|
43 |
|
44 | process.exitCode = 1;
|
45 | }
|
46 | }
|
47 |
|
48 | function lintSnippet(templatePath, markup, options) {
|
49 |
|
50 | const htmllintOptions = options || getHtmllintOptions(true);
|
51 |
|
52 | return htmllint(markup, htmllintOptions)
|
53 | .then((issues) => {
|
54 | htmllintReporter(templatePath, issues);
|
55 | });
|
56 | }
|
57 |
|
58 | module.exports = {
|
59 | getHtmllintOptions,
|
60 | lintSnippet,
|
61 | htmllintReporter,
|
62 | };
|