@saiballo/eleventy-plugin-svg-embed 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 +85 -0
- package/main.js +44 -0
- package/package.json +38 -0
- package/sample-img/logo.png +0 -0
- package/sample-img/logo.svg +1 -0
- package/src/getSvg.js +91 -0
- package/src/log.js +39 -0
- package/src/utils.js +89 -0
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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-svg-embed
|
|
6
|
+
|
|
7
|
+
> A plugin for 11ty that, through a shortcut, allows you to include an SVG image either as an embed or as a file
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
[](https://lbesson.mit-license.org/)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Available on [npm](https://www.npmjs.com/package/@saiballo/eleventy-plugin-svg-embed):
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @saiballo/eleventy-plugin-svg-embed --save
|
|
19
|
+
```
|
|
20
|
+
Add the plugin to your `eleventy.config.js`:
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const svgImg = require("@saiballo/eleventy-plugin-svg-embed");
|
|
24
|
+
|
|
25
|
+
module.exports = function(eleventyConfig) {
|
|
26
|
+
|
|
27
|
+
eleventyConfig.addPlugin(svgImg);
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### Options Parameters
|
|
32
|
+
You can add an options object to the plugin configuration. Below is the complete list of available parameters.
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
module.exports = function(eleventyConfig) {
|
|
36
|
+
|
|
37
|
+
eleventyConfig.addPlugin(svgImg, {
|
|
38
|
+
"includeExt": false,
|
|
39
|
+
"embed": true,
|
|
40
|
+
"defaultSize": 100
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Parameters
|
|
46
|
+
```js
|
|
47
|
+
// set this to true if you want to include file extension is shortcut code
|
|
48
|
+
"includeExt": false,
|
|
49
|
+
|
|
50
|
+
// true | false. if set to "true", the code will be embedded in the page; otherwise, the img src tag will be used
|
|
51
|
+
"embed": true
|
|
52
|
+
|
|
53
|
+
// default size of image if you don't use a css class or declare sizes
|
|
54
|
+
"defaultSize": 100
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
In your template you can add a shortcut (example is for Nunjucks);
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
{% svgImg "path/to/your/image/logo" %}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For every image you can add some data (i.e. accessibility)
|
|
66
|
+
|
|
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]} %}
|
|
69
|
+
```
|
|
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):
|
|
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])
|
|
77
|
+
|
|
78
|
+
## Team ARMADA 429
|
|
79
|
+
<img src="https://raw.githubusercontent.com/saiballo/saiballo/refs/heads/master/armada429.png" width="100" height="100">
|
|
80
|
+
|
|
81
|
+
* Lorenzo "Saibal" Forti
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+

|
package/main.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @preserve
|
|
3
|
+
* Filename: main.js
|
|
4
|
+
*
|
|
5
|
+
* Created: 07/12/2024 (18:32:24)
|
|
6
|
+
* Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
7
|
+
*
|
|
8
|
+
* Last update: 14/12/2024 (18:13:32)
|
|
9
|
+
* Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
10
|
+
*
|
|
11
|
+
* Copyleft: 2024 - Tutti i diritti riservati
|
|
12
|
+
*
|
|
13
|
+
* Comments:
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const getSvg = require("./src/getSvg.js");
|
|
17
|
+
const pkg = require("./package.json");
|
|
18
|
+
const toLog = require("./src/log.js");
|
|
19
|
+
|
|
20
|
+
const defaultConfig = {
|
|
21
|
+
"includeExt": false,
|
|
22
|
+
"embed": true,
|
|
23
|
+
"defaultSize": 100
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
module.exports = (eleventyconfig, config = {}) => {
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
|
|
30
|
+
// merge defaultConfig with custom config
|
|
31
|
+
const pluginConfig = {
|
|
32
|
+
...defaultConfig,
|
|
33
|
+
...config
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
eleventyconfig.versionCheck(pkg["11ty"].compatibility);
|
|
37
|
+
|
|
38
|
+
getSvg(eleventyconfig, pluginConfig);
|
|
39
|
+
|
|
40
|
+
} catch (err) {
|
|
41
|
+
|
|
42
|
+
toLog(err, "error");
|
|
43
|
+
}
|
|
44
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@saiballo/eleventy-plugin-svg-embed",
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
"main": "main.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"11ty",
|
|
8
|
+
"eleventy",
|
|
9
|
+
"eleventy-plugin",
|
|
10
|
+
"embed svg"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": {
|
|
14
|
+
"name": "Lorenzo \"Saibal\" Forti",
|
|
15
|
+
"email": "lorenzo.forti@gmail.com"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/saiballo/eleventy-plugin-svg-embed#readme",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/saiballo/eleventy-plugin-svg-embed.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/saiballo/eleventy-plugin-svg-embed"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=16"
|
|
27
|
+
},
|
|
28
|
+
"11ty": {
|
|
29
|
+
"compatibility": ">=2.0.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"refresh": "rm -rf node_modules && rm package-lock.json && npm install"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"cheerio": "^1.0.0",
|
|
36
|
+
"mime-types": "^2.1.35"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg height="200" viewBox="0 0 15694 15694" width="200" xmlns="http://www.w3.org/2000/svg"><rect fill="#222" height="100%" width="100%"/><path d="m5622 11381c-90 0-135-120-135-361v-5951c0-115-23-169-70-162l-431 108c-79 7-118-72-118-237v-517c0-143 43-224 129-242l1422-366c11-3 27-5 48-5 79 0 118 84 118 253v7120c0 241-47 361-140 361zm3683 11c-144 0-268-10-374-30s-216-65-331-135-209-166-283-288-134-293-180-512-70-479-70-781v-2762c0-72-20-108-59-108h-334c-90 0-135-86-135-258v-291c0-176 45-264 135-264h334c39 0 59-48 59-145l97-2095c11-190 57-285 140-285h539c90 0 135 95 135 285v2095c0 97 21 145 65 145h687c90 0 135 88 135 264v291c0 172-45 258-135 258h-689c-25 0-42 6-51 19-9 12-13 42-13 89v2779c0 208 13 382 40 520s66 240 118 304 104 108 156 129c52 22 116 32 191 32h382c97 0 145 67 145 199v323c0 147-52 221-156 221zm2067 646c82 0 154-67 215-202s92-326 92-574c0-58-36-257-108-598l-1056-4389c-7-50-11-90-11-119 0-129 27-194 81-194h652c50 0 91 17 124 51s58 103 75 207l700 3705c14 43 23 65 27 65 14 0 22-20 22-59l549-3695c14-108 37-180 67-218s69-57 116-57h452c61 0 92 70 92 210 0 32-4 74-11 124l-959 4993c-75 413-158 729-248 948s-190 368-302 447a776 776 0 0 1 -442 124h-54c-291 0-488-77-592-232-29-32-43-115-43-248 0-266 43-399 129-399 7 0 72 18 194 54 124 38 203 56 239 56zm-8460-647c-90 0-135-120-135-361v-5951c0-115-23-169-70-162l-431 108c-79 7-118-72-118-237v-517c0-143 43-224 129-242l1423-367c11-3 27-5 48-5 79 0 118 84 118 253v7120c0 241-47 361-140 361z" fill="#fff" stroke="#fff" stroke-miterlimit="10" stroke-width="280"/></svg>
|
package/src/getSvg.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @preserve
|
|
3
|
+
* Filename: getSvg.js
|
|
4
|
+
*
|
|
5
|
+
* Created: 07/12/2024 (18:41:03)
|
|
6
|
+
* Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
7
|
+
*
|
|
8
|
+
* Last update: 15/12/2024 (19:20:47)
|
|
9
|
+
* Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
10
|
+
*
|
|
11
|
+
* Copyleft: 2024 - Tutti i diritti riservati
|
|
12
|
+
*
|
|
13
|
+
* Comments:
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const fs = require("node:fs");
|
|
17
|
+
const mime = require("mime-types");
|
|
18
|
+
const cheerio = require("cheerio");
|
|
19
|
+
const toLog = require("./log.js");
|
|
20
|
+
const util = require("./utils.js");
|
|
21
|
+
|
|
22
|
+
const getSvg = (eleventyconfig, pluginconfig) => {
|
|
23
|
+
|
|
24
|
+
eleventyconfig.addShortcode("svgImg", (src, classlist = "", customdata = {}) => {
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
|
|
28
|
+
const isEmbed = customdata["embed"] ?? pluginconfig["embed"];
|
|
29
|
+
const imgSrc = pluginconfig["includeExt"] === true ? src : `${src}.svg`;
|
|
30
|
+
const mimeType = mime.lookup(imgSrc);
|
|
31
|
+
|
|
32
|
+
if (mimeType === false) {
|
|
33
|
+
|
|
34
|
+
throw new Error(`not image provided: ${imgSrc}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (isEmbed === true) {
|
|
38
|
+
|
|
39
|
+
if (fs.existsSync(imgSrc) === false) {
|
|
40
|
+
|
|
41
|
+
throw new Error(`file not found for embed request: ${imgSrc}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (mimeType.includes("svg+xml") === false) {
|
|
45
|
+
|
|
46
|
+
throw {
|
|
47
|
+
"message": `SVG embed requested but a non-SVG image was provided: ${imgSrc}`,
|
|
48
|
+
"code": 415
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const tag = "svg";
|
|
53
|
+
const svgContent = fs.readFileSync(imgSrc, {
|
|
54
|
+
"encoding": "utf8"
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const $ = cheerio.load(svgContent, {
|
|
58
|
+
"xmlMode": true
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
util.addAttribute(classlist, customdata, $, tag);
|
|
62
|
+
|
|
63
|
+
util.addSize(customdata["size"], $, pluginconfig["defaultSize"], tag);
|
|
64
|
+
|
|
65
|
+
return $.html(tag);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const tag = "img";
|
|
69
|
+
const $ = cheerio.load(`<img src="${imgSrc}" alt="">`, "", false);
|
|
70
|
+
|
|
71
|
+
util.addAttribute(classlist, customdata, $, tag);
|
|
72
|
+
|
|
73
|
+
util.addSize(customdata["size"], $, pluginconfig["defaultSize"], tag);
|
|
74
|
+
|
|
75
|
+
return $.html();
|
|
76
|
+
|
|
77
|
+
} catch (err) {
|
|
78
|
+
|
|
79
|
+
toLog(`${err["message"]}`, "error");
|
|
80
|
+
|
|
81
|
+
switch (err["code"]) {
|
|
82
|
+
case 415:
|
|
83
|
+
return `[can't embed a non-SVG image: ${src}]`;
|
|
84
|
+
default:
|
|
85
|
+
return `[can't render image: ${src}]`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
module["exports"] = getSvg;
|
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: 07/12/2024 (21:02:35)
|
|
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-svg-embed -`;
|
|
35
|
+
|
|
36
|
+
console.error(`${logLabel} ${message}`);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
module["exports"] = toLog;
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @preserve
|
|
3
|
+
* Filename: utils.js
|
|
4
|
+
*
|
|
5
|
+
* Created: 08/12/2024 (20:07:10)
|
|
6
|
+
* Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
7
|
+
*
|
|
8
|
+
* Last update: 15/12/2024 (19:21:40)
|
|
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 addAttribute = (classlist, customdata, $, tag = "svg") => {
|
|
19
|
+
|
|
20
|
+
if (tag === "svg") {
|
|
21
|
+
|
|
22
|
+
$(tag).prop("role", "img");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (classlist) {
|
|
26
|
+
|
|
27
|
+
$(tag).addClass(classlist);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (customdata["id"]) {
|
|
31
|
+
|
|
32
|
+
$(tag).attr("id", customdata["id"]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (customdata["title"] && customdata["title"].trim() !== "") {
|
|
36
|
+
|
|
37
|
+
switch (tag) {
|
|
38
|
+
case "img":
|
|
39
|
+
$(tag).attr("alt", customdata["title"]);
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
if ($(tag).children("title").length > 0) {
|
|
43
|
+
|
|
44
|
+
$(tag).children("title").text(customdata["title"]);
|
|
45
|
+
|
|
46
|
+
} else {
|
|
47
|
+
|
|
48
|
+
$(tag).prepend(`<title>${customdata["title"]}</title>`);
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
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
|
+
};
|
|
73
|
+
|
|
74
|
+
const addSize = (customsize, $, defaultsize, tag = "svg") => {
|
|
75
|
+
|
|
76
|
+
if (customsize && customsize.length > 0) {
|
|
77
|
+
|
|
78
|
+
const width = customsize.length === 2 ? parseInt(customsize[0]) || defaultsize : parseInt(customsize[0]) || defaultsize;
|
|
79
|
+
const height = customsize.length === 2 ? parseInt(customsize[1]) || width : defaultsize;
|
|
80
|
+
|
|
81
|
+
$(tag).prop("width", width);
|
|
82
|
+
$(tag).prop("height", height);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
module["exports"] = {
|
|
87
|
+
addAttribute,
|
|
88
|
+
addSize
|
|
89
|
+
};
|