Skip to content

Commit

Permalink
docs: update TSDoc for new HTML serializer types
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Oct 25, 2021
1 parent ff15253 commit 05bd482
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type LinkResolverFunction<ReturnType = string> = (
/**
* Serializes a node from a rich text or title field with a function to HTML
*
* Unlike a typical `@prismicio/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/technologies/templating-rich-text-and-title-fields-javascript}
*/
export type HTMLFunctionSerializer = (
Expand All @@ -35,6 +38,9 @@ export type HTMLFunctionSerializer = (
/**
* Serializes a node from a rich text or title field with a map to HTML
*
* Unlike a typical `@prismicio/richtext` map serializer, this serializer
* converts the `children` property to a single string rather than an array of strings.
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
export type HTMLMapSerializer = {
Expand All @@ -47,14 +53,26 @@ export type HTMLMapSerializer = {
}) => string | null | undefined;
};

type HTMLMapSerializerFunction<P extends keyof RichTextMapSerializer<string>> =
RichTextMapSerializerFunction<
string,
ExtractRTNodeType<RichTextMapSerializer<string>[P]>,
ExtractRTTextType<RichTextMapSerializer<string>[P]>
>;
/**
* A {@link RichTextMapSerializerFunction} type specifically for {@link HTMLMapSerializer}.
*
* @typeParam BlockName - The serializer's Rich Text block type.
*/
type HTMLMapSerializerFunction<
BlockType extends keyof RichTextMapSerializer<string>,
> = RichTextMapSerializerFunction<
string,
ExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>,
ExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>
>;

type ExtractRTNodeType<T> = T extends RichTextMapSerializerFunction<
/**
* Returns the `Node` generic from {@link RichTextMapSerializerFunction}.
*
* @typeParam T - The `RichTextMapSerializerFunction` containing the needed
* `Node` generic.
*/
type ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
infer U,
Expand All @@ -64,7 +82,13 @@ type ExtractRTNodeType<T> = T extends RichTextMapSerializerFunction<
? U
: never;

type ExtractRTTextType<T> = T extends RichTextMapSerializerFunction<
/**
* Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.
*
* @typeParam T - The `RichTextMapSerializerFunction` containing the needed
* `TextType` generic.
*/
type ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 05bd482

Please sign in to comment.