Skip to content

Commit

Permalink
enhance(author): handle missing featured images in work block
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed Mar 4, 2024
1 parent f4b3fd9 commit 771476e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
12 changes: 9 additions & 3 deletions db/model/Gdoc/GdocAuthor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
OwidEnrichedGdocBlock,
OwidGdocErrorMessageType,
DbEnrichedLatestWork,
DEFAULT_GDOC_FEATURED_IMAGE,
} from "@ourworldindata/utils"
import { GdocBase } from "./GdocBase.js"
import { htmlToEnrichedTextBlock } from "./htmlToEnriched.js"
Expand Down Expand Up @@ -49,10 +50,15 @@ export class GdocAuthor extends GdocBase implements OwidGdocAuthorInterface {
// until the author page is updated again.
const latestWorkImageFilenames = this.latestWorkLinks
.map((d) => d["featured-image"])
.filter(Boolean)
.filter(Boolean) as string[]

// Load the image metadata for the latest work images
return super.loadImageMetadata(latestWorkImageFilenames)
// Load the image metadata for the latest work images, including the
// default featured image which is used as a fallback in the entire
// research and writing block
return super.loadImageMetadata([
...latestWorkImageFilenames,
DEFAULT_GDOC_FEATURED_IMAGE,
])
}

_enrichSubclassContent = (content: Record<string, any>): void => {
Expand Down
4 changes: 2 additions & 2 deletions packages/@ourworldindata/types/src/domainTypes/Author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ export interface DbRawLatestWork {
slug: string
title: string
authors: string
"featured-image": string
"featured-image": string | null
publishedAt: Date | null
}

export interface DbEnrichedLatestWork {
slug: string
title: string
authors: string[]
"featured-image": string
"featured-image": string | null
publishedAt: Date | null
}

Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/types/src/gdocTypes/GdocConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export const gdocUrlRegex =
/https:\/\/docs\.google\.com\/document(?:\/u\/\d)?\/d\/([\-\w]+)\/?(edit)?#?/

export const gdocIdRegex = /^[0-9A-Za-z\-_]{44}$/

export const DEFAULT_GDOC_FEATURED_IMAGE = "default-featured-image.png"
1 change: 1 addition & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export {
IMAGES_DIRECTORY,
gdocUrlRegex,
gdocIdRegex,
DEFAULT_GDOC_FEATURED_IMAGE,
} from "./gdocTypes/GdocConstants.js"
export {
type OwidVariableWithSource,
Expand Down
11 changes: 9 additions & 2 deletions site/gdocs/components/ResearchAndWriting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { formatAuthors } from "../../clientFormatting.js"
import Image from "./Image.js"
import { AttachmentsContext, DocumentContext } from "../OwidGdoc.js"
import {
DEFAULT_GDOC_FEATURED_IMAGE,
DbEnrichedLatestWork,
RESEARCH_AND_WRITING_DEFAULT_HEADING,
} from "@ourworldindata/types"
Expand All @@ -29,7 +30,13 @@ function ResearchAndWritingLink(
}
) {
let {
value: { url, title, subtitle, authors, filename },
value: {
url,
title,
subtitle,
authors,
filename = DEFAULT_GDOC_FEATURED_IMAGE,
},
shouldHideThumbnail = false,
isSmall = false,
shouldHideSubtitle = false,
Expand Down Expand Up @@ -109,7 +116,7 @@ const parseLatestWorkToResearchAndWritingLink = (
value: {
...latestWork,
url: `${BAKED_BASE_URL}/${latestWork.slug}`,
filename: latestWork["featured-image"],
filename: latestWork["featured-image"] || undefined,
},
}
}
Expand Down

0 comments on commit 771476e

Please sign in to comment.