Skip to content

Commit ab90d7c

Browse files
authored
Add image options (#16)
* Add image lazy loading option * Rename image caption option
1 parent 10d9aa0 commit ab90d7c

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

index.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ function parseCaption(
7878

7979
export function parseImage(
8080
html: string,
81-
{ getFullCaption }: { getFullCaption?: boolean } = { getFullCaption: false }
81+
{
82+
getFullCaption,
83+
useImageLazyLoading,
84+
}: { getFullCaption?: boolean; useImageLazyLoading?: boolean } = {
85+
getFullCaption: false,
86+
useImageLazyLoading: true,
87+
}
8288
): Image {
8389
const element = parse(html).querySelector("img")!;
90+
8491
return {
85-
html: element.toString(),
92+
html: (useImageLazyLoading
93+
? element
94+
: element.removeAttribute("loading")
95+
).toString(),
8696
caption: parseCaption(
8797
parse(element.getAttribute("data-image-caption") ?? "").querySelector(
8898
"p"
@@ -124,21 +134,26 @@ export function joinListOfStrings(strings: string[]) {
124134
.join(", ")} and ${deduplicatedStrings.at(-1)}`;
125135
}
126136

127-
type ImageOptions = { getFullCaption?: boolean; useCache?: boolean };
137+
type ImageOptions = {
138+
getFullCaption?: boolean;
139+
useCache?: boolean;
140+
useLazyLoading?: boolean;
141+
};
128142

129143
export async function fetchImageFromSlug(
130144
slug: string,
131-
{ getFullCaption, useCache }: ImageOptions = {
145+
options: ImageOptions = {
132146
getFullCaption: false,
133147
useCache: true,
148+
useLazyLoading: true,
134149
}
135150
) {
136151
const url = new URL("https://michigandaily.com/wp-json/wp/v2/media");
137152
url.searchParams.set("media_type", "image");
138153
url.searchParams.set("slug", slug);
139154
url.searchParams.set("_fields", "description");
140155

141-
if (!useCache) {
156+
if (!options.useCache) {
142157
url.searchParams.set("time", new Date().toISOString());
143158
}
144159

@@ -161,14 +176,18 @@ export async function fetchImageFromSlug(
161176
return null;
162177
}
163178

164-
return parseImage(image.description.rendered, { getFullCaption });
179+
return parseImage(image.description.rendered, {
180+
getFullCaption: options.getFullCaption,
181+
useImageLazyLoading: options.useLazyLoading,
182+
});
165183
}
166184

167185
export async function fetchImageFromUrl(
168186
url: string,
169187
options: ImageOptions = {
170188
getFullCaption: false,
171189
useCache: true,
190+
useLazyLoading: true,
172191
}
173192
) {
174193
const slug = parseSlugFromUrl(url);
@@ -179,26 +198,28 @@ type PostOptions = {
179198
useTestSite?: boolean;
180199
useCache?: boolean;
181200
getImage?: boolean;
182-
getFullImageCaption?: boolean;
201+
getImageFullCaption?: boolean;
202+
useImageLazyLoading?: boolean;
183203
};
184204

185205
export async function fetchPostFromSlug(
186206
slug: string,
187-
{ useTestSite, useCache, getImage, getFullImageCaption }: PostOptions = {
207+
options: PostOptions = {
188208
useTestSite: false,
189209
useCache: true,
190210
getImage: true,
191-
getFullImageCaption: false,
211+
getImageFullCaption: false,
212+
useImageLazyLoading: true,
192213
}
193214
) {
194215
const url = new URL(
195-
useTestSite
216+
options.useTestSite
196217
? "https://md-clone.newspackstaging.com/wp-json/wp/v2/posts"
197218
: "https://michigandaily.com/wp-json/wp/v2/posts"
198219
);
199220
url.searchParams.set("slug", slug);
200221
url.searchParams.set("_fields", "coauthors,link,title,_links");
201-
if (!useCache) {
222+
if (!options.useCache) {
202223
url.searchParams.set("time", new Date().toISOString());
203224
}
204225

@@ -214,20 +235,21 @@ export async function fetchPostFromSlug(
214235

215236
const story = data.at(0);
216237

217-
if (getImage) {
238+
if (options.getImage) {
218239
const [feature] = story._links["wp:featuredmedia"];
219240

220241
const params = new URLSearchParams();
221242
params.set("_fields", "description");
222243

223-
if (!useCache) {
244+
if (!options.useCache) {
224245
params.set("time", new Date().toISOString());
225246
}
226247

227248
const imageRequest = await fetch(`${feature.href}?${params.toString()}`);
228249
const imageData = await imageRequest.json();
229250
const image = parseImage(imageData.description.rendered, {
230-
getFullCaption: getFullImageCaption,
251+
getFullCaption: options.getImageFullCaption,
252+
useImageLazyLoading: options.useImageLazyLoading,
231253
});
232254

233255
return {
@@ -255,7 +277,8 @@ export async function fetchPostFromUrl(
255277
useTestSite: false,
256278
useCache: true,
257279
getImage: true,
258-
getFullImageCaption: false,
280+
getImageFullCaption: false,
281+
useImageLazyLoading: true,
259282
}
260283
) {
261284
const slug = parseSlugFromUrl(url);

0 commit comments

Comments
 (0)