Skip to content
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

Error: Cannot find transform for node Node<"heading"> #9

Open
MonicaRungi-w opened this issue May 6, 2024 · 2 comments
Open

Error: Cannot find transform for node Node<"heading"> #9

MonicaRungi-w opened this issue May 6, 2024 · 2 comments
Labels
question Further information is requested

Comments

@MonicaRungi-w
Copy link

"heading" is not recognized:
Screenshot 2024-05-06 alle 11 47 09

"isHeading" function isn't checking the node type "heading":
Screenshot 2024-05-06 alle 11 48 38

@octet-stream
Copy link
Owner

octet-stream commented May 6, 2024

Hello. Thanks for your feedback!
Every transform in slate-to-react matches the type field against corresponding html tag, so in order for it to work, your node must have the h<level> type. Because Slate does not enforce any specific schema rules, other than children field for elements and text for leaves, I simply cannot cover every possible scenario for matchers. This is what createElementTransform, createLeafTransform, createElementNodeMatcher and createLeafNodeMatcher are for, so you can bring your own components to render Slate nodes the way that fits your own needs.

This is how you can create your custom element element matcher & transform:

import {createElement} from "react"
import {createElementNodeMatcher} from "slate-to-react"

export interface Heading {
  type: "heading"
  level: 1 | 2 | 3 | 4 | 5 | 6
  children: InlineDescendant[] // This would be your own type
}

export const isHeading = createElementNodeMatcher<Heading>((node): node is Heading => (
  node.type === "heading"
    && typeof node.level === "number"
    && node.level >= 1
    && node.level <= 6
))

export const HeadingElement = createElementTransform(
  isHeading,

  ({key, attributes, element, children}) => createElement(
    element.level,

    {
      ...attributes,

      key
    }
  )
)

I also recommend disabling all default transforms so you can see where you need to add custom elements by setting the defaultTransforms option to false. See: https://github.com/octet-stream/slate-to-react#type-transformnodesoptions

To be clear, this is the convention taken from Plate's schema – they typically place tag names in the type field and this is why I made it in my package by default.

Now I am thinking if it was a good idea making pre-built transforms enabled by default. Maybe this is something I will need to change sometime later.

@octet-stream octet-stream added the question Further information is requested label May 6, 2024
@MonicaRungi-w
Copy link
Author

@octet-stream Thank you for the quick response! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants