HighlightJS's Themes Not Working
Consider this code:
The highlightjs themes are not working on page load. When i highlight nothing gets colored, tho for some odd reason doing a hot reload remedies this issue
8 Replies
not sure how highlight.js works, but just a guess:
createEffect runs before mount meaning before all elements are mounted.
So maybe adding
will do it. Would also explain why hot reload fixes it, because the dom elements are already mounted.
Are the code blocks mounted conditionally?
Made this stackblitz to showcase what happens if there's a condition and code blocks get rendered after the highlightAll() is called.
https://stackblitz.com/edit/github-l3ak3iob-jzvjqhdg?file=src%2Fapp.tsx,src%2Fapp.css,src%2Fentry-server.tsx
doesn't work
and by conditionally wdym?
i think i found something
registering them on the server as normal link elements work
tho it only works on the first load
changing the theme will not change the link elements disable stat
the more i think about it
the more ik where the issue is
SSR stuff
bc its set to render server-side, maybe the signals don't get updated or smth
What’s the implementation of getTheme?
Like in the stackblitz wrapped in a Show component.
Doesn’t need to be conditionally. Could also be client only / lazy loaded.
If
hljs.hihglightAll()
is called before the pre code
elements are in the Dom highlighting doesn’t work.its a signal
the codeblocks are not conditionally rendered
tbh i think i should wrap the link components as a client one
i feel like i've got the hugest brain of mankind when i solved this bug
lmao
the way i patched it is like so
where
i import it client-side instead of server-side
so that the client gets to decide how to highlight the themes
tho when i open the docs page i am greeted with this

this only is triggered when i reload the page
and its caused in
oh fixed
same technique
Here's a working example in stackblitz
https://stackblitz.com/edit/github-l3ak3iob-jzvjqhdg?file=src%2Fcomponents%2FHighlightedCode.tsx
1. Link from
@sokidjs/meta
does not work - normal link does !? (SSR works and also client side navigation and theme change).
2. added location.pathname
to createEffect
which calls hljs.highlightAll() to also trigger hljs to add the classes to the pre and code elements. Necessary as hljs manipulates the DOM so it's necessary to call the update everytime the DOM changes (e.g. route change).
Reduced example for css (note remove the theme toggle) Just added it for testing:
mhm, yho i found it and fixed it as said above
wasn't nesscarry