@saiballo/eleventy-plugin-svg-embed 1.0.0 → 1.0.2

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
@@ -45,7 +45,7 @@ module.exports = function(eleventyConfig) {
45
45
  #### Parameters
46
46
  ```js
47
47
  // set this to true if you want to include file extension is shortcut code
48
- "includeExt": false,
48
+ "includeExt": false
49
49
 
50
50
  // true | false. if set to "true", the code will be embedded in the page; otherwise, the img src tag will be used
51
51
  "embed": true
@@ -56,7 +56,7 @@ module.exports = function(eleventyConfig) {
56
56
 
57
57
  ## Usage
58
58
 
59
- In your template you can add a shortcut (example is for Nunjucks);
59
+ In your template you can add a shortcut (the example below is for Nunjucks):
60
60
 
61
61
  ```js
62
62
  {% svgImg "path/to/your/image/logo" %}
@@ -65,15 +65,24 @@ In your template you can add a shortcut (example is for Nunjucks);
65
65
  For every image you can add some data (i.e. accessibility)
66
66
 
67
67
  ```js
68
- {% svgImg "path/to/your/image/logo", "css-class another-css-class", {embed: true, title: "svg title", desc: "svg desc", size:[500,500]} %}
68
+ {% svgImg "path/to/your/image/logo", "css-class another-css-class", {embed: true, title: "svg title", desc: "svg desc", size:[500,500], stroke: "grey", fill: [["#ff0000", "#999"], ["#19450E", "#333"]], "stroke-width": [[2,1]] } %}
69
69
  ```
70
70
 
71
- As you can see you can add css class list as second parameter of the shortcut. The third parameter is a object with which you can customize (all keys are optionals):
71
+ As you can see you can add css class list as second parameter of the shortcut.
72
72
 
73
- - embed: overwrite the embed option in plugin config
74
- - title: for accessibility purposes you can provide a title
75
- - desc: for accessibility purposes you can provide a desc
76
- - size: an array for width and height of the image (or you can provide only one dimension for both. i.e. [100])
73
+ The third parameter is a object with which you can customize some data and colors too (all keys are optionals):
74
+
75
+ - **embed**: overwrite the embed option in plugin config
76
+ - **title**: for accessibility purposes you can provide a title
77
+ - **desc**: for accessibility purposes you can provide a desc
78
+ - **size**: an array for width and height of the image (or you can provide only one dimension for both. i.e. [100])
79
+ - **stroke**: you can provide one value (named or hex code) or an array. In the first case, all the "stroke" attributes found in the paths will be replaced. In the second case, the first value of each array will be searched in the "stroke" attributes and replaced with the second value of the same array.
80
+ - **fill**: you can provide one value (named or hex code) or an array. In the first case, all the "fill" attributes found in the paths will be replaced. In the second case, the first value of each array will be searched in the "fill" attributes and replaced with the second value of the same array.
81
+ - **stroke-width**: you can provide one value or an array. In the first case, all the "stroke-width" attributes found in the paths will be replaced. In the second case, the first value of each array will be searched in the "stroke-width" attributes and replaced with the second value of the same array.
82
+
83
+ ## Changelog
84
+ ### 1.0.1
85
+ - Added support for the color attributes stroke, fill, and stroke-width
77
86
 
78
87
  ## Team ARMADA 429
79
88
  <img src="https://raw.githubusercontent.com/saiballo/saiballo/refs/heads/master/armada429.png" width="100" height="100">
package/jsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "compilerOptions": {
3
+ "checkJs": false
4
+ },
5
+ "exclude": [
6
+ "node_modules"
7
+ ]
8
+ }
package/main.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 07/12/2024 (18:32:24)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 14/12/2024 (18:13:32)
8
+ * Last update: 22/12/2024 (13:01:34)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saiballo/eleventy-plugin-svg-embed",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A plugin for 11ty that, through a shortcut, allows you to include an SVG image either as an embed or as a file",
5
5
  "main": "main.js",
6
6
  "keywords": [
package/src/getSvg.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 07/12/2024 (18:41:03)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 15/12/2024 (19:20:47)
8
+ * Last update: 22/12/2024 (18:08:23)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -19,6 +19,11 @@ const cheerio = require("cheerio");
19
19
  const toLog = require("./log.js");
20
20
  const util = require("./utils.js");
21
21
 
22
+ /**
23
+ * The function `getSvg` adds a shortcode `svgImg` to Eleventy configuration for embedding or displaying SVG images with specified attributes and error handling.
24
+ * @param eleventyconfig - it is an object that contains configuration settings for the Eleventy static site generator
25
+ * @param pluginconfig - it is an object containing configuration options for the plugin
26
+ */
22
27
  const getSvg = (eleventyconfig, pluginconfig) => {
23
28
 
24
29
  eleventyconfig.addShortcode("svgImg", (src, classlist = "", customdata = {}) => {
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: 07/12/2024 (21:02:35)
8
+ * Last update: 22/12/2024 (18:07:31)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -15,6 +15,11 @@
15
15
 
16
16
  "use strict";
17
17
 
18
+ /**
19
+ * The function `toLog` logs messages with different icons based on the type of message.
20
+ * @param [message] - it is a string that represents the message to be logged. If no message is provided, the default message "OPS! An error has occurred" will be used.
21
+ * @param [type=default] - it specifies the type of log message being displayed. It can have one of the following values: info, error
22
+ */
18
23
  const toLog = (message = "OPS! An error has occured", type = "default") => {
19
24
 
20
25
  let logIcon;
package/src/utils.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Created: 08/12/2024 (20:07:10)
6
6
  * Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
7
7
  *
8
- * Last update: 15/12/2024 (19:21:40)
8
+ * Last update: 22/12/2024 (18:09:02)
9
9
  * Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
10
10
  *
11
11
  * Copyleft: 2024 - Tutti i diritti riservati
@@ -15,13 +15,72 @@
15
15
 
16
16
  "use strict";
17
17
 
18
+ /**
19
+ * The `addAttribute` function modifies attributes of SVG images based on custom data and class list provided.
20
+ * @param classlist - it is used to specify one or more CSS classes that you want to add to the selected element.
21
+ * @param customdata - it is an object containing custom attributes and values that you want to add to the SVG or image element.
22
+ * It can include attributes like `fill`, `stroke`, `desc`, `id`, and `title`. The function `addAttribute` processes this data and updates the SVG or image
23
+ * @param $ - it is typically used as a reference to the jQuery library. It allows you to manipulate and interact with elements in the DOM using jQuery methods
24
+ * @param [tag=svg] - it specifies the type of element to which the attributes will be added. By default, it is set to "svg"
25
+ * @returns The function `addAttribute` returns `true` if a description (`desc`) is successfully added to the SVG element, otherwise it does not explicitly return anything.
26
+ */
18
27
  const addAttribute = (classlist, customdata, $, tag = "svg") => {
19
28
 
29
+ // only svg images
20
30
  if (tag === "svg") {
21
31
 
32
+ // role
22
33
  $(tag).prop("role", "img");
34
+
35
+ // fill and stroke
36
+ ["fill", "stroke", "stroke-width"].forEach((attribute) => {
37
+
38
+ if (customdata[attribute]) {
39
+
40
+ const option = customdata[attribute];
41
+
42
+ if (Array.isArray(option) === false) {
43
+
44
+ $(tag).find("path").attr(attribute, option);
45
+
46
+ } else {
47
+
48
+ $(tag).find("path").each(function() {
49
+
50
+ option.forEach(([sourceColor, targetColor]) => {
51
+
52
+ if (String($(this).attr(attribute)).toLowerCase() === String(sourceColor).toLowerCase()) {
53
+
54
+ $(this).attr(attribute, targetColor);
55
+ }
56
+ });
57
+ });
58
+ }
59
+ }
60
+ });
61
+
62
+ // desc
63
+ if (customdata["desc"] && customdata["desc"].trim() !== "") {
64
+
65
+ if ($(tag).children("desc").length > 0) {
66
+
67
+ $(tag).children("desc").text(customdata["desc"]);
68
+
69
+ return true;
70
+ }
71
+
72
+ if ($(tag).children("title").length > 0) {
73
+
74
+ $(tag).children("title").after(`<desc>${customdata["desc"]}</desc>`);
75
+
76
+ } else {
77
+
78
+ $(tag).prepend(`<desc>${customdata["desc"]}</desc>`);
79
+ }
80
+ }
23
81
  }
24
82
 
83
+ // all images type
25
84
  if (classlist) {
26
85
 
27
86
  $(tag).addClass(classlist);
@@ -50,27 +109,15 @@ const addAttribute = (classlist, customdata, $, tag = "svg") => {
50
109
  break;
51
110
  }
52
111
  }
53
-
54
- if (tag === "svg" && customdata["desc"] && customdata["desc"].trim() !== "") {
55
-
56
- if ($(tag).children("desc").length > 0) {
57
-
58
- $(tag).children("desc").text(customdata["desc"]);
59
-
60
- return true;
61
- }
62
-
63
- if ($(tag).children("title").length > 0) {
64
-
65
- $(tag).children("title").after(`<desc>${customdata["desc"]}</desc>`);
66
-
67
- } else {
68
-
69
- $(tag).prepend(`<desc>${customdata["desc"]}</desc>`);
70
- }
71
- }
72
112
  };
73
113
 
114
+ /**
115
+ * The `addSize` function sets the width and height of a specified HTML element using custom or default sizes.
116
+ * @param customsize - Customsize is an array that represents the width and height values for resizing an element.
117
+ * @param $ - The `$` parameter is typically used to represent the jQuery library in JavaScript. It is commonly used for DOM manipulation and traversal.
118
+ * @param defaultsize - it represents the default size value that will be used if a custom size is not provided or if the custom size provided is invalid.
119
+ * @param [tag=svg] - it is used to specify the HTML tag name that you want to target for setting the width and height properties. By default it is set to "svg"
120
+ */
74
121
  const addSize = (customsize, $, defaultsize, tag = "svg") => {
75
122
 
76
123
  if (customsize && customsize.length > 0) {