Skip to content

Commit

Permalink
refactor!: rename config htmlRichTextSerializer to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed May 16, 2023
1 parent db98c9a commit b4bdf7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
23 changes: 11 additions & 12 deletions src/helpers/asHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ type AsHTMLConfig = {
/**
* An optional Rich Text Serializer, unhandled cases will fallback to the default serializer
*/
htmlRichTextSerializer?: HTMLRichTextSerializer | null;
serializer?: HTMLRichTextSerializer | null;
};

// TODO: Remove when we remove support for deprecated tuple-style configuration.
Expand All @@ -222,7 +222,7 @@ type AsHTMLConfig = {
*/
type AsHTMLDeprecatedTupleConfig = [
linkResolver?: LinkResolverFunction | null,
htmlRichTextSerializer?: HTMLRichTextSerializer | null,
serializer?: HTMLRichTextSerializer | null,
];

/**
Expand Down Expand Up @@ -253,7 +253,7 @@ export const asHTML: {
* @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 htmlRichTextSerializer - An optional Rich Text Serializer, unhandled cases will fallback
* @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
Expand All @@ -264,8 +264,8 @@ export const asHTML: {
* ```ts
* asHTML(field);
* asHTML(field, { linkResolver });
* asHTML(field, { htmlRichTextSerializer });
* asHTML(field, { linkResolver, htmlRichTextSerializer });
* asHTML(field, { serializer });
* asHTML(field, { linkResolver, serializer });
* ```
*/
<Field extends RichTextField | null | undefined>(
Expand All @@ -279,29 +279,28 @@ export const asHTML: {
): AsHTMLReturnType<Field> => {
if (richTextField) {
// TODO: Remove when we remove support for deprecated tuple-style configuration.
const [configObjectOrLinkResolver, maybeHTMLRichTextSerializer] =
configObjectOrTuple;
const [configObjectOrLinkResolver, maybeSerializer] = configObjectOrTuple;
let config: AsHTMLConfig;
if (
typeof configObjectOrLinkResolver === "function" ||
configObjectOrLinkResolver == null
) {
config = {
linkResolver: configObjectOrLinkResolver,
htmlRichTextSerializer: maybeHTMLRichTextSerializer,
serializer: maybeSerializer,
};
} else {
config = { ...configObjectOrLinkResolver };
}

let serializer: RichTextFunctionSerializer<string>;
if (config.htmlRichTextSerializer) {
if (config.serializer) {
serializer = composeSerializers(
typeof config.htmlRichTextSerializer === "object"
? wrapMapSerializerWithStringChildren(config.htmlRichTextSerializer)
typeof config.serializer === "object"
? wrapMapSerializerWithStringChildren(config.serializer)
: (type, node, text, children, key) =>
// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.
(config.htmlRichTextSerializer as HTMLRichTextFunctionSerializer)(
(config.serializer as HTMLRichTextFunctionSerializer)(
type,
node,
text,
Expand Down
8 changes: 4 additions & 4 deletions test/helpers-asHTML.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ it("serializes with a custom function serializer", () => {
expect(
asHTML(richTextFixture.en, {
linkResolver,
htmlRichTextSerializer: htmlRichTextFunctionSerializer,
serializer: htmlRichTextFunctionSerializer,
}),
).toMatchSnapshot();

Expand All @@ -30,7 +30,7 @@ it("serializes with a custom function serializer", () => {
).toBe(
asHTML(richTextFixture.en, {
linkResolver,
htmlRichTextSerializer: htmlRichTextFunctionSerializer,
serializer: htmlRichTextFunctionSerializer,
}),
);
});
Expand All @@ -39,7 +39,7 @@ it("serializes with a custom map serializer", () => {
expect(
asHTML(richTextFixture.en, {
linkResolver,
htmlRichTextSerializer: htmlRichTextMapSerializer,
serializer: htmlRichTextMapSerializer,
}),
).toMatchSnapshot();

Expand All @@ -49,7 +49,7 @@ it("serializes with a custom map serializer", () => {
).toBe(
asHTML(richTextFixture.en, {
linkResolver,
htmlRichTextSerializer: htmlRichTextMapSerializer,
serializer: htmlRichTextMapSerializer,
}),
);
});
Expand Down

0 comments on commit b4bdf7f

Please sign in to comment.