Skip to content

Commit

Permalink
SUP-107 Added metadata for book detail pages
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Apr 22, 2024
1 parent e2a445e commit 42ac381
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions app/[...slug]/metadata.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Maybe, NodeStanfordEvent, NodeStanfordNews, NodeStanfordPage, NodeStanfordPerson, NodeStanfordPolicy, NodeUnion, ParagraphStanfordWysiwyg, ParagraphUnion} from "@lib/gql/__generated__/drupal.d";
import {Maybe, NodeStanfordEvent, NodeStanfordNews, NodeStanfordPage, NodeStanfordPerson, NodeStanfordPolicy, NodeSupBook, NodeUnion, ParagraphStanfordWysiwyg, ParagraphUnion} from "@lib/gql/__generated__/drupal.d";
import {Metadata} from "next";
import {decode} from "html-entities";

Expand Down Expand Up @@ -37,11 +37,38 @@ export const getNodeMetadata = (node: NodeUnion): Metadata => {
...getPolicyMetaData(node),
...defaultData
}

case "NodeSupBook":
return {
...getBookMetaData(node),
...defaultData
}
}

return defaultData;
}

const getBookMetaData = (node: NodeSupBook) => {
const image = node.supBookImage?.mediaImage;
const description = getCleanDescription(node.supBookDescription?.processed);

return {
description: description,
openGraph: {
type: "book",
title: node.title,
isbn: node.supBookIsbn13Isw || node.supBookIsbn13Paper || node.supBookIsbn13Cloth || node.supBookIsbn13Alt,
authors: node.supBookAuthors?.map(author => ({
"profile:first_name": author.given,
"profile:last_name": author.family
})),
releaseDate: node.supBookPubYearFirst,
description: description,
images: image ? getOpenGraphImage(image.url, image.alt || "") : []
}
}
}

const getBasicPageMetaData = (node: NodeStanfordPage) => {
const pageTitleBannerImage = node.suPageBanner?.__typename === "ParagraphStanfordPageTitleBanner" && node.suPageBanner.suTitleBannerImage.mediaImage;
const bannerImage = node.suPageBanner?.__typename === "ParagraphStanfordBanner" && node.suPageBanner.suBannerImage?.mediaImage;
Expand Down Expand Up @@ -141,7 +168,7 @@ const getFirstText = (components?: Maybe<ParagraphUnion[]>) => {

const getCleanDescription = (description: string | undefined): string | undefined => {
if (description) {
const text: string = description.replace(/(<([^>]+)>)/gi, " ").replace("/ +/", " ").split(".").slice(0, 1).join(".") + ".";
const text: string = description.replace(/(<([^>]+)>)/gi, " ").replace("/ +/", " ").split(".").slice(0, 2).join(".") + ".";
return text?.length > 1 ? decode(text) : undefined;
}
}
Expand Down

0 comments on commit 42ac381

Please sign in to comment.