fix(docs): highlight code blocks after client-side navigation (#3287)#3288
Open
MFA-G wants to merge 2 commits into
Open
fix(docs): highlight code blocks after client-side navigation (#3287)#3288MFA-G wants to merge 2 commits into
MFA-G wants to merge 2 commits into
Conversation
The docs site highlights code on full page load via an inline hljs.highlightAll(), which targets the 'pre code' selector and so highlights every fenced block, including those without a language tag. The client-side navigation path (pathchange handler) instead selected only code[class^="language"], so fenced blocks written without a language hint were left unhighlighted after in-app navigation while appearing correctly on a hard reload. Align the pathchange handler with highlightAll() by selecting 'pre code' and skipping blocks hljs has already processed (data-highlighted=yes), keeping the pass idempotent across repeated pathchange events. Fixes HeyPuter#3287
|
|
Contributor
|
Fix is correct selector change to Two nits:
|
Address review nits on the pathchange highlight pass:
- Move hljs.configure({ ignoreUnescapedHTML: true }) out of the per-element
loop since it is global config and only needs to run once.
- Scope the selector to '.docs-content pre code' so the re-highlight pass
only touches the swapped-in docs body and leaves code blocks elsewhere
on the page untouched.
Author
|
Thanks for the review @xsourabhsharma — both addressed in 2e2d91b:
|
Contributor
|
@MFA-G could you plz sign the CLA? |
Member
|
thanks @MFA-G @xsourabhsharma , i reviewed this, and it's ready to merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #3287 — code highlighting is missing on the docs site after client-side navigation (e.g. clicking Deployments in the sidebar). The code only gets styled after a hard refresh.
Root cause
The docs use two different code paths to run highlight.js, and they target different selectors:
hljs.highlightAll()(injected inbuild.js).highlightAll()uses highlight.js's defaultpre codeselector, so it highlights every fenced block — including blocks written without a language tag..docs-contentand fires apathchangeevent. Thepathchangehandler inexample.jshighlighted onlycode[class^="language"].Markdown fenced blocks without a language hint render as bare
<pre><code>(nolanguage-*class), so they matched on full load but were skipped after in-app navigation — exactly the symptom in the issue.On the Deployments page there are 3
pre codeblocks but only 1 carries alanguage-class, so 2 of 3 blocks stayed unstyled after client-side nav.Fix
Align the
pathchangehandler withhighlightAll()by selectingpre code, and skip blocks highlight.js has already processed (data-highlighted="yes") so repeatedpathchangeevents stay idempotent and don't log highlight.js's "Element previously highlighted" warning.Testing
npm run buildinsrc/docssucceeds.pathchangenavigation to the Deployments page, highlightedpre codeblocks went from 0 → 3 of 3. Pre-fix the same flow highlighted only 1 of 3.pathchangeagain leaves all 3 blocks highlighted with no re-processing.Scoped to a single docs file; no behavior change for blocks that already carried a language class.