-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate-image-html.js
183 lines (157 loc) · 5.06 KB
/
generate-image-html.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const omit = require('lodash').omit;
const DEFAULT_ATTRIBUTES = {
// loading: "lazy",
// decoding: "async",
};
const LOWSRC_FORMAT_PREFERENCE = ['jpeg', 'png', 'svg', 'webp', 'avif'];
/*
Returns:
e.g. { img: { alt: "", src: "" }
e.g. { picture: [
{ source: { srcset: "", sizes: "" } },
{ source: { srcset: "", sizes: "" } },
{ img: { alt: "", src: "" } },
]}
*/
// eslint-disable-next-line complexity,sonarjs/cognitive-complexity,max-statements
function generateObject(metadata, attributes = {}) {
attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
// The attributes.src gets overwritten later on. Save it here to make the error outputs less cryptic.
const originalSrc = attributes.src;
if (attributes.alt === undefined) {
// You bet we throw an error on missing alt (alt="" works okay)
throw new Error(
`Missing \`alt\` attribute on eleventy-img shortcode from: ${originalSrc}`
);
}
const formats = Object.keys(metadata);
const values = Object.values(metadata);
let entryCount = 0;
for (const imageFormat of values) {
entryCount += imageFormat.length;
}
if (entryCount === 0) {
throw new Error(
'No image results found from `eleventy-img` in generateHTML. Expects a results object similar to: https://www.11ty.dev/docs/plugins/image/#usage.'
);
}
let lowsrc;
let lowsrcFormat;
for (const format of LOWSRC_FORMAT_PREFERENCE) {
if (format in metadata && metadata[format].length) {
lowsrcFormat = format;
lowsrc = metadata[lowsrcFormat];
break;
}
}
if (!lowsrc || !lowsrc.length) {
throw new Error(
`Could not find the lowest <img> source for responsive markup for ${originalSrc}`
);
}
attributes.src = `${process.env.STATIC_PATH ? process.env.STATIC_PATH : ''}${
lowsrc[0].url
}`;
attributes.width = lowsrc[lowsrc.length - 1].width;
attributes.height = lowsrc[lowsrc.length - 1].height;
const attributesWithoutSizes = { ...attributes };
delete attributesWithoutSizes.sizes;
// <img>: one format and one size
if (entryCount === 1) {
return { img: attributesWithoutSizes };
}
const missingSizesErrorMessage = `Missing \`sizes\` attribute on eleventy-img shortcode from: ${
originalSrc || attributes.src
}`;
// <img srcset>: one format and multiple sizes
if (formats.length === 1) {
// implied entryCount > 1
if (entryCount > 1 && !attributes.sizes) {
// Per the HTML specification sizes is required when multiple sources are in srcset
// The default "100vw" is okay
throw new Error(missingSizesErrorMessage);
}
const imgAttributes = { ...attributesWithoutSizes };
const srcsetAttrValue = Object.values(lowsrc)
.map(
(entry) =>
`${process.env.STATIC_PATH ? process.env.STATIC_PATH : ''}${
entry.srcset
}`
)
.join(', ');
imgAttributes.srcset = srcsetAttrValue;
imgAttributes.sizes = attributes.sizes;
return { img: imgAttributes };
}
const children = [];
values
.filter((imageFormat) => {
return (
imageFormat.length > 0 &&
(lowsrcFormat !== imageFormat[0].format || imageFormat.length !== 1)
);
})
.forEach((imageFormat) => {
if (imageFormat.length > 1 && !attributes.sizes) {
// Per the HTML specification sizes is required when multiple sources are in srcset
// The default "100vw" is okay
throw new Error(missingSizesErrorMessage);
}
const sourceAttrs = {
type: imageFormat[0].sourceType,
srcset: imageFormat
.map(
(entry) =>
`${process.env.STATIC_PATH ? process.env.STATIC_PATH : ''}${
entry.srcset
}`
)
.join(', '),
};
if (attributes.sizes) {
sourceAttrs.sizes = attributes.sizes;
}
children.push({
source: sourceAttrs,
});
});
children.push({
img: omit(attributesWithoutSizes, ['width', 'height']),
});
return {
picture: children,
};
}
function mapObjectToHTML(tagName, attrs = {}) {
const attrHtml = Object.entries(attrs)
.map((entry) => {
const [key, value] = entry;
return `${key}="${value}"`;
})
.join(' ');
return `<${tagName}${attrHtml ? ` ${attrHtml}` : ''}>`;
}
function generateHTML(metadata, attributes = {}, options = {}) {
const isInline = options.whitespaceMode !== 'block';
const markup = [];
const { pictureClass, ...restAttributes } = attributes;
const obj = generateObject(metadata, restAttributes);
for (const tag in obj) {
if (!Array.isArray(obj[tag])) {
markup.push(mapObjectToHTML(tag, obj[tag]));
} else {
markup.push(`<${tag}${pictureClass ? ` class="${pictureClass}"` : ''}>`);
for (const child of obj[tag]) {
const childTagName = Object.keys(child)[0];
markup.push(
(!isInline ? ' ' : '') +
mapObjectToHTML(childTagName, child[childTagName])
);
}
markup.push(`</${tag}>`);
}
}
return markup.join(!isInline ? '\n' : '');
}
module.exports = generateHTML;