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

Fix Code Block syntax highlighting for imported text #1388

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Hansenq
Copy link

@Hansenq Hansenq commented Jan 23, 2025

Code blocks work great when the user is typing or pasting text into the code block. When this happens, a newline \n character is inserted into the prosemirror node's text content.

When code is imported, there's an off-by-one syntax highlighting error for every newline in the code block. Note that this bug only occurs when code is imported (not typed or pasted).

Incorrect result:
Output of problematic code

When importing a code block from HTML/Markdown/initial placeholder content, the imported code block is converted to an array of Blocks in markdownToBlocks, then inserted into the document with insertBlocks. insertBlocks converts these blocks to Prosemirror Nodes using the blocksToNode function. This function calls styledTextToNodes, which converts \n characters into a HardBreak node. This HardBreak node breaks Shiki's syntax highlighting because it inserts <br> tags inside the CodeBlock's <pre><code> tag. Shiki expects everything inside <pre><code> tags to be a string and results in an off-by-one syntax highlighting error if there's a <br> tag.

Expected result:
Proper syntax highlighting

Fixes #1231. Involves some prop drilling but I couldn't figure out a better way to solve this.

Copy link

vercel bot commented Jan 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
blocknote ✅ Ready (Inspect) Visit Preview Jan 23, 2025 0:42am

Copy link

vercel bot commented Jan 23, 2025

@Hansenq is attempting to deploy a commit to the TypeCell Team on Vercel.

A member of the Team first needs to authorize it.

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

Successfully merging this pull request may close these issues.

Syntax highlighting in code blocks not working properly when added in the initial content.
1 participant