fix(docs): highlight code blocks after client-side navigation (#3287)#3288
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
|
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.
|
Thanks for the review @xsourabhsharma — both addressed in 2e2d91b:
|
|
@MFA-G could you plz sign the CLA? |
|
thanks @MFA-G @xsourabhsharma , i reviewed this, and it's ready to merge |
|
@reynaldichernando this seems good to go but still stuck on CLA, and @MFA-G hasn't been active for a few days what do you prefer here I can open a new PR with the same fix from my side or you can cherry pick it if that’s easier? |
|
that works @xsourabhsharma |
|
Sorry for the delay — I’ll take care of the CLA so this PR can move forward. If there is still any issue after I sign/recheck it, feel free to cherry-pick or reopen from your side, but I’d prefer to keep this PR if possible. |
|
@MFA-G i already tested this PR, and it works, just pending CLA |
|
@MFA-G did you facing any problem signing CLA? it work for me |
|
@xsourabhsharma no problem now — just signed it, the @reynaldichernando CLA is signed and all checks are passing — this should be good to merge whenever you're ready. Appreciate the review 🙏 |
|
thank you for your contribution! |
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.