-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for diff-highlight #90
Comments
Interested! I'd like to have diff + javascript for instance. |
this is my code const isDiff = language.startsWith('diff-')
let highlightStyle = []
let code = children
if (isDiff) {
code = []
language = language.substr(5)
highlightStyle = children.split('\n').map((line) => {
if (line.startsWith('+')) {
code.push(line.substr(1))
return 'inserted'
}
if (line.startsWith('-')) {
code.push(line.substr(1))
return 'deleted'
}
code.push(line)
})
code = code.join('\n')
} https://github.com/maqi1520/mdx-editor/blob/main/src/components/MDX/CodeBlock.jsx |
You can check my solution for this docusaurus issue facebook/docusaurus#3318 on a possible apporach to remiplement prism plugins for prism-react-renderer using hooks |
You don't need to rewrite the plugins to make this work. I assign the tokens to a ref and then run the const codeBlock: string = 'hello world';
const editorLanguage:string = 'my-custom-lang';
const tokensRef = React.useRef<Token[][]>([]);
useLayoutEffect(() => {
Prism.hooks.run('complete', {
code: codeBlock,
grammar: Prism.languages[editorLanguage],
language: editorLanguage,
tokens: tokensRef.current,
element: codeRef.current,
});
}, [tokensRef.current]);
return (
<Highlight code={codeBlock} language={editorLanguage} prism={Prism} theme={talkwalkerTheme}>
{({ className, style, tokens, getLineProps, getTokenProps }) => {
tokensRef.current = tokens;
return (
<pre className="prism-highlighting" aria-hidden="true">
<code
className={`${className} match-braces rainbow-braces no-keep-markup`} // class names used by plugins
ref={codeRef}>
{tokens.map((line, i) => line.map((token, key) => <span key={key} {...getTokenProps({ token })} />))}
</code>
</pre>
);
}}
</Highlight>
); |
Related to #2 and facebook/docusaurus#3318
Prism comes with a useful plugin: diff-highlight for combining syntax highlighting and diff highlighting.
Since prism-react-renderer does not support plugins, it would be interesting to reimplement this plugin directly in prism-react-renderer. This can increase quite significantly the readability of diffs.
The text was updated successfully, but these errors were encountered: