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

textSerializers in generateText don't override renderText set in schema #3538

Closed
2 tasks done
tovaschreier opened this issue Dec 13, 2022 · 2 comments · Fixed by #3546
Closed
2 tasks done

textSerializers in generateText don't override renderText set in schema #3538

tovaschreier opened this issue Dec 13, 2022 · 2 comments · Fixed by #3546
Labels
Type: Bug The issue or pullrequest is related to a bug

Comments

@tovaschreier
Copy link
Contributor

tovaschreier commented Dec 13, 2022

What’s the bug you are facing?

Providing textSerializers to generateText does not override the renderText function defined in the schema

Which browser was this experienced in? Are any special extensions installed?

Chrome, no special extensions

How can we reproduce the bug on our side?

Provide a custom renderText in your schema:

  const extensions = [
    StarterKit,
    Image.extend({
      // default renderText defined in the schema
      renderText() {
        return "[Image text from renderText in schema]";
      }
    })
  ];

  const editor = useEditor({
    extensions,
    content: `
      <p>
        this is a basic example of tiptap with an image below. 
      </p>
      <img src="https://faculty.washington.edu/ejslager/random-generator/duck.gif" alt="duck swimming">
    `
  });

Call generateText and pass in a custom textSerializer for the node:

  const textContent = generateText(editor.getJSON(), extensions, {
        // this should override the default renderText method
        textSerializers: {
          image: () => "[Image text from textSerializer]"
        }
      });

Expected behavior: the text returned in the text serializer should be output when calling generateText

Can you provide a CodeSandbox?

Yes! https://codesandbox.io/s/infallible-keldysh-tf1ldm?file=/src/App.js

What did you expect to happen?

The textSerializers provided to generateText would override the renderText function defined in the schema.

In fact, this seems to be the expected behavior laid out in the PR that introduced the generateText method - the PR description says:

usage of generateText()

// with some options
const text = generateText(json, [
  Document,
  Paragraph,
  Text,
  HardBreak,
  // other extensions …
], {
  // define a custom block separator if you want to
  blockSeparator: '\n',
  // overwrite `renderText` method of nodes
  textSerializers: {
    hardBreak: () => '\n',
  },
})

Note the "overwrite renderText method of nodes" comment right before textSerializers

Anything to add? (optional)

I believe this is just a matter of switching the order of these 2 lines:

  return getText(contentNode, {
    blockSeparator,
    textSerializers: {
      ...textSerializers,
      ...getTextSerializersFromSchema(schema),
    },
  })

so that the text serializers from the schema get added first, and then any custom text serializers overwrite the default ones in the schema.

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖
@georgemandis
Copy link
Contributor

Just wanted to chime-in with a +1 on this—it would be very helpful the linked-to PR really does look like it just involves switching 2 lines.

You all have been very responsive in the past for us, so much appreciated!

@georgemandis
Copy link
Contributor

Forgot to add this link for context, which i think is helpful:

The documentation here says it's overridable, but the explanation here shows why that doesn't work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
2 participants