Fix Code Block syntax highlighting for imported text #1388
+72
−19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
When importing a code block from HTML/Markdown/initial placeholder content, the imported code block is converted to an array of
Block
s inmarkdownToBlocks
, then inserted into the document withinsertBlocks
.insertBlocks
converts these blocks to Prosemirror Nodes using theblocksToNode
function. This function callsstyledTextToNodes
, 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:
Fixes #1231. Involves some prop drilling but I couldn't figure out a better way to solve this.