Skip to content
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

Open
rrousselGit opened this issue Aug 21, 2020 · 4 comments
Open

Add support for diff-highlight #90

rrousselGit opened this issue Aug 21, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@rrousselGit
Copy link

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.

@kud
Copy link
Contributor

kud commented Jan 31, 2021

Interested! I'd like to have diff + javascript for instance.

@maqi1520
Copy link

maqi1520 commented Apr 21, 2022

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

image

@carbonrobot carbonrobot added the enhancement New feature or request label Feb 7, 2024
@thefat32
Copy link

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 "before-tokenize" and "after-tokenize"

@nabak9
Copy link

nabak9 commented Apr 23, 2024

You don't need to rewrite the plugins to make this work.
I got my plugins to work with a few lines of code, I hope this will help everyone that has been waiting for so long:

I assign the tokens to a ref and then run the 'complete' hook that calls the plugins registered to prism in useLayoutEffect as I need a reference to my code element.

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>
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants