react-syntax-highlighter
sane syntax highlighting component for react
Last updated 8 years ago by conorhastings .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install react-syntax-highlighter 
SYNC missed versions from official npm registry.

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

  • language - the language to highlight code in. Available options here for hljs and here for prism. (pass text to just render plain monospaced text)
  • style - style object required from styles/hljs or styles/prism directory depending on whether or not you are importing from react-syntax-highlighter or react-syntax-highlighter/prism directory here for hljs. and here for prism. import { style } from 'react-syntax-highlighter/dist/esm/styles/{hljs|prism}' . Will use default if style is not included.
  • children - the code to highlight.
  • customStyle - prop that will be combined with the top level style on the pre tag, styles here will overwrite earlier styles.
  • codeTagProps - props that will be spread into the <code> tag that is the direct parent of the highlighted code elements. Useful for styling/assigning classNames.
  • useInlineStyles - if this prop is passed in as false, react syntax highlighter will not add style objects to elements, and will instead append classNames. You can then style the code block by using one of the CSS files provided by highlight.js.
  • showLineNumbers - if this is enabled line numbers will be shown next to the code block.
  • showInlineLineNumbers - if this is enabled in conjunction with showLineNumbers, line numbers will be rendered into each line, which allows line numbers to display properly when using renderers such as react-syntax-highlighter-virtualized-renderer. (This prop will have no effect if showLineNumbers is false.)
  • startingLineNumber - if showLineNumbers is enabled the line numbering will start from here.
  • lineNumberContainerStyle - the line numbers container default to appearing to the left with 10px of right padding. You can use this to override those styles.
  • lineNumberStyle - inline style to be passed to the span wrapping each number. Can be either an object or a function that receives current line number as argument and returns style object.
  • wrapLines - a boolean value that determines whether or not each line of code should be wrapped in a parent element. defaults to false, when false one can not take action on an element on the line level. You can see an example of what this enables here
  • wrapLongLines - boolean to specify whether to style the <code> block with white-space: pre-wrap or white-space: pre. Demo
  • lineProps - props to be passed to the span wrapping each line if wrapLines is true. Can be either an object or a function that receives current line number as argument and returns props object.
  • renderer - an optional custom renderer for rendering lines of code. See here for an example.
  • PreTag - the element or custom react component to use in place of the default pre tag, the outermost tag of the component (useful for custom renderer not targeting DOM).
  • CodeTag - the element or custom react component to use in place of the default code tag, the second tag of the component tree (useful for custom renderer not targeting DOM).
  • spread props pass arbitrary props to pre tag wrapping code.
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;

Built with React Syntax Highlighter

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

License

MIT

Current Tags

  • 12.1.0                                ...           beta (5 years ago)
  • 15.5.0                                ...           latest (3 years ago)
  • 5.0.0-rc.1                                ...           next (8 years ago)

122 Versions

  • 15.5.0                                ...           3 years ago
  • 15.4.5                                ...           3 years ago
  • 15.4.4                                ...           3 years ago
  • 11.0.3                                ...           3 years ago
  • 15.4.3                                ...           4 years ago
  • 15.4.2                                ...           4 years ago
  • 15.4.1                                ...           4 years ago
  • 15.4.0                                ...           4 years ago
  • 15.3.1                                ...           4 years ago
  • 15.3.0                                ...           4 years ago
  • 15.2.1                                ...           4 years ago
  • 15.2.0                                ...           4 years ago
  • 15.1.0                                ...           4 years ago
  • 15.0.1                                ...           4 years ago
  • 15.0.0                                ...           4 years ago
  • 14.0.2                                ...           4 years ago
  • 14.0.0                                ...           4 years ago
  • 13.5.3                                ...           4 years ago
  • 13.5.2                                ...           4 years ago
  • 13.5.1                                ...           4 years ago
  • 13.5.0                                ...           4 years ago
  • 13.4.0                                ...           4 years ago
  • 13.3.1                                ...           4 years ago
  • 13.2.1                                ...           4 years ago
  • 13.2.0                                ...           4 years ago
  • 13.1.0                                ...           4 years ago
  • 13.0.0                                ...           4 years ago
  • 12.2.1                                ...           5 years ago
  • 12.1.0                                ...           5 years ago
  • 12.0.2                                ...           5 years ago
  • 12.0.1                                ...           5 years ago
  • 12.0.0                                ...           5 years ago
  • 11.0.2                                ...           5 years ago
  • 11.0.1                                ...           5 years ago
  • 11.0.0                                ...           5 years ago
  • 10.3.5                                ...           5 years ago
  • 10.3.4                                ...           5 years ago
  • 10.3.3                                ...           5 years ago
  • 10.3.2                                ...           5 years ago
  • 10.3.1                                ...           5 years ago
  • 10.3.0                                ...           5 years ago
  • 10.2.1                                ...           6 years ago
  • 10.2.0                                ...           6 years ago
  • 10.1.3                                ...           6 years ago
  • 10.1.2                                ...           6 years ago
  • 10.1.1                                ...           6 years ago
  • 10.1.0                                ...           6 years ago
  • 10.0.1                                ...           6 years ago
  • 10.0.0                                ...           6 years ago
  • 9.0.1                                ...           6 years ago
  • 9.0.0                                ...           6 years ago
  • 8.1.0                                ...           6 years ago
  • 8.0.1                                ...           6 years ago
  • 8.0.0                                ...           6 years ago
  • 7.0.5-beta.1                                ...           6 years ago
  • 7.0.4                                ...           7 years ago
  • 7.0.3                                ...           7 years ago
  • 7.0.2                                ...           7 years ago
  • 7.0.1                                ...           7 years ago
  • 7.0.0                                ...           7 years ago
  • 7.0.0-beta                                ...           7 years ago
  • 6.1.2                                ...           7 years ago
  • 6.1.1                                ...           7 years ago
  • 6.1.0                                ...           7 years ago
  • 6.0.4                                ...           7 years ago
  • 6.0.3                                ...           7 years ago
  • 6.0.2                                ...           7 years ago
  • 6.0.1                                ...           7 years ago
  • 6.0.1-beta                                ...           7 years ago
  • 6.0.0                                ...           7 years ago
  • 5.8.0                                ...           7 years ago
  • 5.7.1                                ...           7 years ago
  • 5.7.0                                ...           7 years ago
  • 5.6.3                                ...           7 years ago
  • 5.6.2                                ...           7 years ago
  • 5.6.1                                ...           7 years ago
  • 5.6.0                                ...           8 years ago
  • 5.5.2                                ...           8 years ago
  • 5.5.1                                ...           8 years ago
  • 5.5.0                                ...           8 years ago
  • 5.4.1                                ...           8 years ago
  • 5.4.0                                ...           8 years ago
  • 5.3.0                                ...           8 years ago
  • 5.1.3                                ...           8 years ago
  • 5.1.2                                ...           8 years ago
  • 5.1.1                                ...           8 years ago
  • 5.1.0                                ...           8 years ago
  • 5.0.0                                ...           8 years ago
  • 5.0.0-rc.1                                ...           8 years ago
  • 4.0.1                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.11.0                                ...           8 years ago
  • 2.10.0                                ...           8 years ago
  • 2.9.0                                ...           8 years ago
  • 2.8.0                                ...           8 years ago
  • 2.7.1                                ...           8 years ago
  • 2.7.0                                ...           8 years ago
  • 2.6.1                                ...           8 years ago
  • 2.6.0                                ...           8 years ago
  • 2.5.0                                ...           8 years ago
  • 2.4.0                                ...           8 years ago
  • 2.3.0                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.1                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.4                                ...           8 years ago
  • 2.0.3                                ...           9 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.3.0                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.2                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.0.2                                ...           9 years ago
  • 0.0.1                                ...           9 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (16)

© 2010 - cnpmjs.org x YWFE | Home | YWFE