import { EmbedContentSchema, EmbedContentType } from "../embed" import type { EmbedContent } from "../embed" import type { EmbedLegacy } from "../legacy/embed" import { EmbedLegacySchema, EmbedLegacyLooseSchema } from "../legacy/embed" import type { LegacyContentCtx, LegacyCodec } from "./legacyContentCtx" // Also used by content/codec/richText export function embedLegacyToContent(value: EmbedLegacy): EmbedContent { const parsed = EmbedLegacySchema.parse(value) return { ...parsed, all: value, __TYPE__: EmbedContentType, } } // Also used by content/codec/richText export function embedContentToLegacy(value: EmbedContent): EmbedLegacy { /** * We cast here because actually in the all property we can have * extra keys that are never parsed and we don't want to loose them. */ return value.all as EmbedLegacy } export const EmbedLegacyCodec = ( ctx: LegacyContentCtx, ): LegacyCodec => ({ name: "EmbedLegacy", is(input): input is EmbedContent { return EmbedContentSchema.safeParse(input).success }, toContent(input) { const parsed = EmbedLegacyLooseSchema.safeParse(input) if (!parsed.success) { return parsed } return { success: true, data: embedLegacyToContent(parsed.data), } }, fromContent(input) { return { content: embedContentToLegacy(input), types: { [ctx.keyOfType]: "Embed" }, keys: {}, } }, })