forked from 11ty/eleventy-img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-html.js
157 lines (132 loc) · 4.81 KB
/
generate-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
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: "" } },
]}
*/
function generateObject(metadata, attributes = {}, options = {}) {
attributes = Object.assign({}, 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}`);
}
let formats = Object.keys(metadata);
let values = Object.values(metadata);
let entryCount = 0;
for(let 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(let format of LOWSRC_FORMAT_PREFERENCE) {
if((format in metadata) && metadata[format].length) {
lowsrcFormat = format;
lowsrc = metadata[lowsrcFormat];
break;
}
}
// Handle if empty intersection between format and LOWSRC_FORMAT_PREFERENCE (e.g. gif)
// If there’s only one format in the results, use that
if(!lowsrc && formats.length === 1) {
lowsrcFormat = formats[0];
lowsrc = metadata[lowsrcFormat];
}
if(!lowsrc || !lowsrc.length) {
throw new Error(`Could not find the lowest <img> source for responsive markup for ${originalSrc}`);
}
attributes.src = lowsrc[0].url;
attributes.width = lowsrc[lowsrc.length - 1].width;
attributes.height = lowsrc[lowsrc.length - 1].height;
let attributesWithoutSizes = Object.assign({}, attributes);
delete attributesWithoutSizes.sizes;
// <img>: one format and one size
if(entryCount === 1) {
return {
"img": attributesWithoutSizes
};
}
let 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 srcset is using the `w` unit
// https://html.spec.whatwg.org/dev/semantics.html#the-link-element:attr-link-imagesrcset-4
// Using the default "100vw" is okay
throw new Error(missingSizesErrorMessage);
}
let imgAttributes = Object.assign({}, attributesWithoutSizes);
let srcsetAttrValue = Object.values(lowsrc).map(entry => entry.srcset).join(", ");
imgAttributes.srcset = srcsetAttrValue;
imgAttributes.sizes = attributes.sizes;
return {
img: imgAttributes
};
}
let 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 srcset is using the `w` unit
// https://html.spec.whatwg.org/dev/semantics.html#the-link-element:attr-link-imagesrcset-4
// Using the default "100vw" is okay
throw new Error(missingSizesErrorMessage);
}
let sourceAttrs = {
type: imageFormat[0].sourceType,
srcset: imageFormat.map(entry => entry.srcset).join(", "),
};
if(attributes.sizes) {
sourceAttrs.sizes = attributes.sizes;
}
children.push({
"source": sourceAttrs
});
});
children.push({
"img": attributesWithoutSizes
});
return {
"picture": children
};
}
function mapObjectToHTML(tagName, attrs = {}) {
let attrHtml = Object.entries(attrs).map(entry => {
let [key, value] = entry;
return `${key}="${value}"`;
}).join(" ");
return `<${tagName}${attrHtml ? ` ${attrHtml}` : ""}>`;
}
function generateHTML(metadata, attributes = {}, options = {}) {
let isInline = options.whitespaceMode !== "block";
let markup = [];
let obj = generateObject(metadata, attributes, options);
for(let tag in obj) {
if(!Array.isArray(obj[tag])) {
markup.push(mapObjectToHTML(tag, obj[tag]));
} else {
markup.push(`<${tag}>`);
for(let child of obj[tag]) {
let childTagName = Object.keys(child)[0];
markup.push((!isInline ? " " : "") + mapObjectToHTML(childTagName, child[childTagName]));
}
markup.push(`</${tag}>`);
}
}
return markup.join(!isInline ? "\n" : "");
}
module.exports = generateHTML;
module.exports.generateObject = generateObject;