Question Desp: How could I highlight a custom syntax with prismjs in a react project?
I've created an extending syntax file (let's call it newlang
) by following the guide.
And I know in prismjs there are three ways to highlight code:
highlightElement()
highlightAll()
highlight()
First, I tried to require my custom syntax file in my app.
import 'utils/highlight/prism-newlang';
Then, in my highlighting component,
- Method 1:
highlightElement()
: ❌CANNOT get my code highlighting
import Prism from 'prismjs';
function CodeHighlight({ value, language }) {
const codeEle = useRef(null);
useEffect(() => {
Prism.highlightElement(codeEle.current, false);
}, []);
return (
<pre className={`language-${language}`}>
<code ref={codeEle} className={`language-${language}`}>
{value}
</code>
</pre>
);
}
- Method 2:
highlightAll()
: ❌CANNOT get my code highlighting - Method 3:
highlight()
: ✅CAN get my code highlighting
import { highlight, languages } from 'prismjs/components/prism-core';
function CodeHighlight({ value, language }) {
const codeNode = useRef(null);
useEffect(() => {
const code = codeNode.current.textContent;
const highlightHTML = highlightCode(code, language);
codeNode.current.innerHTML = highlightHTML;
});
return (
<pre className={`language-${language}`}>
<code ref={codeNode} className={`language-${language}`}>
{value}
</code>
</pre>
);
}
I wonder what's the problem of Method 1 & 2. (Will be the matter of the loading order of custom syntax file?)