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

[Bug]: generateJSON is stripping trailing whitespace #4432

Open
1 of 2 tasks
ko-synth opened this issue Sep 8, 2023 · 7 comments
Open
1 of 2 tasks

[Bug]: generateJSON is stripping trailing whitespace #4432

ko-synth opened this issue Sep 8, 2023 · 7 comments
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@ko-synth
Copy link

ko-synth commented Sep 8, 2023

Which packages did you experience the bug in?

react-tiptap

What Tiptap version are you using?

2.1.8

What’s the bug you are facing?

The issue was already reported, but went stale.

Everything from the old issue still applied. I quickly hacked a demo together, please find it here.

Screenshot 2023-09-08 at 17 18 26

What browser are you using?

Chrome

Code example

No response

What did you expect to happen?

I expected to end up with the same result with both methods.

Anything to add? (optional)

No response

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. 💖
@ko-synth ko-synth added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Sep 8, 2023
@github-project-automation github-project-automation bot moved this to Triage open in Tiptap Sep 8, 2023
@hariacharya80
Copy link
Contributor

hariacharya80 commented Oct 6, 2023

Working
Edit: I mean, working on it.

@ko-synth
Copy link
Author

ko-synth commented Oct 8, 2023

Working

You mean, you couldn't reproduce?

Find a video bellow of how to go about it:

Screen.Recording.2023-10-08.at.21.48.31.mov

@hariacharya80
Copy link
Contributor

hariacharya80 commented Oct 9, 2023

@ko-synth I'm trying to work around this, I have a bit of a stop in this function:

export function generateJSON(html: string, extensions: Extensions): Record<string, any> {
  const schema = getSchema(extensions)
  const dom = elementFromString(html)

  return DOMParser.fromSchema(schema).parse(dom).toJSON()
}

While it seems that the DOMParser is using the schema from extensions to parse the JSON, I think this could be something to do with the starterkit? Not sure.. need guidance.

PS: I looked over this: https://prosemirror.net/docs/ref/#model.NodeSpec.parseDOM and https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/DOMParser

@calumgunn
Copy link

Hello, if it helps --
Seems that generateJSON is removing trailing/preceding tabs, too. For example, preceding \t characters in <code> elements.

@calumgunn
Copy link

Minimal test, and result:

// Using \t, &Tab; or &#9; to denote a tab all have the same result
const testHtml = "<code>&#9;Test after a tab.</code>";
const testJson = generateJSON(testHtml, [StarterKit]);

Produces:
Screenshot 2023-12-15 at 15 19 41

Which then renders as:
Screenshot 2023-12-15 at 15 17 29

@vollmond08
Copy link

vollmond08 commented Dec 26, 2023

editor.commands.setContent(`<p>Example Text </p>`, false, { preserveWhitespace: true, });

helped me, like a workaround

@asiraky
Copy link
Contributor

asiraky commented May 16, 2024

@ko-synth I'm trying to work around this, I have a bit of a stop in this function:

export function generateJSON(html: string, extensions: Extensions): Record<string, any> {
  const schema = getSchema(extensions)
  const dom = elementFromString(html)

  return DOMParser.fromSchema(schema).parse(dom).toJSON()
}

While it seems that the DOMParser is using the schema from extensions to parse the JSON, I think this could be something to do with the starterkit? Not sure.. need guidance.

PS: I looked over this: https://prosemirror.net/docs/ref/#model.NodeSpec.parseDOM and https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/DOMParser

The fix was to pass preserveWhitespace: true as an option to the parse function. I have submitted a PR to allow the caller to pass the options.

Edit: here is the PR #5158

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
No open projects
Status: Triage open
Development

No branches or pull requests

7 participants