@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
147 lines (145 loc) • 6.72 kB
TypeScript
import { RichTextField } from "../types/value/richText.js";
import { RichTextFunctionSerializer, RichTextMapSerializer, RichTextMapSerializerFunction } from "../richtext/types.js";
import { LinkResolverFunction } from "./asLink.js";
//#region src/helpers/asHTML.d.ts
/**
* Serializes a node from a rich text 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 Learn how to style rich text and customize rendering: {@link https://prismic.io/docs/fields/rich-text}
*/
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 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 Learn how to style rich text and customize rendering: {@link https://prismic.io/docs/fields/rich-text}
*/
type HTMLRichTextMapSerializer = { [P in keyof RichTextMapSerializer<string>]: P extends RichTextMapSerializer<string>["span"] ? HTMLStrictRichTextMapSerializer[P] : HTMLStrictRichTextMapSerializer[P] | HTMLRichTextMapSerializerShorthand };
/**
* Serializes a node from a rich text 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 Learn how to style rich text and customize rendering: {@link https://prismic.io/docs/fields/rich-text}
*/
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.
*/
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 field with a map or a function to HTML.
*
* @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}
* @see Learn how to style rich text and customize rendering: {@link https://prismic.io/docs/fields/rich-text}
*/
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` option from the API.
*/
linkResolver?: LinkResolverFunction | null;
/**
* An optional rich text serializer. Unhandled cases will fall back 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;
declare const asHTML: {
/**
* Converts a rich text field to an HTML string.
*
* @example
*
* ```ts
* const html = asHTML(document.data.content)
* // => "<p>Hello world</p>"
* ```
*
* @param richTextField - A rich text field from Prismic.
* @param config - Configuration that determines the output of `asHTML()`.
*
* @returns HTML equivalent of the rich text field, or `null` if the field is
* empty.
*
* @see Learn how to style rich text and customize rendering: {@link https://prismic.io/docs/fields/rich-text}
*/
<Field extends RichTextField | null | undefined>(richTextField: Field, config?: AsHTMLConfig): AsHTMLReturnType<Field>;
/**
* Converts a rich text field to an HTML string.
*
* @deprecated Use object-style configuration instead.
*
* @param richTextField - A rich text field from Prismic.
* @param linkResolver - An optional link resolver function to resolve links.
* Without it, you're expected to use the `routes` option from the API.
* @param serializer - An optional rich text serializer. Unhandled cases will
* fall back to the default serializer.
*
* @returns HTML equivalent of the rich text field, or `null` if the field is
* empty.
*
* @see Learn how to style rich text and customize rendering: {@link https://prismic.io/docs/fields/rich-text}
*/
<Field extends RichTextField | null | undefined>(richTextField: Field, ...config: AsHTMLDeprecatedTupleConfig): AsHTMLReturnType<Field>;
};
//#endregion
export { HTMLRichTextFunctionSerializer, HTMLRichTextMapSerializer, HTMLRichTextSerializer, asHTML };
//# sourceMappingURL=asHTML.d.ts.map