JSPM

  • Created
  • Published
  • Downloads 2365113
  • Score
    100M100P100Q207224F
  • License MIT

sane syntax highlighting component for react

Package Exports

  • react-syntax-highlighter
  • react-syntax-highlighter/dist/styles
  • react-syntax-highlighter/dist/styles/docco
  • react-syntax-highlighter/dist/styles/github
  • react-syntax-highlighter/dist/styles/github-gist
  • react-syntax-highlighter/dist/styles/rainbow
  • react-syntax-highlighter/dist/styles/tomorrow
  • react-syntax-highlighter/dist/styles/tomorrow-night
  • react-syntax-highlighter/dist/styles/tomorrow-night-blue

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-syntax-highlighter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

React Syntax Highlighter

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

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

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 fine tuned patching of the 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!

Use

Currently this modules requires that you build with webpack as it uses dynamic require statements

props

  • language - the language to highlight code in.
  • style - style object rquired from styles directory. here. import { style } from 'react-syntax-highlighter/styles' . Will use default if style is not included.
  • children - the code to highlight.
  • spread props pass arbitrary props to pre tag wrapping code.
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/styles';
const Component = () => {
  const codeString = '(num) => num + 1';
  return <SyntaxHighlighter language='javascript' style={docco}>{codeString}</SyntaxHighlighter>;  
}