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,7 +1,10 @@
import { z } from "zod"
import { tempImageVaultAssetSchema } from "../schemas/imageVault"
import { getDescription, getImages, getTitle } from "./utils"
import { RTETypeEnum } from "@/types/rte/enums"
export const rawMetaDataDataSchema = z.object({
web: z.object({
seo_metadata: z
@@ -14,6 +17,10 @@ export const rawMetaDataDataSchema = z.object({
z.object({
node: z.object({
url: z.string(),
dimension: z.object({
width: z.number(),
height: z.number(),
}),
}),
})
),
@@ -39,6 +46,36 @@ export const rawMetaDataDataSchema = z.object({
})
.optional()
.nullable(),
hero_image: tempImageVaultAssetSchema,
blocks: z
.array(
z.object({
content: z
.object({
content: z
.object({
json: z.object({
children: z.array(
z.object({
type: z.nativeEnum(RTETypeEnum),
children: z.array(
z.object({
text: z.string().optional().nullable(),
})
),
})
),
}),
})
.optional()
.nullable(),
})
.optional()
.nullable(),
})
)
.optional()
.nullable(),
})
export const metaDataSchema = rawMetaDataDataSchema.transform((data) => {

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 []
}