react-syntax-highlighter

React Syntax Highlighter

Actions Status npm

Syntax highlighting component for React using the seriously super amazing lowlight and refractor by wooorm

Check out a small demo here and see the component in action highlighting the generated test code here.

For React Native you can use react-native-syntax-highlighter

Install

npm install react-syntax-highlighter --save

Why This One?

There are other syntax highlighters for React out there so why use this one? The biggest reason is that all the others rely on triggering calls in componentDidMount and componentDidUpdate to highlight the code block and then insert it in the render function using dangerouslySetInnerHTML or just manually altering the DOM with native javascript. This utilizes a syntax tree to dynamically build the virtual dom which allows for updating only the changing DOM instead of completely overwriting it on any change, and because of this it is also using more idiomatic React and allows the use of pure function components brought into React as of 0.14.

Javascript Styles!

One of the biggest pain points for me trying to find a syntax highlighter for my own projects was the need to put a stylesheet tag on my page. I wanted to provide out of the box code styling with my modules without requiring awkward inclusion of another libs stylesheets. The styles in this module are all javascript based, and all styles supported by highlight.js have been ported!

I do realize that javascript styles are not for everyone, so you can optionally choose to use css based styles with classNames added to elements by setting the prop useInlineStyles to false (it defaults to true).

Use

props

import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
const Component = () => {
  const codeString = '(num) => num + 1';
  return (
    <SyntaxHighlighter language="javascript" style={docco}>
      {codeString}
    </SyntaxHighlighter>
  );
};

Prism

Using refractor we can use an ast built on languages from Prism.js instead of highlight.js. This is beneficial especially when highlighting jsx, a problem long unsolved by this module. The semantics of use are basically the same although a light mode is not yet supported (though is coming in the future). You can see a demo(with jsx) using Prism(refractor) here.

import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { dark } from 'react-syntax-highlighter/dist/esm/styles/prism';
const Component = () => {
  const codeString = '(num) => num + 1';
  return (
    <SyntaxHighlighter language="javascript" style={dark}>
      {codeString}
    </SyntaxHighlighter>
  );
};

Light Build

React Syntax Highlighter used in the way described above can have a fairly large footprint. For those that desire more control over what exactly they need, there is an option to import a light build. If you choose to use this you will need to specifically import desired languages and register them using the registerLanguage export from the light build. There is also no default style provided.

import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
import js from 'react-syntax-highlighter/dist/esm/languages/hljs/javascript';
import docco from 'react-syntax-highlighter/dist/esm/styles/hljs/docco';

SyntaxHighlighter.registerLanguage('javascript', js);

You can require PrismLight from react-syntax-highlighter to use the prism light build instead of the standard light build.

import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import jsx from 'react-syntax-highlighter/dist/esm/languages/prism/jsx';
import prism from 'react-syntax-highlighter/dist/esm/styles/prism/prism';

SyntaxHighlighter.registerLanguage('jsx', jsx);

Async Build

For optimal bundle size for rendering ASAP, there’s a async version of prism light & light. This versions requires you to use a bundler that supports the dynamic import syntax, like webpack. This will defer loading of refractor (17kb gzipped) & the languages, while code splits are loaded the code will show with line numbers but without highlighting.

Prism version:

import { PrismAsyncLight as SyntaxHighlighter } from 'react-syntax-highlighter';

Highlight version

import { LightAsync as SyntaxHighlighter } from 'react-syntax-highlighter';

Supported languages

Access via the supportedLanguages static field.

SyntaxHighlighter.supportedLanguages;

Add support for another language

To add another language, use the light build and registerLanguage. For example to add cURL with highlight.js:

import { Light as LightSyntaxHighlighter } from 'react-syntax-highlighter';
import curl from 'highlightjs-curl';

Then you can do:

LightSyntaxHighlighter.registerLanguage('curl', curl);

Built with React Syntax Highlighter

If your project uses react-syntax-highlighter please send a pr to add!

License

MIT

Contributing

You’ll need Node 16.x installed & active on your system to build this package.

npm i
npm run dev