@saiballo/eleventy-plugin-validate-html 1.0.3 → 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
@@ -59,7 +59,7 @@ eleventyConfig.addPlugin(pageValidation, {
59
59
  // as previously seen, it is a comma-separated list of extensions to check for validation
60
60
  "extensionList": "htm,html"
61
61
 
62
- // Possible values: "WHATWG" | "https://html5.validator.nu"
62
+ // Possible values: "WHATWG" | "https://validator.w3.org/nu/"
63
63
  //"WHATWG" is recommended. it will validate against the WHATWG standards.
64
64
  "validator": "WHATWG"
65
65
 
package/main.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 30/11/2024 (19:43:22)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 12/12/2024 (10:47:22)
8
+ * Last update: 13/12/2024 (17:06:58)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -44,13 +44,13 @@ module.exports = (eleventyconfig, config = {}) => {
44
44
 
45
45
  eleventyconfig.versionCheck(pkg["11ty"].compatibility);
46
46
 
47
- eleventyconfig.on("eleventy.after", (output) => {
47
+ eleventyconfig.on("eleventy.after", async (output) => {
48
48
 
49
- const pageList = getPageList(extensionList, output);
49
+ const pageList = await getPageList(extensionList, output);
50
50
 
51
51
  if (Object.keys(pageList).length > 0) {
52
52
 
53
- doValidation(pageList, pluginConfig);
53
+ await doValidation(pageList, pluginConfig);
54
54
 
55
55
  } else {
56
56
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saiballo/eleventy-plugin-validate-html",
3
- "version": "1.0.3",
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": [
@@ -18,7 +18,7 @@
18
18
  "homepage": "https://github.com/saiballo/eleventy-plugin-validate-html#readme",
19
19
  "repository": {
20
20
  "type": "git",
21
- "url": "https://github.com/saiballo/eleventy-plugin-validate-html.git"
21
+ "url": "git+https://github.com/saiballo/eleventy-plugin-validate-html.git"
22
22
  },
23
23
  "bugs": {
24
24
  "url": "https://github.com/saiballo/eleventy-plugin-validate-html"
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: 12/12/2024 (11:03:09)
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
@@ -17,29 +17,8 @@
17
17
 
18
18
  const htmlValidator = require("@saiballo/html-validator-enhanced");
19
19
  const fs = require("node:fs");
20
- const fm = require("front-matter");
21
20
  const toLog = require("./log.js");
22
21
 
23
- // get frontmatter data from every template
24
- const getMatterData = (item) => {
25
-
26
- return new Promise((resolve, reject) => {
27
-
28
- fs.readFile(item, "utf8", (err, data) => {
29
-
30
- if (err !== null) {
31
-
32
- toLog(err, "error");
33
- reject(err);
34
- }
35
-
36
- const matterData = fm(data);
37
-
38
- resolve(matterData["attributes"]);
39
- });
40
- });
41
- };
42
-
43
22
  // validator different from WHATWG and format "json" does not accept "ignore" option
44
23
  const checkIgnoreOption = (pluginconfig) => {
45
24
 
@@ -55,83 +34,83 @@ const checkIgnoreOption = (pluginconfig) => {
55
34
  return clonedConfig;
56
35
  };
57
36
 
37
+ // collect errors list
38
+ const processError = (errorlist, isWhatwg = true) => {
39
+
40
+ const result = [];
41
+
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}]`);
48
+ });
49
+
50
+ return result;
51
+ };
58
52
 
53
+ // validation main function
59
54
  const doValidation = async (pageList, pluginconfig) => {
60
55
 
56
+ const pluginConfig = checkIgnoreOption(pluginconfig);
57
+ const validatorType = pluginConfig["validator"].toLowerCase();
61
58
  let checkedTotal = 0;
62
59
  let counterError = 0;
63
60
 
64
- const pluginConfig = checkIgnoreOption(pluginconfig);
65
-
66
61
  /* eslint-disable no-await-in-loop */
67
62
  // avoid to send to validator an excessive amount of requests in parallel using await in loop
68
63
  for (const page of pageList) {
69
64
 
70
65
  try {
71
66
 
72
- const frontMatter = await getMatterData(page["input"]);
73
- const toValidate = frontMatter["validateHtml"] ?? true;
74
-
75
- if (toValidate === true) {
67
+ let errorList = [];
68
+ let isValid = true;
69
+ // increment counter
70
+ checkedTotal += 1;
76
71
 
77
- const errorText = [];
78
- let isValid = true;
79
- // increment counter
80
- 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
+ };
81
79
 
82
- if (typeof frontMatter["isFragment"] !== "undefined") {
80
+ // validating the page
81
+ const result = await htmlValidator(validateConfig);
83
82
 
84
- pluginConfig["isFragment"] = frontMatter["isFragment"];
85
- }
83
+ // validator WHATWG
84
+ if (validatorType === "whatwg" && result["isValid"] === false) {
86
85
 
87
- const validateConfig = {
88
- ...pluginConfig,
89
- // not want user can overwrites format option. must be json
90
- "format": "json",
91
- "data": fs.readFileSync(page["output"], "utf8")
92
- };
86
+ errorList = processError(result["errors"]);
93
87
 
94
- // validating the page
95
- const result = await htmlValidator(validateConfig);
96
-
97
- // validator WHATWG
98
- if (pluginConfig["validator"].toLowerCase() === "whatwg" && result["isValid"] === false) {
99
-
100
- result["errors"].forEach((item, index) => {
101
-
102
- errorText.push(`${index+1}. ${item.message} [Line ${item.line}, Column ${item.column}]`);
103
- });
104
-
105
- isValid = false;
106
- }
107
-
108
- // validator web url
109
- if (pluginConfig["validator"].toLowerCase() !== "whatwg" && result["messages"].length > 0) {
88
+ isValid = false;
89
+ }
110
90
 
111
- result["messages"].forEach((item, index) => {
91
+ // validator w3c
92
+ if (validatorType !== "whatwg" && result["messages"].length > 0) {
112
93
 
113
- errorText.push(`${index+1}. ${item.message} [Line ${item.lastLine}, Column ${item.lastColumn}]`);
114
- });
94
+ errorList = processError(result["messages"]);
115
95
 
116
- isValid = false;
96
+ isValid = false;
117
97
 
118
- // code for text format. not used
119
- // const validText = [
120
- // "The document validates according to the specified schema(s).",
121
- // "The document is valid HTML5 + ARIA + SVG 1.1 + MathML 2.0"
122
- // ];
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
+ // ];
123
103
 
124
- // isValid = validText.some((el) => result.includes(el));
125
- // message = result;
126
- }
104
+ // isValid = validText.some((el) => result.includes(el));
105
+ // message = result;
106
+ }
127
107
 
128
- if (isValid === false) {
108
+ if (isValid === false) {
129
109
 
130
- const message = errorText.join("\n");
131
- counterError += 1;
110
+ const message = errorList.join("\n");
111
+ counterError += 1;
132
112
 
133
- toLog(`validation error āŒ\nšŸ“• Input: ${page["input"]}\nšŸ“˜ Output: ${page["output"]}\n${message}\n`, "error");
134
- }
113
+ toLog(`validation error āŒ\nšŸ“• Input: ${page["input"]}\nšŸ“˜ Output: ${page["output"]}\n${message}\n`, "error");
135
114
  }
136
115
 
137
116
  } catch (err) {