Skip to content

Commit

Permalink
add 'lazy: false' in listing items to control lazy loading of images.…
Browse files Browse the repository at this point in the history
… true by default
  • Loading branch information
cscheid committed Apr 22, 2024
1 parent 84704e6 commit 4c937c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/project/types/website/listing/website-listing-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export const kImageAlign = "image-align";
// Alt text for the item's image
export const kImageAlt = "image-alt";

// Lazy loading for the item's image. If unset, the default is true.
export const kImageLazyLoading = "image-lazy-loading";

// The placeholder image for the item
export const kImagePlaceholder = "image-placeholder";

Expand Down Expand Up @@ -211,6 +214,7 @@ export interface ListingItem extends Record<string, unknown> {
date?: Date;
image?: string;
[kImageAlt]?: string;
[kImageLazyLoading]?: boolean;
path?: string;
filename?: string;
[kFieldFileModified]?: Date;
Expand Down
7 changes: 4 additions & 3 deletions src/project/types/website/listing/website-listing-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function templateMarkdownHandler(
// For file modified specifically, include the time portion
const includeTime = field === kFieldFileModified;

const date = typeof (dateRaw) === "string"
const date = typeof dateRaw === "string"
? parsePandocDate(dateRaw as string)
: dateRaw as Date;
if (date) {
Expand Down Expand Up @@ -422,6 +422,7 @@ export function reshapeListing(
src: string,
classes: string,
alt?: string,
lazy?: boolean,
) => {
const pageSize = listing[kPageSize];
const classAttr = classes ? `class="${classes}"` : "";
Expand All @@ -430,8 +431,8 @@ export function reshapeListing(
: "";
const altAttr = alt ? `alt="${encodeAttributeValue(alt)}"` : "";
const srcAttr = itemNumber > pageSize ? "data-src" : "src";

return `<img ${srcAttr}="${src}" ${classAttr} ${styleAttr} ${altAttr}>`;
const lazyAttr = lazy === false ? "" : "loading='lazy' ";
return `<img ${lazyAttr}${srcAttr}="${src}" ${classAttr} ${styleAttr} ${altAttr}>`;
};
utilities.imgPlaceholder = (
itemNumber: number,
Expand Down

0 comments on commit 4c937c5

Please sign in to comment.