-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.eleventy.cjs
More file actions
140 lines (109 loc) · 4.89 KB
/
Copy path.eleventy.cjs
File metadata and controls
140 lines (109 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// CJS wrapper for backwards compatibility
// For new projects, use ESM: import plugin from 'eleventy-plugin-phosphoricons'
const fs = require('fs');
const path = require('path');
const { parseHTML } = require('linkedom');
const phosphorIcons = JSON.parse(fs.readFileSync(path.join(__dirname, 'icons.json'), 'utf8'));
const phosphorCorePath = path.join(
path.dirname(require.resolve("@phosphor-icons/core")),
"../assets"
);
module.exports = (eleventyConfig, attributes = {}) => {
const defaultAttributes = {
class: `phicon`,
render: `svg`,
loading: `lazy`,
decoding: `async`,
altPrefix: `icon`,
eleventyIgnore: true,
transformClass: false,
transformFill: undefined
}
const globalAttributes = { ...defaultAttributes, ...attributes };
const shortcodeHandler = (iconName, iconType = 'regular', attributesOrClasses = {}) => {
if (!iconName) {
throw new Error(
"[eleventy-plugin-phosphoricons] the iconName must be specified"
);
}
// Handle 3rd arg: string = extra classes, object = attributes
let attributes = {};
if (typeof attributesOrClasses === 'string' && attributesOrClasses.trim()) {
const defaultClass = globalAttributes.class || '';
attributes.class = defaultClass ? `${defaultClass} ${attributesOrClasses}` : attributesOrClasses;
} else if (typeof attributesOrClasses === 'object' && attributesOrClasses !== null) {
attributes = attributesOrClasses;
}
attributes = { ...globalAttributes, ...attributes };
attributes.render = attributes.render.toLowerCase();
if (!['regular', 'thin', 'light', 'bold', 'fill', 'duotone'].includes(iconType)) {
iconType = 'regular';
}
const fileName = iconType === 'regular' ? iconName : `${iconName}-${iconType}`;
if (!phosphorIcons[iconType].includes(fileName)) {
throw new Error(
`[eleventy-plugin-phosphoricons] the iconName "${iconName}" does not exist in the ${iconType} type`
);
}
if (!['svg', 'image', 'img'].includes(attributes.render)) {
attributes.render = 'svg';
console.warn(`[eleventy-plugin-phosphoricons] the render attribute must be one of the following: svg, image, img. Defaulting to svg.`);
}
if (typeof attributes.transformFill === 'function' && attributes.transformClass) {
try {
attributes.fill = attributes.transformFill(attributes.transformClass);
} catch (error) {
console.warn(`[eleventy-plugin-phosphoricons] the transformFill function failed: ${error}`);
console.warn(`[eleventy-plugin-phosphoricons] the attributes.transformClass will be ignored`);
}
}
const svgContent = fs.readFileSync(
path.join(phosphorCorePath, `./${iconType}/${fileName}.svg`),
"utf8"
);
const { document } = parseHTML(svgContent);
const svgIcon = document.querySelector('svg');
if (attributes.render === 'svg') {
if (attributes.class) {
svgIcon.setAttribute('class', attributes.class);
}
svgIcon.setAttribute('aria-hidden', 'true');
}
if (attributes.style) {
svgIcon.setAttribute('style', attributes.style);
}
if (attributes.size) {
svgIcon.setAttribute('width', attributes.size);
svgIcon.setAttribute('height', attributes.size);
}
if (attributes.fill) {
svgIcon.setAttribute('fill', attributes.fill);
}
let iconString = svgIcon.outerHTML;
if (['image', 'img'].includes(attributes.render)) {
let defaultAltText = attributes.altPrefix ? [attributes.altPrefix, iconName] : [iconName];
let imageAttributes = {
src: `data:image/svg+xml,${encodeURIComponent(iconString)}`,
width: attributes.size,
height: attributes.size,
loading: attributes.loading,
decoding: attributes.decoding,
alt: attributes.alt || defaultAltText.join(' '),
"aria-hidden": true,
};
if (attributes.eleventyIgnore) {
imageAttributes["eleventy:ignore"] = true;
}
if (attributes.style) {
imageAttributes.style = attributes.style;
}
if (attributes.class) {
imageAttributes.class = attributes.class;
}
iconString = `<img ${Object.keys(imageAttributes).map(key => `${key}="${imageAttributes[key]}"`).join(' ')} />`;
}
return iconString.trim();
};
eleventyConfig.addShortcode("phosphor", shortcodeHandler);
eleventyConfig.addShortcode("phicon", shortcodeHandler);
};