teqo-backup/src/utilities/extractFirstImageFromLexical.ts
Francisco Ignácio Fontoura Solla 9313eb7f29 feat: Add schema-dts to package.json and update SEO metadata
Adds the `schema-dts` dependency to `package.json` for structured data.
Updates the petition page to include JSON-LD schema markup for improved
SEO and rich results. This involves extracting the first image from the
Lexical editor or falling back to a global image for Open Graph and
schema.org metadata.
2026-05-01 02:13:39 -03:00

39 lines
956 B
TypeScript

import type { Media } from '@/payload-types'
type LexicalNode = {
type?: string
relationTo?: string
value?: unknown
children?: LexicalNode[]
[key: string]: unknown
}
type LexicalRoot = {
root: LexicalNode
[key: string]: unknown
}
const isImageMedia = (value: unknown): value is Media =>
typeof value === 'object' &&
value !== null &&
typeof (value as Media).url === 'string' &&
typeof (value as Media).mimeType === 'string' &&
(value as Media).mimeType!.startsWith('image/')
const findImage = (node: LexicalNode): Media | null => {
if (node.type === 'upload' && node.relationTo === 'media' && isImageMedia(node.value)) {
return node.value
}
if (Array.isArray(node.children)) {
for (const child of node.children) {
const found = findImage(child)
if (found) return found
}
}
return null
}
export const extractFirstImageFromLexical = (data: LexicalRoot): Media | null =>
findImage(data.root)