How is code highlighting supposed to work? #5428
Replies: 3 comments 2 replies
-
My guess:In the lexical state, code block is CodeNode and inline code is TextNode. lexical/packages/lexical-code/src/CodeNode.ts Lines 104 to 113 in 9d247ac inline: lexical/packages/lexical/src/nodes/LexicalTextNode.ts Lines 99 to 102 in 9d247ac And I think code highlighting should be used with |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply here, but as you can see from my linked code sandbox (and the picture above) there is no
But without Code highlighting seems to work over at the Playground, but only if you first apply the code in markdown and then switch back. Typing the code out and then marking it as a code block from the rich editor's code button does not work (and in fact the formatting is different from the markdown->text path as well). The playground also does not emit |
Beta Was this translation helpful? Give feedback.
-
I see. I certainly missed adding the theme. Now I see the updated classes in each of the items of the deconstructed code block. I used the same theme as the playground, so it now seems quite similar. And so now highlighting is working as expected. Many thanks for your help here. I have used other highlighting tooling in the past that was far more opinionated at color scheme and such, so it was lost on me that both the theme object + CSS is required to make it work here. |
Beta Was this translation helpful? Give feedback.
-
The documentation on this is rather sparse (none?).
https://lexical.dev/docs/api/modules/lexical_code
https://github.com/facebook/lexical/blob/main/packages/lexical-playground/src/plugins/CodeHighlightPlugin/index.ts
I've pieced together this example from various sources online, as well as scouring the code and what documentation does exist (it's mostly just an API reference without any explanation of how to use it).
https://codesandbox.io/p/sandbox/stoic-pasteur-j3czn4
With the above code I can see in developer tools that the editor's code appears to be tokenized as you would expect ...
but no colors are applied
Am I missing something or is this an issue specifically with
@lexical/markdown
? From looking at the code I can see that prismjs is used internally. Though I am unable to get it to properly apply.Can code highlightning be used with either
RichTextPlugin
andPlainTextPlugin
? I've tried both.Also, shouldn't
<code>
generally be wrapped in<pre>
? Without that I'm not sure how to distinguishinline
code fromblock
code.Beta Was this translation helpful? Give feedback.
All reactions