@saiballo/eleventy-plugin-validate-html 1.0.0

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 ADDED
@@ -0,0 +1,97 @@
1
+ <p align="center">
2
+ <img src="https://www.11ty.dev/img/logo-github.svg" width="100" height="100" alt="11ty logo">
3
+ </p>
4
+
5
+ # eleventy-plugin-validate-html
6
+
7
+ > A plugin, with options, that runs on the `eleventy.after` event and validates all the files that were built.
8
+ > Inspired by [matt-auckland's plugin](https://github.com/matt-auckland/eleventy-plugin-html-validate)
9
+
10
+
11
+ ![](https://img.shields.io/badge/Made%20with%20love%20and%20with-javascript%2C%20node-blue)
12
+ [![MIT license](https://img.shields.io/badge/License-MIT-green.svg)](https://lbesson.mit-license.org/)
13
+
14
+ ## Installation
15
+
16
+ Available on [npm](https://www.npmjs.com/package/eleventy-plugin-seo):
17
+
18
+ ```sh
19
+ npm install @saiballo/eleventy-plugin-validate-html --save
20
+ ```
21
+ Add the plugin to your `eleventy.config.js`:
22
+
23
+ ```js
24
+ const pageValidation = require("@saiballo/eleventy-plugin-validate-html");
25
+
26
+ module.exports = function(eleventyConfig) {
27
+ eleventyConfig.addPlugin(pageValidation)
28
+ };
29
+ ```
30
+
31
+ ## Usage
32
+
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
+
35
+ ```js
36
+ eleventyConfig.addPlugin(pageValidation, {
37
+ "extensionList": "ext1,ext2"
38
+ })
39
+ ```
40
+
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
+ The complete default configuration that you can override is:
43
+
44
+ ```js
45
+ eleventyConfig.addPlugin(pageValidation, {
46
+ "validator": "WHATWG",
47
+ "isLocal": false,
48
+ "isFragment": false,
49
+ "extensionList": "htm,html"
50
+ })
51
+ ```
52
+ #### Parameters
53
+ ```js
54
+ // Possible values: "WHATWG" | "https://html5.validator.nu"
55
+ //"WHATWG" is recommended. it will validate against the WHATWG standards.
56
+ "validator": "WHATWG"
57
+
58
+ // set this to true if you want to validate local urls
59
+ "isLocal": false
60
+
61
+ // 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.
63
+ "isFragment": false
64
+
65
+ // as previously seen, it is a comma-separated list of extensions to check for validation
66
+ "extensionList": "htm,html"
67
+ ```
68
+ ## Front Matter Data
69
+
70
+ Two parameters can be used as frontmatter data:
71
+
72
+ - `validateHtml: true|false` to enable or disable validation for individual pages.
73
+ - `isFragment: true|false` to declare a document as a fragment.
74
+
75
+ If the parameters are not set, the defaults are:
76
+
77
+ - `validateHtml: true`
78
+ - `isFragment: false`
79
+
80
+
81
+ Example:
82
+
83
+ ```js
84
+ ---
85
+ validateHtml: true
86
+ isFragment: false
87
+ ---
88
+ ```
89
+
90
+ ## Team ARMADA 429
91
+ <img src="https://raw.githubusercontent.com/saiballo/saiballo/refs/heads/master/armada429.png" width="100" height="100">
92
+
93
+ * Lorenzo "Saibal" Forti
94
+
95
+ ## License
96
+
97
+ ![](https://img.shields.io/badge/License-Copyleft%20Saibal%20--%20All%20Rights%20Reserved-red)
package/main.js ADDED
@@ -0,0 +1,61 @@
1
+
2
+ /**
3
+ * @preserve
4
+ * Filename: main.js
5
+ *
6
+ * Created: 30/11/2024 (19:43:22)
7
+ * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
8
+ *
9
+ * Last update: 06/12/2024 (18:07:42)
10
+ * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
11
+ *
12
+ * Copyleft: 2024 - Tutti i diritti riservati
13
+ *
14
+ * Comments:
15
+ */
16
+
17
+ const getPageList = require("./src/pageList.js");
18
+ const doValidation = require("./src/validate.js");
19
+ const pkg = require("./package.json");
20
+ const toLog = require("./src/log.js");
21
+
22
+ const defaultConfig = {
23
+ "validator": "WHATWG",
24
+ "isLocal": false,
25
+ "isFragment": false,
26
+ "extensionList": "htm,html"
27
+ };
28
+
29
+ module.exports = (eleventyconfig, config = {}) => {
30
+
31
+ try {
32
+
33
+ // merge defaultConfig with custom config
34
+ const pluginConfig = {
35
+ ...defaultConfig,
36
+ ...config
37
+ };
38
+
39
+ const { extensionList } = pluginConfig;
40
+
41
+ eleventyconfig.versionCheck(pkg["11ty"].compatibility);
42
+
43
+ eleventyconfig.on("eleventy.after", (output) => {
44
+
45
+ const pageList = getPageList(extensionList, output);
46
+
47
+ if (Object.keys(pageList).length > 0) {
48
+
49
+ doValidation(pageList, pluginConfig);
50
+
51
+ } else {
52
+
53
+ toLog("no valid page extensions found in dist/public folder. nothing to validate.");
54
+ }
55
+ });
56
+
57
+ } catch (err) {
58
+
59
+ toLog(err, "error");
60
+ }
61
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@saiballo/eleventy-plugin-validate-html",
3
+ "version": "1.0.0",
4
+ "description": "A plugin for 11ty which validates your site's pages after build.",
5
+ "main": "main.js",
6
+ "keywords": [
7
+ "11ty",
8
+ "eleventy",
9
+ "eleventy-plugin",
10
+ "html-validator"
11
+ ],
12
+ "license": "MIT",
13
+ "bugs": {
14
+ "url": "https://github.com/saiballo/eleventy-plugin-validate-html"
15
+ },
16
+ "engines": {
17
+ "node": ">=16"
18
+ },
19
+ "11ty": {
20
+ "compatibility": ">=2.0.0"
21
+ },
22
+ "dependencies": {
23
+ "html-validator": "^6.0.1",
24
+ "front-matter": "^4.0.2"
25
+ },
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"
33
+ }
34
+ }
package/src/log.js ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @preserve
3
+ * Filename: log.js
4
+ *
5
+ * Created: 30/11/2024 (18:44:22)
6
+ * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
+ *
8
+ * Last update: 06/12/2024 (18:08:18)
9
+ * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
+ *
11
+ * Copyleft: 2024 - Tutti i diritti riservati
12
+ *
13
+ * Comments:
14
+ */
15
+
16
+ "use strict";
17
+
18
+ const toLog = (message = "OPS! An error has occured", type = "default") => {
19
+
20
+ let logIcon;
21
+
22
+ switch (type) {
23
+ case "info":
24
+ logIcon = "āœ…";
25
+ break;
26
+ case "error":
27
+ logIcon = "āŒ";
28
+ break;
29
+ default:
30
+ logIcon = "šŸ“£";
31
+ break;
32
+ }
33
+
34
+ const logLabel = `${logIcon} plugin-validate-html:`;
35
+
36
+ console.error(`${logLabel} ${message}`);
37
+ };
38
+
39
+ module["exports"] = toLog;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @preserve
3
+ * Filename: pageList.js
4
+ *
5
+ * Created: 02/12/2024 (19:59:52)
6
+ * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
+ *
8
+ * Last update: 06/12/2024 (18:08:06)
9
+ * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
+ *
11
+ * Copyleft: 2024 - Tutti i diritti riservati
12
+ *
13
+ * Comments:
14
+ */
15
+
16
+ "use strict";
17
+
18
+ // get only valid extension pages
19
+ const getPageList = (extensionlist, output) => {
20
+
21
+ const extensionList = extensionlist.split(",").join("|");
22
+ const pageList = [];
23
+
24
+ output.results.flatMap((res) => {
25
+
26
+ if (res.outputPath.match(`^.*\\.(${extensionList})$`) !== null) {
27
+
28
+ pageList.push({
29
+ "input": res["inputPath"],
30
+ "output": res["outputPath"]
31
+ });
32
+ }
33
+ });
34
+
35
+ return pageList;
36
+ };
37
+
38
+ module["exports"] = getPageList;
@@ -0,0 +1,138 @@
1
+ /**
2
+ * @preserve
3
+ * Filename: validate.js
4
+ *
5
+ * Created: 30/11/2024 (18:44:22)
6
+ * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
+ *
8
+ * Last update: 06/12/2024 (18:07:58)
9
+ * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
+ *
11
+ * Copyleft: 2024 - Tutti i diritti riservati
12
+ *
13
+ * Comments:
14
+ */
15
+
16
+ "use strict";
17
+
18
+ const htmlValidator = require("html-validator");
19
+ const fs = require("fs");
20
+ const fm = require("front-matter");
21
+ const toLog = require("./log.js");
22
+
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
+ const doValidation = async (pageList, pluginconfig) => {
44
+
45
+ let checkedTotal = 0;
46
+ let counterError = 0;
47
+
48
+ /* eslint-disable no-await-in-loop */
49
+ // avoid to send to validator an excessive amount of requests in parallel using await in loop
50
+ for (const page of pageList) {
51
+
52
+ try {
53
+
54
+ const frontMatter = await getMatterData(page["input"]);
55
+ const toValidate = frontMatter["validateHtml"] ?? true;
56
+
57
+ if (toValidate === true) {
58
+
59
+ const errorText = [];
60
+ let isValid = true;
61
+ // increment counter
62
+ checkedTotal += 1;
63
+
64
+ if (typeof frontMatter["isFragment"] !== "undefined") {
65
+
66
+ pluginconfig["isFragment"] = frontMatter["isFragment"];
67
+ }
68
+
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
+ };
75
+
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) {
92
+
93
+ result["messages"].forEach((item, index) => {
94
+
95
+ errorText.push(`${index+1}. ${item.message} [Line ${item.lastLine}, Column ${item.lastColumn}]`);
96
+ });
97
+
98
+ isValid = false;
99
+
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
+ // ];
105
+
106
+ // isValid = validText.some((el) => result.includes(el));
107
+ // message = result;
108
+ }
109
+
110
+ if (isValid === false) {
111
+
112
+ const message = errorText.join("\n");
113
+ counterError += 1;
114
+
115
+ toLog(`validation error āŒ\nšŸ“• Input: ${page["input"]}\nšŸ“˜ Output: ${page["output"]}\n${message}\n`, "error");
116
+ }
117
+ }
118
+
119
+ } catch (err) {
120
+
121
+ toLog(err, "error");
122
+ }
123
+ }
124
+ /* eslint-enable no-await-in-loop */
125
+
126
+ toLog(`${checkedTotal} pages checked`);
127
+
128
+ if (counterError > 0) {
129
+
130
+ toLog(`found ${counterError} page(s) with errors`, "error");
131
+
132
+ } else {
133
+
134
+ toLog("all your html files are valid!", "info");
135
+ }
136
+ };
137
+
138
+ module["exports"] = doValidation;