Fix bug with wrapLines
that butchers highlighting
#561
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.
The Problem
When highlighting in JSX or TSX and using
prism
, template strings would sometimes fail to be highlighted correctly, as seen in the demo screenshot below.To reproduce, include any JSX node with multi-line text as well as a multi-line template string. You'll see that everything between the first and last lines of the template string is highlighted as
plaintext
instead ofstring
.This happens even though the demo for the underlying
refractor
package does not produce such an error.The Cause
The code for
createLineElement
inhighlight.js
modified thelineProps
variable directly without copying it. This leads tolineProps
at times taking on the className value of the currently processed line, which can then lead to other lines being wrapped with those classes.What happened in the example above is that when the plaintext inside the JSX is processed,
lineProps
is butchered and then overrides the previously emittedstring
classes for the template string.This code also leads to odd behavior when
lineProps
is specified butwrapLines
is not, with only some lines being affected bylineProps
and others not, seemingly at random.The Fix
lineProps
object to prevent it from being butchered.lineProps
object whenwrapLines
is set to true, as otherwise (according to the README documentation) it has no effect.className
field oflineProps
with the generated classes to ensure both are present in the final output.Testing
I have confirmed this fixes the issue above, while also preserving
wrapLines
functionality. Additionally,lineProps
now has no effect on output whenwrapLines
is not specified.