@saiballo/eleventy-plugin-validate-html 1.0.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  ## Installation
15
15
 
16
- Available on [npm](https://www.npmjs.com/package/eleventy-plugin-seo):
16
+ Available on [npm](https://www.npmjs.com/package/@saiballo/eleventy-plugin-validate-html):
17
17
 
18
18
  ```sh
19
19
  npm install @saiballo/eleventy-plugin-validate-html --save
@@ -24,7 +24,7 @@ Add the plugin to your `eleventy.config.js`:
24
24
  const pageValidation = require("@saiballo/eleventy-plugin-validate-html");
25
25
 
26
26
  module.exports = function(eleventyConfig) {
27
- eleventyConfig.addPlugin(pageValidation)
27
+ eleventyConfig.addPlugin(pageValidation)
28
28
  };
29
29
  ```
30
30
 
@@ -33,25 +33,33 @@ module.exports = function(eleventyConfig) {
33
33
  By default, the plugin checks all pages with the `.html` or `.htm` extensions. You can add or change extensions by providing a custom configuration. You can use "extensionList" parameter with a list of extensions (without the dot) separated by commas:
34
34
 
35
35
  ```js
36
- eleventyConfig.addPlugin(pageValidation, {
37
- "extensionList": "ext1,ext2"
38
- })
36
+ eleventyConfig.addPlugin(pageValidation, {
37
+ "extensionList": "ext1,ext2"
38
+ })
39
39
  ```
40
40
 
41
41
  This plugin relies on [html-validator](https://www.npmjs.com/package/html-validator). This allows you to use some configuration parameters from that package.
42
42
  The complete default configuration that you can override is:
43
43
 
44
44
  ```js
45
- eleventyConfig.addPlugin(pageValidation, {
46
- "validator": "WHATWG",
47
- "isLocal": false,
48
- "isFragment": false,
49
- "extensionList": "htm,html"
50
- })
45
+ eleventyConfig.addPlugin(pageValidation, {
46
+ "extensionList": "htm,html",
47
+ "validator": "WHATWG",
48
+ "isLocal": false,
49
+ "isFragment": false,
50
+ "ignore": [],
51
+ "whatwgConfig": {
52
+ "elements": [],
53
+ "rules": {}
54
+ }
55
+ })
51
56
  ```
52
57
  #### Parameters
53
58
  ```js
54
- // Possible values: "WHATWG" | "https://html5.validator.nu"
59
+ // as previously seen, it is a comma-separated list of extensions to check for validation
60
+ "extensionList": "htm,html"
61
+
62
+ // Possible values: "WHATWG" | "https://validator.w3.org/nu/"
55
63
  //"WHATWG" is recommended. it will validate against the WHATWG standards.
56
64
  "validator": "WHATWG"
57
65
 
@@ -59,15 +67,40 @@ The complete default configuration that you can override is:
59
67
  "isLocal": false
60
68
 
61
69
  // set this to true only if all your pages are not a complete document
62
- // if only certain pages are fragments, you can use "isFragment: true" in these pages as front matter data.
70
+ // if only certain pages are fragments, you can use "isFragment: true" in these pages as front matter data
63
71
  "isFragment": false
64
72
 
65
- // as previously seen, it is a comma-separated list of extensions to check for validation
66
- "extensionList": "htm,html"
73
+ // only for "WHATWG" validator. string or array of strings or rules (when using WHATWG) you want the checker to remove in the response. even partial text.
74
+ // e.g. "ignore": ["Mismatched close-tag, expected '</div>' but found '</body>'", "another partial error response text"]
75
+ "ignore": [],
76
+
77
+ // only for "WHATWG" validator. additional configuration for elements and rules
78
+ "whatwgConfig": {
79
+ "elements": [],
80
+ "rules": {}
81
+ }
82
+ ```
83
+ ## custom WHATWG configuration
84
+ ```js
85
+ // with this example, you add a custom tag called "customtag" to the valid tags in the validation. see https://html-validate.org/guide/metadata/simple-component.html
86
+ // additionally, you set the "heading-level" rule to "on" instead of "off". see https://html-validate.org/rules/index.html
87
+ "whatwgConfig": {
88
+ "elements": [
89
+ {
90
+ "customtag": {
91
+ "flow": true,
92
+ "phrasing": true
93
+ }
94
+ }
95
+ ],
96
+ "rules": {
97
+ "heading-level": "error"
98
+ }
99
+ }
67
100
  ```
68
101
  ## Front Matter Data
69
102
 
70
- Two parameters can be used as frontmatter data:
103
+ Two parameters can be used as front matter data:
71
104
 
72
105
  - `validateHtml: true|false` to enable or disable validation for individual pages.
73
106
  - `isFragment: true|false` to declare a document as a fragment.
package/main.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * @preserve
4
3
  * Filename: main.js
@@ -6,7 +5,7 @@
6
5
  * Created: 30/11/2024 (19:43:22)
7
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
8
7
  *
9
- * Last update: 06/12/2024 (18:07:42)
8
+ * Last update: 13/12/2024 (17:06:58)
10
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
11
10
  *
12
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -20,10 +19,15 @@ const pkg = require("./package.json");
20
19
  const toLog = require("./src/log.js");
21
20
 
22
21
  const defaultConfig = {
22
+ "extensionList": "htm,html",
23
23
  "validator": "WHATWG",
24
24
  "isLocal": false,
25
25
  "isFragment": false,
26
- "extensionList": "htm,html"
26
+ "ignore": [],
27
+ "whatwgConfig": {
28
+ "elements": [],
29
+ "rules": {}
30
+ }
27
31
  };
28
32
 
29
33
  module.exports = (eleventyconfig, config = {}) => {
@@ -40,13 +44,13 @@ module.exports = (eleventyconfig, config = {}) => {
40
44
 
41
45
  eleventyconfig.versionCheck(pkg["11ty"].compatibility);
42
46
 
43
- eleventyconfig.on("eleventy.after", (output) => {
47
+ eleventyconfig.on("eleventy.after", async (output) => {
44
48
 
45
- const pageList = getPageList(extensionList, output);
49
+ const pageList = await getPageList(extensionList, output);
46
50
 
47
51
  if (Object.keys(pageList).length > 0) {
48
52
 
49
- doValidation(pageList, pluginConfig);
53
+ await doValidation(pageList, pluginConfig);
50
54
 
51
55
  } else {
52
56
 
package/package.json CHANGED
@@ -1,15 +1,25 @@
1
1
  {
2
2
  "name": "@saiballo/eleventy-plugin-validate-html",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "description": "A plugin for 11ty which validates your site's pages after build.",
5
5
  "main": "main.js",
6
6
  "keywords": [
7
7
  "11ty",
8
8
  "eleventy",
9
9
  "eleventy-plugin",
10
- "html-validator"
10
+ "html-validator",
11
+ "@saiballo/html-validator-enhanced"
11
12
  ],
12
13
  "license": "MIT",
14
+ "author": {
15
+ "name": "Lorenzo \"Saibal\" Forti",
16
+ "email": "lorenzo.forti@gmail.com"
17
+ },
18
+ "homepage": "https://github.com/saiballo/eleventy-plugin-validate-html#readme",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/saiballo/eleventy-plugin-validate-html.git"
22
+ },
13
23
  "bugs": {
14
24
  "url": "https://github.com/saiballo/eleventy-plugin-validate-html"
15
25
  },
@@ -19,16 +29,13 @@
19
29
  "11ty": {
20
30
  "compatibility": ">=2.0.0"
21
31
  },
22
- "dependencies": {
23
- "html-validator": "^6.0.1",
24
- "front-matter": "^4.0.2"
32
+ "scripts": {
33
+ "refresh": "rm -rf node_modules && rm package-lock.json && npm install"
25
34
  },
26
- "author": {
27
- "name": "Lorenzo \"Saibal\" Forti",
28
- "email": "lorenzo.forti@gmail.com"
29
- },
30
- "repository": {
31
- "type": "git",
32
- "url": "https://github.com/saiballo/eleventy-plugin-validate-html.git"
35
+ "dependencies": {
36
+ "deep-extend": "^0.6.0",
37
+ "front-matter": "^4.0.2",
38
+ "@saiballo/html-validator-enhanced": "^1.0.0"
39
+
33
40
  }
34
- }
41
+ }
package/src/log.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 30/11/2024 (18:44:22)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 06/12/2024 (18:08:18)
8
+ * Last update: 07/12/2024 (21:02:39)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -31,7 +31,7 @@ const toLog = (message = "OPS! An error has occured", type = "default") => {
31
31
  break;
32
32
  }
33
33
 
34
- const logLabel = `${logIcon} plugin-validate-html:`;
34
+ const logLabel = `${logIcon} plugin-validate-html -`;
35
35
 
36
36
  console.error(`${logLabel} ${message}`);
37
37
  };
package/src/pageList.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 02/12/2024 (19:59:52)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 06/12/2024 (18:08:06)
8
+ * Last update: 13/12/2024 (17:25:18)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -15,24 +15,89 @@
15
15
 
16
16
  "use strict";
17
17
 
18
- // get only valid extension pages
19
- const getPageList = (extensionlist, output) => {
18
+ const fs = require("fs").promises;
19
+ const fm = require("front-matter");
20
+ const toLog = require("./log.js");
21
+
22
+ // get frontmatter data from every template
23
+ const getFrontMatter = async (filePath) => {
24
+
25
+ try {
26
+
27
+ const fileContent = await fs.readFile(filePath, "utf8");
28
+ const parsedData = fm(fileContent);
29
+
30
+ return parsedData["attributes"];
31
+
32
+ } catch (err) {
33
+
34
+ toLog(err, "error");
35
+ }
36
+ };
37
+
38
+ // get only valid extension pages. used Promise.all() for greater efficiency
39
+ const getPageList = async (extensionlist, output) => {
20
40
 
21
41
  const extensionList = extensionlist.split(",").join("|");
22
- const pageList = [];
42
+ const validExtension = new RegExp(`\\.(${extensionList})$`);
43
+
44
+ const promises = output["results"].map(async (res) => {
45
+
46
+ if (validExtension.test(res["outputPath"]) === true) {
47
+
48
+ try {
23
49
 
24
- output.results.flatMap((res) => {
50
+ const frontMatter = await getFrontMatter(res["inputPath"]);
51
+ const toValidate = frontMatter["validateHtml"] ?? true;
25
52
 
26
- if (res.outputPath.match(`^.*\\.(${extensionList})$`) !== null) {
53
+ if (toValidate === true) {
27
54
 
28
- pageList.push({
29
- "input": res["inputPath"],
30
- "output": res["outputPath"]
31
- });
55
+ return {
56
+ "input": res["inputPath"],
57
+ "output": res["outputPath"],
58
+ "isFragment": frontMatter["isFragment"] ?? null
59
+ };
60
+ }
61
+
62
+ } catch (err) {
63
+
64
+ toLog(err, "error");
65
+ }
32
66
  }
67
+
68
+ return null;
33
69
  });
34
70
 
35
- return pageList;
71
+ return (await Promise.all(promises)).filter(Boolean);
36
72
  };
37
73
 
74
+ // get only valid extension pages. this version has await inside loop. less efficient
75
+ // /* eslint-disable no-await-in-loop */
76
+ // const getPageList = async (extensionlist, output) => {
77
+
78
+ // const extensionList = extensionlist.split(",").join("|");
79
+ // const pageList = [];
80
+
81
+ // for (const res of output["results"]) {
82
+
83
+ // if (res["outputPath"].match(`\\.(${extensionList})$`) !== null) {
84
+
85
+ // const frontMatter = await getFrontMatter(res["inputPath"]);
86
+ // const toValidate = frontMatter["validateHtml"] ?? true;
87
+
88
+ // if (toValidate === true) {
89
+
90
+ // pageList.push({
91
+ // "input": res["inputPath"],
92
+ // "output": res["outputPath"],
93
+ // "isFragment": frontMatter["isFragment"] ?? null
94
+ // });
95
+ // }
96
+ // }
97
+ // }
98
+
99
+ // return pageList;
100
+ // };
101
+ // /* eslint-enable no-await-in-loop */
102
+
38
103
  module["exports"] = getPageList;
package/src/validate.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 30/11/2024 (18:44:22)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 06/12/2024 (18:07:58)
8
+ * Last update: 13/12/2024 (17:09:11)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -15,33 +15,46 @@
15
15
 
16
16
  "use strict";
17
17
 
18
- const htmlValidator = require("html-validator");
19
- const fs = require("fs");
20
- const fm = require("front-matter");
18
+ const htmlValidator = require("@saiballo/html-validator-enhanced");
19
+ const fs = require("node:fs");
21
20
  const toLog = require("./log.js");
22
21
 
23
- // get frontmatter data from every template
24
- const getMatterData = (item) => {
22
+ // validator different from WHATWG and format "json" does not accept "ignore" option
23
+ const checkIgnoreOption = (pluginconfig) => {
25
24
 
26
- return new Promise((resolve, reject) => {
25
+ const clonedConfig = {
26
+ ...pluginconfig
27
+ };
27
28
 
28
- fs.readFile(item, "utf8", (err, data) => {
29
+ if (clonedConfig["validator"].toLowerCase() !== "whatwg") {
29
30
 
30
- if (err !== null) {
31
+ delete clonedConfig["ignore"];
32
+ }
31
33
 
32
- toLog(err, "error");
33
- reject(err);
34
- }
34
+ return clonedConfig;
35
+ };
36
+
37
+ // collect errors list
38
+ const processError = (errorlist, isWhatwg = true) => {
35
39
 
36
- const matterData = fm(data);
40
+ const result = [];
37
41
 
38
- resolve(matterData["attributes"]);
39
- });
42
+ errorlist.forEach((error, index) => {
43
+
44
+ const line = isWhatwg === true ? error["line"] : error["lastLine"];
45
+ const column = isWhatwg === true ? error["column"] : error["lastColumn"];
46
+
47
+ result.push(`${index + 1}. ${error.message} [Line ${line}, Column ${column}]`);
40
48
  });
49
+
50
+ return result;
41
51
  };
42
52
 
53
+ // validation main function
43
54
  const doValidation = async (pageList, pluginconfig) => {
44
55
 
56
+ const pluginConfig = checkIgnoreOption(pluginconfig);
57
+ const validatorType = pluginConfig["validator"].toLowerCase();
45
58
  let checkedTotal = 0;
46
59
  let counterError = 0;
47
60
 
@@ -51,73 +64,59 @@ const doValidation = async (pageList, pluginconfig) => {
51
64
 
52
65
  try {
53
66
 
54
- const frontMatter = await getMatterData(page["input"]);
55
- const toValidate = frontMatter["validateHtml"] ?? true;
56
-
57
- if (toValidate === true) {
67
+ let errorList = [];
68
+ let isValid = true;
69
+ // increment counter
70
+ checkedTotal += 1;
58
71
 
59
- const errorText = [];
60
- let isValid = true;
61
- // increment counter
62
- checkedTotal += 1;
72
+ const validateConfig = {
73
+ ...pluginConfig,
74
+ // not want user can overwrites format option. must be json
75
+ "format": "json",
76
+ "isFragment": page["isFragment"] ?? pluginconfig["isFragment"],
77
+ "data": fs.readFileSync(page["output"], "utf8")
78
+ };
63
79
 
64
- if (typeof frontMatter["isFragment"] !== "undefined") {
80
+ // validating the page
81
+ const result = await htmlValidator(validateConfig);
65
82
 
66
- pluginconfig["isFragment"] = frontMatter["isFragment"];
67
- }
83
+ // validator WHATWG
84
+ if (validatorType === "whatwg" && result["isValid"] === false) {
68
85
 
69
- const validateConfig = {
70
- ...pluginconfig,
71
- // not want user can overwrites format option. must be json
72
- "format": "json",
73
- "data": fs.readFileSync(page["output"], "utf8")
74
- };
86
+ errorList = processError(result["errors"]);
75
87
 
76
- // validating the page
77
- const result = await htmlValidator(validateConfig);
78
-
79
- // validator WHATWG
80
- if (pluginconfig["validator"] === "WHATWG" && result["isValid"] === false) {
81
-
82
- result["errors"].forEach((item, index) => {
83
-
84
- errorText.push(`${index+1}. ${item.message} [Line ${item.line}, Column ${item.column}]`);
85
- });
86
-
87
- isValid = false;
88
- }
89
-
90
- // validator web url
91
- if (pluginconfig["validator"] !== "WHATWG" && result["messages"].length > 0) {
88
+ isValid = false;
89
+ }
92
90
 
93
- result["messages"].forEach((item, index) => {
91
+ // validator w3c
92
+ if (validatorType !== "whatwg" && result["messages"].length > 0) {
94
93
 
95
- errorText.push(`${index+1}. ${item.message} [Line ${item.lastLine}, Column ${item.lastColumn}]`);
96
- });
94
+ errorList = processError(result["messages"]);
97
95
 
98
- isValid = false;
96
+ isValid = false;
99
97
 
100
- // code for text format. not used
101
- // const validText = [
102
- // "The document validates according to the specified schema(s).",
103
- // "The document is valid HTML5 + ARIA + SVG 1.1 + MathML 2.0"
104
- // ];
98
+ // code for text format. not used
99
+ // const validText = [
100
+ // "The document validates according to the specified schema(s).",
101
+ // "The document is valid HTML5 + ARIA + SVG 1.1 + MathML 2.0"
102
+ // ];
105
103
 
106
- // isValid = validText.some((el) => result.includes(el));
107
- // message = result;
108
- }
104
+ // isValid = validText.some((el) => result.includes(el));
105
+ // message = result;
106
+ }
109
107
 
110
- if (isValid === false) {
108
+ if (isValid === false) {
111
109
 
112
- const message = errorText.join("\n");
113
- counterError += 1;
110
+ const message = errorList.join("\n");
111
+ counterError += 1;
114
112
 
115
- toLog(`validation error āŒ\nšŸ“• Input: ${page["input"]}\nšŸ“˜ Output: ${page["output"]}\n${message}\n`, "error");
116
- }
113
+ toLog(`validation error āŒ\nšŸ“• Input: ${page["input"]}\nšŸ“˜ Output: ${page["output"]}\n${message}\n`, "error");
117
114
  }
118
115
 
119
116
  } catch (err) {
120
117
 
118
+ counterError += 1;
119
+
121
120
  toLog(err, "error");
122
121
  }
123
122
  }