Closed ArthurFlag closed 3 years ago
Your Prism setup is weird.
The JSON code is clearly highlighted as JSON (SSR I reckon) but the client-side instance of Prism doesn't have JSON.
Same with the Copy to clipboard plugin. That JSON code block clearly has a Copy button but your client-side Prism instance doesn't have that plugin loaded.
Even weirder: the client-side Prism instance only gets loaded after you hover over those links.
I think this lazy loading is the problem. Prism will auto highlight all code blocks on a page when loaded if Prism.manual
isn't true.
What happens is the following:
So why is the code reformatted?
It isn't. At least, not from Prism's perspective. Docusaurus2 or prism-react-renderer (whichever is responsible) doesn't use line breaks (\n or \r\n) but CSS flex layout to separate lines. This means that the text content of the <code>
element doesn't contain line breaks.
(Seems familiar, right?)
The problem is Prims re-highlighting the entire page. The solution is simple:
Set Prism.manual = true
. (See the docs on how to do this.)
Also, this fix has to be applied by whichever library loads/uses Prism. You can apply this fix on your website right now but please make an issue for the library responsible so they can fix it for everyone.
Thanks a lot @RunDevelopment, this is great news 👍
Let's ping @slorber. Is it something that can be set by default in Docusaurus?
Hey,
I'm on holiday soon so can't investigate much, but the Docusaurus site is able to render json properly:
https://docusaurus.io/docs/next/browser-support#default-values
So I guess the issue is related to @ArthurFlageul 's site specificities
The JSON code is clearly highlighted as JSON (SSR I reckon) but the client-side instance of Prism doesn't have JSON.
We have a webpack config to optimize the loading of Prism packages and reduce bundle size (if I remember correctly):
configureWebpack() {
const prismLanguages = additionalLanguages
.map((lang) => `prism-${lang}`)
.join('|');
return {
plugins: [
new ContextReplacementPlugin(
/prismjs[\\/]components$/,
new RegExp(`^./(${prismLanguages})$`),
),
],
};
},
Same with the Copy to clipboard plugin. That JSON code block clearly has a Copy button but your client-side Prism instance doesn't have that plugin loaded.
We don't use a copy to clipboard plugin, it's code from Docusaurus
I know we have a quite unusual setup (that I didn't work on much) and Prism is likely not the problem here.
I don't think Docusaurus either because we render json correctly, as well as many other doc sites (it's not like json is not a popular language...)
@ArthurFlageul I already mentioned this is not valid JSON:
"effects": [ { "campaignId": 377, "rulesetId": 1096, "ruleIndex": 0, "ruleName": "25% off", "effectType": "setDiscount", "triggeredByCoupon": 97534, "props": { "name": "25% off", "value": 66.25 }
Please add {}
around that code block first. I don't want to invest time solving problems we don't have in the first place 😅
Information:
Description
Go on any page of this website that contains a code block. Hover over the top-right links to the API reference docs, and the code blocks of the current page break.
It's a website built with Docusaurus2. I've reported the issue to them, but it doesn't seem to come from them, and I can't figure out the issue so I'm thinking it's part of Prism.
Example of formatted code block:
The same code block but broken:
Somehow,
codeBlockLines_39YC
loses its javascript class.Let me know if you need extra information.