feat(SW-200): Added other options for description and images

This commit is contained in:
Erik Tiekstra
2024-11-14 15:02:45 +01:00
parent b22888db5f
commit 6aba0d8f52
10 changed files with 137 additions and 60 deletions

View File

@@ -1,3 +1,4 @@
import { RTETypeEnum } from "@/types/rte/enums"
import { RawMetaDataSchema } from "@/types/trpc/routers/contentstack/metadata"
export const affix = "metadata"
@@ -30,15 +31,42 @@ export function getDescription(data: RawMetaDataSchema) {
if (data.header?.preamble) {
return data.header.preamble
}
if (data.blocks?.length) {
const jsonData = data.blocks[0].content?.content?.json
// Finding the first paragraph with text
const firstParagraph = jsonData?.children?.find(
(child) => child.type === RTETypeEnum.p && child.children[0].text
)
if (firstParagraph?.children?.length) {
return firstParagraph.children[0].text
}
}
return ""
}
export function getImages(data: RawMetaDataSchema) {
const metaData = data.web.seo_metadata
if (metaData?.imageConnection) {
return metaData.imageConnection.edges.map((edge) => ({
url: edge.node.url,
}))
const metaDataImages = data.web.seo_metadata?.imageConnection?.edges
const heroImage = data.hero_image
if (metaDataImages?.length) {
return metaDataImages.map((edge) => {
const { width, height } = edge.node.dimension
return {
url: edge.node.url,
width: 1200,
height: Math.round((1200 * height) / width),
}
})
}
if (heroImage) {
return [
{
url: heroImage.url,
width: heroImage.dimensions.width,
height: heroImage.dimensions.height,
},
]
}
return []
}