Comments (2)
Hey there, thank you so much for contributing and submitting a nice reproduction.
I'm sorry we failed to get back to you in a timely manner but I'm glad you had a workaround from the get go.
tl;dr;
- This is not a regression but a bug fix.
- Returning
content
was working because it was wrongfullyundefined
in prior versions, this got fixed on@prismicio/richtext
, an upstream package, between2.3.0
and2.3.1
: prismicio/prismic-richtext#55 - The correct way to fall back to the serializer default behavior if by returning
null
as you figured out.
Details
What's happening?
asHTML()
and other serialization methods (components, hooks, etc.), serialize Prismic rich text fields by relying on an internal default serializer with sensible defaults (https://github.com/prismicio/prismic-client/blob/3bc5b95a9d4bd69e70068a209e1e9e4b04b87c69/src/helpers/asHTML.ts#L119-L171). Basically, it's just saying, in the case of an HTML serialization, that a paragraph should wrap its children with a <p>
tag, and so on.
However, we often need something different than these defaults, maybe we're using TailwindCSS and want to apply extra classes, or maybe we don't want to wrap children of specific element types like in your case. In such scenarios, we can provide an extra "custom serializer" to the asHTML()
method or its derivatives. This "custom serializer" can either:
- Serialize differently some elements
- Serialize differently all elements
It's quite rare to fall in the case of nĀ°2, that's why a "custom serializer" can fall back to the internal default serializer by returning a falsy value, null
as a convention.
@prismicio/[email protected]
and earlier had an issue not filling the content
property received by the "custom serializer" as expected. This meant that by returning content
when this version was installed as a dependency of @prismicio/helpers
, it was effectively returning undefined
, telling asHTML()
to fallback to its internal default serializer, serializing links and other elements as expected.
When this was fixed in @prismicio/[email protected]
(prismicio/prismic-richtext#55), content
was now filled as expected, and the "custom serializer" stopped returning a falsy value, meaning no fallback to the internal default serializer were happening anymore.
Why @prismicio/[email protected]
and prior?
I'm not completely nominal on that, but I'm gonna assume CodeSandbox has some caching mechanism on dependencies/lock files.
When using @prismicio/[email protected]
and earlier, it was using an older version of @prismicio/richtext
not including this fix: prismicio/prismic-richtext#55 (supposedly 2.0.1). This could also happen locally depending on the project's lock file.
You can see this fork with an explicit @prismicio/richtext
installed reproducing the same behavior despite relying on @prismicio/[email protected]
: https://codesandbox.io/s/prismicio-helpers-html-serializer-bug-forked-yrtwtc?file=/src/App.tsx:1019-1026
How to do it?
The recommended way to achieve what you wanted was to return null
as you figured out.
asHTML(field, null, (type, node, content, children) => {
if (type === "paragraph") return children;
return null;
});
I'd recommend in such scenarios relying on a map serializer instead of a function serializer. This is the equivalent of the above and has simpler types:
asHTML(field, null, {
paragraph: ({ children }) => children;
});
@prismicio/client
v7
Just a friendly notification that we recently released @prismicio/client
v7 that unifies our core libraries (@prismicio/types
and @prismicio/helpers
were merged inside @prismicio/client
so you just need to install one package moving forward)
Read the announcement and learn more about it: https://prismic.io/blog/client-v7-new-unified-core-library
Hope this answers your issue, let us know if anything!
from prismic-helpers.
This issue has been labeled as a bug since it was created using the šØ Bug Report Template.
Hi there, thank you so much for the report!
Following our Maintenance Process, we will review your bug report and get back to you next Wednesday. To ensure a smooth review of your issue and avoid unnecessary delays, please make sure your issue includes the following:
- Information about your environment and packages you use (Node.js version, package names and their versions, etc.)
Feel free to attach a copy of yourpackage.json
file. - Any troubleshooting steps you already went through
- A minimal reproduction of the issue, and/or instructions on how to reproduce it
If you have identified the cause of the bug described in your report and know how to fix it, you're more than welcome to open a pull request addressing it. Check out our quick start guide for a simple contribution process.
If you think your issue is a question (not a bug) and would like quicker support, please close this issue and forward it to an appropriate section on our community forum: https://community.prismic.io
- The Prismic Open-Source Team
from prismic-helpers.
Related Issues (20)
- Provide HTML Serializer children as string HOT 5
- HTML Serializer object syntax HOT 1
- [bug] `get()` doesn't accept single predicate HOT 2
- feat: provide `defineHTMLFunctionSerializer` and `defineHTMLMapSerializer` helpers HOT 6
- V2 Incompatible with gatsby-source-prismic? HOT 2
- `isFilled` helper HOT 2
- Support nullish inputs for `isFilled` helpers HOT 3
- RFC: Provide image URL helpers for Imgix's URL API HOT 10
- Create a helper to transform html code in RichText object HOT 1
- `isFilled.image` is not fully compatible with `ImageField` type HOT 3
- Create an `asRepositoryName()` helper HOT 3
- `isFilled.image` does not work with `ImageField` out of the box HOT 2
- utility or cli to extract response type definition from prismic custom types HOT 3
- Type error: '?' expected. HOT 8
- Error: Type 'P_2' cannot be used to index type 'FilledImageFieldImage & Record<ThumbnailNames extends string ? Exclude<ThumbnailNames, "url" | "dimensions" | "alt" | "copyright"> : never, FilledImageFieldImage>' HOT 5
- asHTML renders hyperlinks with undefined target HOT 3
- `prismicH.asImageSrc` using `width` instead of `w` doesn't do anything HOT 2
- Nuxtjs 2 using `prismicH.Element` return error `need an appropriate loader` HOT 4
- .asHtml not recognized in prismicH.asHtml (using Vite, Vue 3 and Nuxt 3) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prismic-helpers.