@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
142 lines (141 loc) • 6.85 kB
TypeScript
import type { RichTextField } from "../types/value/richText";
import type { RichTextFunctionSerializer, RichTextMapSerializer, RichTextMapSerializerFunction } from "../richtext";
import type { LinkResolverFunction } from "./asLink";
/**
* Serializes a node from a rich text or title field with a function to HTML.
*
* Unlike a typical `@prismicio/client/richtext` function serializer, this
* serializer converts the `children` argument to a single string rather than an
* array of strings.
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
export type HTMLRichTextFunctionSerializer = (type: Parameters<RichTextFunctionSerializer<string>>[0], node: Parameters<RichTextFunctionSerializer<string>>[1], text: Parameters<RichTextFunctionSerializer<string>>[2], children: Parameters<RichTextFunctionSerializer<string>>[3][number], key: Parameters<RichTextFunctionSerializer<string>>[4]) => string | null | undefined;
/**
* Serializes a node from a rich text or title field with a map to HTML
*
* Unlike a typical `@prismicio/client/richtext` map serializer, this serializer
* converts the `children` property to a single string rather than an array of
* strings and accepts shorthand declarations.
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
export type HTMLRichTextMapSerializer = {
[P in keyof RichTextMapSerializer<string>]: P extends RichTextMapSerializer<string>["span"] ? HTMLStrictRichTextMapSerializer[P] : HTMLStrictRichTextMapSerializer[P] | HTMLRichTextMapSerializerShorthand;
};
/**
* Serializes a node from a rich text or title field with a map to HTML
*
* Unlike a typical `@prismicio/client/richtext` map serializer, this serializer
* converts the `children` property to a single string rather than an array of
* strings but doesn't accept shorthand declarations.
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
export type HTMLStrictRichTextMapSerializer = {
[P in keyof RichTextMapSerializer<string>]: (payload: {
type: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["type"];
node: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["node"];
text: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["text"];
children: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["children"][number];
key: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["key"];
}) => string | null | undefined;
};
/**
* A {@link RichTextMapSerializerFunction} type specifically for
* {@link HTMLRichTextMapSerializer}.
*
* @typeParam BlockName - The serializer's rich text block type.
*/
type HTMLRichTextMapSerializerFunction<BlockType extends keyof RichTextMapSerializer<string>> = RichTextMapSerializerFunction<string, ExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>, ExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>>;
/**
* Returns the `Node` generic from {@link RichTextMapSerializerFunction}.
*
* @typeParam T - The `RichTextMapSerializerFunction` containing the needed
* `Node` generic.
*/
type ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<any, infer U, any> ? U : never;
/**
* Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.
*
* @typeParam T - The `RichTextMapSerializerFunction` containing the needed
* `TextType` generic.
*/
type ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<any, any, infer U> ? U : never;
/**
* A shorthand definition for {@link HTMLRichTextMapSerializer} element types.
*/
export type HTMLRichTextMapSerializerShorthand = {
/**
* Classes to apply to the element type.
*/
class?: string;
/**
* Other attributes to apply to the element type.
*/
[Attribute: string]: string | boolean | null | undefined;
};
/**
* Serializes a node from a rich text or title field with a map or a function to
* HTML
*
* @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
export type HTMLRichTextSerializer = HTMLRichTextMapSerializer | HTMLRichTextFunctionSerializer;
/**
* Configuration that determines the output of `asHTML()`.
*/
type AsHTMLConfig = {
/**
* An optional link resolver function to resolve links. Without it you're
* expected to use the `routes` options from the API.
*/
linkResolver?: LinkResolverFunction | null;
/**
* An optional rich text serializer, unhandled cases will fallback to the
* default serializer
*/
serializer?: HTMLRichTextSerializer | null;
};
/**
* @deprecated Use object-style configuration instead.
*/
type AsHTMLDeprecatedTupleConfig = [
linkResolver?: LinkResolverFunction | null,
serializer?: HTMLRichTextSerializer | null
];
/**
* The return type of `asHTML()`.
*/
type AsHTMLReturnType<Field extends RichTextField | null | undefined> = Field extends RichTextField ? string : null;
export declare const asHTML: {
/**
* Serializes a rich text or title field to an HTML string.
*
* @param richTextField - A rich text or title field from Prismic
* @param config - Configuration that determines the output of `asHTML()`
*
* @returns HTML equivalent of the provided rich text or title field
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
<Field extends RichTextField | null | undefined>(richTextField: Field, config?: AsHTMLConfig): AsHTMLReturnType<Field>;
/**
* Serializes a rich text or title field to an HTML string.
*
* @deprecated Use object-style configuration instead.
*
* @param richTextField - A rich text or title field from Prismic
* @param linkResolver - An optional link resolver function to resolve links,
* without it you're expected to use the `routes` options from the API
* @param serializer - An optional rich text serializer, unhandled cases will
* fallback to the default serializer
*
* @returns HTML equivalent of the provided rich text or title field
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
<Field extends RichTextField | null | undefined>(richTextField: Field, ...config: AsHTMLDeprecatedTupleConfig): AsHTMLReturnType<Field>;
};
export {};