-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add unstable_htmlAsRichText
and unstable_markdownAsRichText
helpers
#342
Conversation
- remove `sync` helpers - rename `converter` option to `serializer` - add `direction`, `include` options - remove `silent` option - allow deep selectors in the `serializer` map - refactor helpers return type - refactor `rehypeRichText` plugin - refactor tests
- add, test, and export `filterRichTextField` helper - refactor `htmlAsRichText` and `markdownAsRichText` to be synchronous - support `model` option - support `defaultWrapperNodeType` option - test `markdownAsRichText`
- support function serializers - document all public interfaces - refactor `hastUtiltoRichText` - extract default node serializers to `serializerHelpers` - delete `isNodeType` helpers - finish testing coverage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work! It seems to work really well — not an easy feat.
I'm going to mark the PR as "Request changes" since there are a number of comments that require discussion, but that doesn't necessarily mean anything needs to change. I don't have any blocking comments.
❓ General questions
-
Returning
messages
asVFileMessage[]
ties our API to unified. Although we don't have any immediate plans to transition away from unified, I could see us wanting to explore a version without it for performance and size benefits. What do you think of abstractingVFileMessage[]
into a higher-level version with objects that we create? -
Should
warnings
be namedmessages
to match unified's output? I wonder if there would ever be a message that isn't a warning, in which casewarnings
would not make sense. If the messages are always warnings,warnings
is good. -
filterRichTextField
seems like something that belongs in user code rather than@prismicio/client
. It functions as a way to clean up already existing rich text data. It assumes the user has easy access to the model and is working with invalid rich text data for the model. Couldn't the user simply not return invalid rich text nodes in the serializer?- What happens when rich text data that contains invalid nodes (e.g. a
heading1
when Heading 1 is not enabled) is uploaded to Prismic? Does it filter out the invalid nodes automatically? If yes,filterRichTextField
may not be necessary for the*AsRichText
helper's intended use cases.
- What happens when rich text data that contains invalid nodes (e.g. a
let width = (node.properties?.width as number) ?? 0; | ||
let height = (node.properties?.height as number) ?? 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ #question: Do these values mean images will be 0x0
by default? I thought we were leaving the values off if we couldn't infer it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated them to undefined
but I had to cast them as number
down the road because the API types are always filled. Does that sound OK to you? Should we update the API types instead to allow undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm… the public API always returns dimension values so we should keep them as always defined.
How do we know the Migration API doesn't require them? If the API really doesn't require them, then casting is good with me. When we start working on the Migration API client, we may want to look into a custom Migration API-specific ImageField
type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to cast! That's what @Heyrows shared with me for reference: https://github.com/prismicio/migration-api/blob/5a8455118c5fcbcb60225a94919a97cc7646fd73/src/models/import/validators/fields/nestable/Image/merge.ts#L35-L38, they leave them empty and compute them later on.
I don't have an issue with having a separate ImageField
type, but then that means we also need to make the rich text types and nodes more flexible to accommodate both or duplicate them, which I feel meh about 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a small comment about it linking to this thread 👌
Thank you so much for the prompt review! I updated everything as per it and answered discussions
I updated the signature to be an array of string :)
Discussed here: #342 (comment), this is a bit trickier than not providing a serializer for the given type of node I'm afraid.
We'll discover that pretty soon as I start to work with the migration API I assume 🤔 |
We agreed to export everything from
|
src/richtext/utils/hastToRichText.ts
Outdated
*/ | ||
export type HastUtilToRichTextConfig = { | ||
export type HASTToRichTextConfig = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 #idea: I think this type should be written as HastToRichTextConfig
since hast is not an acronym like HTML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From GitHub, it's the acronym for Hypertext Abstract Syntax Tree, so I'll leave it as-is, but we can rename it later.
See how letters are bolded at the very start of the README: https://github.com/syntax-tree/hast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! ❤️
As we discussed, we just need to export the helpers from the right entries and reorganize some of the lib
functions.
Tree-shaking works as we want: there are no traces of the unified ecosystem if you don't use the *AsRichText
helpers. ~48 KB was added when using the markdownAsRichText
helper. As a general rule, we'll recommend against using the helper on the client.
Co-authored-by: Angelo Ashmore <[email protected]>
unstable_htmlAsRichText
and unstable_markdownAsRichText
helpers
@@ -4,6 +4,7 @@ export { asText } from "./asText"; | |||
export { serialize } from "./serialize"; | |||
export { wrapMapSerializer } from "./wrapMapSerializer"; | |||
export { composeSerializers } from "./composeSerializers"; | |||
export { filterRichTextField } from "../lib/filterRichTextField"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed that on master 🙈
Implements: RFC: HTML to Rich Text Serializer
Fixes: #331
Description
This pull request implements helpers to serialize HTML and markdown to Prismic rich text field format as described per the related RFC.
These new helpers are introduced to help users migrating to Prismic through the migration API to convert their HTML content to rich text easily.
Checklist
htmlAsRichText
,markdownAsRichText
, andAsRichTextConfig
fromfilterRichTextField
(could be helpful internally)rehypeRichText
andhastUtilToRichText
How to QA 1
Footnotes
Please use these labels when submitting a review:
⚠️ #issue: Strongly suggest a change.
❓ #ask: Ask a question.
💡 #idea: Suggest an idea.
🎉 #nice: Share a compliment. ↩