import type { RTAnyNode, RTEmNode, RTEmbedNode, RTHeading1Node, RTHeading2Node, RTHeading3Node, RTHeading4Node, RTHeading5Node, RTHeading6Node, RTImageNode, RTLabelNode, RTLinkNode, RTListItemNode, RTListNode, RTOListItemNode, RTOListNode, RTParagraphNode, RTPreformattedNode, RTSpanNode, RTStrongNode, RichTextNodeTypes, } from "../types/value/richText" import { RichTextNodeType } from "../types/value/richText" // Serializers /** * Serializes a node from a rich text or title field with a function * * @typeParam ReturnType - Return type of the function serializer. * @see Learn how to work with rich text fields: {@link https://prismic.io/docs/fields/rich-text} */ export type RichTextFunctionSerializer = ( type: RichTextNodeTypes, node: RTAnyNode, text: string | undefined, children: ReturnType[], key: string, ) => ReturnType | null | undefined /** * Map serializer's tag function serializer, can be helpful for typing those handlers * * @typeParam ReturnType - Return type of the tag serializer */ export type RichTextMapSerializerFunction< ReturnType, Node extends RTAnyNode = RTAnyNode, TextType = string | undefined, > = (payload: { type: Node["type"] node: Node text: TextType children: ReturnType[] key: string }) => ReturnType | null | undefined /** * Serializes a node from a rich text field with a map. * * @remarks * This type of serializer needs to be processed through {@link wrapMapSerializer} before being * used with {@link serialize}. * @typeParam ReturnType - Return type of the map serializer. * @see Learn how to work with rich text fields: {@link https://prismic.io/docs/fields/rich-text} */ export type RichTextMapSerializer = { heading1?: RichTextMapSerializerFunction heading2?: RichTextMapSerializerFunction heading3?: RichTextMapSerializerFunction heading4?: RichTextMapSerializerFunction heading5?: RichTextMapSerializerFunction heading6?: RichTextMapSerializerFunction paragraph?: RichTextMapSerializerFunction preformatted?: RichTextMapSerializerFunction strong?: RichTextMapSerializerFunction em?: RichTextMapSerializerFunction listItem?: RichTextMapSerializerFunction oListItem?: RichTextMapSerializerFunction list?: RichTextMapSerializerFunction oList?: RichTextMapSerializerFunction image?: RichTextMapSerializerFunction embed?: RichTextMapSerializerFunction hyperlink?: RichTextMapSerializerFunction label?: RichTextMapSerializerFunction span?: RichTextMapSerializerFunction } // Tree export interface Tree { key: string children: TreeNode[] } export interface TreeNode { key: string type: RichTextNodeTypes text?: string node: RTAnyNode children: TreeNode[] } // Helpers export const RichTextReversedNodeType = { [RichTextNodeType.listItem]: "listItem", [RichTextNodeType.oListItem]: "oListItem", [RichTextNodeType.list]: "list", [RichTextNodeType.oList]: "oList", } as const