-
Notifications
You must be signed in to change notification settings - Fork 51
CodeBlock - Fix line wrapping and highlighting rendering #2856
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
// we need to re-trigger the line numbers generation as late as possible to account for any line wrapping styles that are applied | ||
if (this.args.hasLineWrapping && Prism?.plugins?.['lineNumbers']) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
Prism.plugins['lineNumbers'].resize(this._preCodeElement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use the same logic as the line highlight and piggy back on triggering a resize
event because the Prism.js line number plugin does a check to see if the viewport width actually changes on a resize
event. It aborts any updates if the viewport is unchanged.
845b2d3
to
6fe1258
Compare
6fe1258
to
65c3779
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dchyun I did a quick first pass, but happy to do a second more in depth pass if/when you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple of comments/suggestions but nothing that is a blocker.
Caught a new issue in prod with line highlighting when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great PR! 👏👏👏
📌 Summary
Preview
If merged, this PR would fix rendering issues in the
CodeBlock
with the line wrapping and line highlighting. The primary issues found and resolved are.The
CodeBlock
was also converted from usingdid-insert
anddid-update
to custom modifiers.🛠️ Detailed description
Originally in #2826 there were several issues observed in the
CodeBlock
. It was observed that when the code block was used inside the tabs the line numbers and line highlighting of the code blocks inside tabs initially hidden were not rendering correctly.After additional investigation several issues with the Prism.js library were contributing to this problem.
Line numbers issues when line wrapping is enabled
First, line numbers are not being rendered correctly in the following situation
hasLineWrapping
istrue
CodeBlock
on the pageThe reason this issue has not been caught before is that when line highlighting is true, we trigger a
resize
event on the window to re-trigger Prism.js's line highlighting rendering. When there are other code blocks on a page that have line highlighting, this event was being listened to by code blocks without line highlighting as well. It would then re-trigger a Prism update, which would fix the line number issues.If a page has no other code blocks with line highlighting to trigger this
resize
event, then the issues with the line numbers are not fixed.Line number and highlight issues inside tabs
When the code block is initially hidden, such as being inside an inactive tab, the line numbers and highlighting are not rendered correctly when it is made visible, if
hasLineWrapping
istrue
.When the code block is hidden on load, the
resize
event to re-trigger the prism.js updates does not correctly apply the styles to the hidden content. To solve this issue, we are now re-triggering Prism.js when the code block becomes visible using anIntersectionObserver
.Line highlighting with no line numbers
When line highlighting is set but
hasLineNumbers
isfalse
the highlights start one line earlier than they should. This is due to Prism js calculating the line numbers based on thepre
element when line numbers are present, but calculating it based on thecode
element when they are not (source code link).📸 Screenshots
Line number & wrapping issues
Note: This was only possible to show locally by only displaying one code block on the showcase page
Before

After

Tabs issue
Before

After

Line highlighting with no line numbers
Before

After

🔗 External links
Jira ticket: HDS-4875
👀 Component checklist
💬 Please consider using conventional comments when reviewing this PR.