GithubHelp home page GithubHelp logo

react-syntax-highlighter / react-syntax-highlighter Goto Github PK

View Code? Open in Web Editor NEW
4.0K 4.0K 272.0 48.29 MB

syntax highlighting component for react with prismjs or highlightjs ast using inline styles

Home Page: http://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/

License: MIT License

JavaScript 100.00%

react-syntax-highlighter's Introduction

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

react-syntax-highlighter's People

Contributors

0xflotus avatar armanio avatar bmathews avatar bridgear avatar byeokim avatar cheapsteak avatar conorhastings avatar dependabot[bot] avatar dschaller avatar evenchange4 avatar exkazuu avatar forresto avatar fracture91 avatar karlhorky avatar ktmud avatar marcodejongh avatar marktinsley avatar maxmcd avatar mkosir avatar patrykkopycinski avatar pd4d10 avatar prateek3255 avatar prichey avatar rememberlenny avatar rusackas avatar simmerer avatar szhu avatar twe4ked avatar urish avatar vinnl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-syntax-highlighter's Issues

React.createElement: type should not be null, undefined, boolean, or number

I'm using React 15.0.0

Full stacktrace:

warning.js:44 Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of HtmlTab.warning @ warning.js:44ReactElementValidator.createElement @ ReactElementValidator.js:221HtmlTab_render @ HtmlTab.jsx:48(anonymous function) @ makeAssimilatePrototype.js:15ReactCompositeComponentMixin._renderValidatedComponentWithoutOwnerOrContext @ ReactCompositeComponent.js:679ReactCompositeComponentMixin._renderValidatedComponent @ ReactCompositeComponent.js:699ReactCompositeComponent__renderValidatedComponent @ ReactPerf.js:66ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:284ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39mountComponentIntoNode @ ReactMount.js:103Mixin.perform @ Transaction.js:136batchedMountComponentIntoNode @ ReactMount.js:124Mixin.perform @ Transaction.js:136ReactDefaultBatchingStrategy.batchedUpdates @ ReactDefaultBatchingStrategy.js:63batchedUpdates @ ReactUpdates.js:97ReactMount._renderNewRootComponent @ ReactMount.js:277ReactMount__renderNewRootComponent @ ReactPerf.js:66ReactMount._renderSubtreeIntoContainer @ ReactMount.js:354ReactMount.render @ ReactMount.js:374React_render @ ReactPerf.js:66(anonymous function) @ mainApp.jsx:34(anonymous function) @ mainApp.jsx:75(anonymous function) @ mainApp.jsx:76(anonymous function) @ main.jsundefined:1264__webpack_require__ @ main.jsundefined:545fn @ main.jsundefined:76(anonymous function) @ multi_main:3(anonymous function) @ main.jsundefined:578__webpack_require__ @ main.jsundefined:545(anonymous function) @ main.jsundefined:568(anonymous function) @ main.jsundefined:571 invariant.js:38

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of HtmlTab.invariant @ invariant.js:38instantiateReactComponent @ instantiateReactComponent.js:66instantiateChild @ ReactChildReconciler.js:28traverseAllChildrenImpl @ traverseAllChildren.js:98traverseAllChildren @ traverseAllChildren.js:186ReactChildReconciler.instantiateChildren @ ReactChildReconciler.js:51ReactMultiChild.Mixin._reconcilerInstantiateChildren @ ReactMultiChild.js:159ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:196ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactMultiChild.Mixin.mountChildren @ ReactMultiChild.js:203ReactDOMComponent.Mixin._createInitialChildren @ ReactDOMComponent.js:617ReactDOMComponent.Mixin.mountComponent @ ReactDOMComponent.js:465ReactDOMComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39ReactCompositeComponentMixin.performInitialMount @ ReactCompositeComponent.js:290ReactCompositeComponentMixin.mountComponent @ ReactCompositeComponent.js:237ReactCompositeComponent_mountComponent @ ReactPerf.js:66ReactReconciler.mountComponent @ ReactReconciler.js:39mountComponentIntoNode @ ReactMount.js:103Mixin.perform @ Transaction.js:136batchedMountComponentIntoNode @ ReactMount.js:124Mixin.perform @ Transaction.js:136ReactDefaultBatchingStrategy.batchedUpdates @ ReactDefaultBatchingStrategy.js:63batchedUpdates @ ReactUpdates.js:97ReactMount._renderNewRootComponent @ ReactMount.js:277ReactMount__renderNewRootComponent @ ReactPerf.js:66ReactMount._renderSubtreeIntoContainer @ ReactMount.js:354ReactMount.render @ ReactMount.js:374React_render @ ReactPerf.js:66(anonymous function) @ mainApp.jsx:34(anonymous function) @ mainApp.jsx:75(anonymous function) @ mainApp.jsx:76(anonymous function) @ main.jsundefined:1264__webpack_require__ @ main.jsundefined:545fn @ main.jsundefined:76(anonymous function) @ multi_main:3(anonymous function) @ main.jsundefined:578__webpack_require__ @ main.jsundefined:545(anonymous function) @ main.jsundefined:568(anonymous function) @ main.jsundefined:571 8ReactDOMComponentTree.js:105

Uncaught TypeError: Cannot read property '__reactInternalInstance$0rtr6rqckkdzhlrxlg3vt8d7vi' of null

Duplicate package highlight.js

Hello @conorhastings.

There is duplication of highlight.js in dist build of your lib.

Your lib require highlight.js": ~9.8.0 and lowlight": ^1.5.0
The lowlight that is end up in use is "1.9.0" i.e the latest.
And that latest lowlight is using highlight.js": "~9.12.0

So webpack include both version.

As i can see there are several possible solutions.

  • go from "Caret Ranges" to "Tilde Ranges" for lowlight and fixate version more tight.
  • remove highlight.js from dependencies and use that of lowlight

How to provide custom classes

How do I customize the styling of code string I'm providing to the Highlighter.
Example I've a string JSON available and I want the color of attributes to be different than shipped with themes.

No way to change font with inline styles

Browsers have the following default css:

code {
  font-family: monospace;
}

That means that currently there's no way to change the font when using react-syntax-highlighter without a <style> element overriding that. If you add a fontFamily style to the <pre> element, it has no effect.

It seems like a good solution would be to add fontFamily: inherit to the <code> element, but that'd be a breaking change for some, probably.

[Question] line style does not work as I expected

Sorry to create an issue here.
Now I am using this component to create a git diff view component. I found if some of the codes are too long to overflow the width of highlighter, the background of line will be cut. You can get more info from below screenshot.
image

If this issue is not a good issue, please feel free to close it. :)

Lots of styles missing a background color

A large number of the styles have no defined background color, e.g. ardunio-light, arta, brown-paper, color-brewer, etc.

This can be seen by selecting on one of these styles on the demo page.

for react native?

Hello is there any plan to build this for react-native?

thanks!

๐Ÿ‘

Open source license?

This project currently doesn't have a LICENSE file so it's unknown what can be done with this code. Would it be possible to add a license to this so it can safely be used by consuming projects?

https://choosealicense.com/

react 15.2.0 makes warning error

react is updated to 15.2.0 and it makes following error message. All of us who manage packages which depend on react should fix this...

warning.js:44 Warning: Unknown prop `language` on <pre> tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop
    in pre (created by SyntaxHighlighter)
    in SyntaxHighlighter (created by Highlighter)
    in div (created by Highlighter)
    in Highlighter (created by Post)
    in article (created by Post)
    in div (created by Post)
    in Post (created by Connect(Post))
    in Connect(Post) (created by Connect(Connect(Post)))
    in Connect(Connect(Post)) (created by RouterContext)
    in div (created by App)
    in div (created by App)
    in App (created by Connect(App))
    in Connect(App) (created by RouterContext)
    in RouterContext (created by AsyncConnect)
    in AsyncConnect (created by Connect(AsyncConnect))
    in Connect(AsyncConnect) (created by Router)
    in Router (created by Root)
    in Provider (created by Root)
    in Root
    in AppContainer

Each line wrapped in a div?

Would it make sense to wrap each line in a div? My use case is that I'll be using this to highlight code coverage, so I'd like to have some lines with a different background color than other lines. I think this could also be useful for allowing people to highlight line ranges or click on the line number gutter to link to lines of code, like how GitHub does in source view.

provide interface to lowlight.registerLanguage

this might make the light build a bit less clunky feeling, basically thinking it can just be named exprort which would allow doing something like

import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/dist/light';

instead of having to import lowlight directly, what do you think @bmathews

`Unknown prop` warnings

Got this warning:

warning.js:44Warning: Unknown prop `stylesheet` on <pre> tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop
    in pre (created by SyntaxHighlighter)
    in SyntaxHighlighter ...

Hook events

Hi!

Is there a way to hook on the render events or any events?
If I have a longer code of html for example rendering, then it takes a lot of time and the UI is blocking that way so the best would be if I could hook on the event when the rendering of the code is finished and display a loader in the meanwhile.

Not able to require the styles?

Getting a Uncaught Error: Cannot find module './styles/docco' for

import SyntaxHighlighter from 'react-syntax-highlighter';
const Component = () => {
  const codeString = '(num) => num + 1';
  return <SyntaxHighlighter language='javascript' stylesheet='docco'>{codeString}</SyntaxHighlighter>;  
}

JSX support

Highlight.js claims to support JSX, but you can tell by the screenshots below that JSX entered into react-syntax-highlighter does not get the correct coloring applied to the tags. Is this an issue with this library, or with Highlight.js itself?

screenshot from 2016-05-05 22-13-43

screenshot from 2016-05-05 22-15-42

Light Build and Webpack 2 still load all highlight.js languages

Hi folks.

Thank you for this awesome package.

Today I was to raise an issue related wit bundle size.

I am trying to use the light build as you can see in the following code

import SyntaxHighlighter, { registerLanguage } from "react-syntax-highlighter/dist/light"

import foundation from 'react-syntax-highlighter/dist/styles/foundation';

['javascript', 'php', 'xml', 'bash', 'markdown', 'json', 'yaml'].forEach((langName) => {
    const langModule = require(`react-syntax-highlighter/dist/languages/${langName}`);
    registerLanguage(langName, langModule);
});

But when you use Webpack 2 to create a package, the tree shaking logic doesn't work as you can see in the following image.
all-languajes-includes

I asked in webpack forums, and people mentioned that maybe internally maybe all languages are included.

I tried also trying to exclude with the same result

new webpack.IgnorePlugin(/^\.\/lib\/languages$/, /highlight\.js/),

I would really appreacite any recommendation.

lineStyles attached to line children and missing line numbers

screen shot 2017-05-25 at 14 17 28

I have as you can see, in the picture, a problem with the lineStyles.
At first, they are attached to the child spans, with this I have the problem that I get unexpected line breaks. Probably are this line breaks also the problem why the line numbers break up on line 40 (I used the line numbers just for be debugging purposes).
I load the code via redux-sagas and bind them then like this:

<SyntaxHighlighter
      language='java'
      wrapLines
      lineStyle={(lineNumber: number) => {
         const style = { display: '', backgroundColor: '' };
         if (this.state.fileCoverage.COVERED.includes(lineNumber)) {
              style.display = 'block';
              style.backgroundColor = '#A9DBB8';
         } else if (this.state.fileCoverage.COVERED_NEW.includes(lineNumber)) {
             style.display = 'block';
             style.backgroundColor = '#EDE4AF';
         } else if (this.state.fileCoverage.UNCOVERED.includes(lineNumber)) {
             style.display = 'block';
             style.backgroundColor = '#BA2D0B';
         }
         return style;
      }}
      showLineNumbers
    >
     {this.props.codeFile}
</SyntaxHighlighter>

Thanks

Massive filesize

This package just added 600kb to my production bundle. I actually really like this lib, but the filesize could be a dealbreaker for me. I tried not importing styles but filesize is still the same. Right now my only workaround is to include react-syntax-highlighter in my vendor bundle and exclude it from the main app bundle.

https://gyazo.com/c9bbe6b0f9fd5917c3abb020ce980c14

The only comfort I have from this is that it's down to 200+kb after gzipped.

Option to use standard style sheets / class names

Hey, there.

Was really happy to see that someone had finally made an actual React syntax highlighter, not using dangerouslySetInnerHTML or other such undesirable methods. Good job.

I was, however, quickly disappointed when I saw that this uses inline styles, and inline styles only.

I am not a fan of this method of styling, there are numerous downsides over using standard CSS.

I was hoping to use react-syntax-highlighter for the documentation for my latest library that hopes to remove the need / want for inline styles completely... so you can see how using react-syntax-highlighter for the documentation would be kind of contradictory.

Is there any chance you could add the ability to choose to use either inline styles, or class names? Unfortunately until such a feature is added, I cannot use this library.

Custom styles

How can I have custom styles? The natural way is using style prop or CSS classes, but we have style for theming and, in my project, I can't use css classes.

Disable syntax highlighting

First of all thank you for react-syntax-highlighter. We use it in atlaskit library and it's great!

There's an issue which we're still missing in react-syntax-highlighter: disable syntax highlighting. It looks like lowlight.js is also missing this feature while highlight.js supports this through classnames.

I looked into into react-syntax-highlighter code and didn't find an easy way to do this. In our code the easiest way to do this is to do a manual check and mimic react-syntax-highlight HTML layout on our part:

image

But it would be great to have "text" language support in react-syntax-highlighter

Update README on npm

The README on npm doesn't correctly show how to retrieve the script styles, it says

import { docco } from 'react-syntax-highlighter/styles';

while it should be

import { docco } from 'react-syntax-highlighter/dist/styles';

It should be a quick fix :)

code folding

Is there any good way to implement json/html code folding with react-syntax-highlighter?

Unable to registerLanguage

When attempting to lighten my package using the registerLanguage example provided in the README, it doesn't seem to work. In fact the component seems to lose all styling. No errors or warnings are visible in the console, but the following code:

import React, { Component } from 'react'
import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/dist/light'
import js from 'highlight.js/lib/languages/javascript'

registerLanguage('javascript', js);

class FileCoverage extends Component {
  render() {
    const codeString = '(num) => num + 1';
    return (
      <SyntaxHighlighter showLineNumbers language='javascript'>
        {codeString}
      </SyntaxHighlighter>
    )
  }
}

Renders a very basic:

no style

However when I use this code:

import React, { Component } from 'react'
import SyntaxHighlighter from 'react-syntax-highlighter'

class FileCoverage extends Component {
  render() {
    const codeString = '(num) => num + 1';
    return (
      <SyntaxHighlighter showLineNumbers language='javascript'>
        {codeString}
      </SyntaxHighlighter>
    )
  }
}

The component is rendered with color and you can even see the style is applied via the grey background:

style

Highlight why to use this project

There are other react highlighters out there, but they all use 'dangerouslySetInnerHTML', whereas this project does not. Which is very cool, and I think you should highlight it :)

`wrapLines=true` removes leading whitespace before `<`

Input

import SyntaxHighlighter from 'react-syntax-highlighter';
<SyntaxHighlighter language="javascript" wrapLines={true}>
  { " (\n  <br/>\n );" }
</SyntaxHighlighter>

Actual output

 (
<br/>
 );

Expected output

 (
  <br/>
 );

The output is as expected with wrapLines={false}.
Update: It's also correct with language="html".

Cannot read property 'exports' of undefined

Hi,

first of all, thanks for this awesome project.

A few days ago everything worked fine for me. Now if i npm start my project, the console tells me that some method of highlight.js ->

.... module.exports = function(hljs) { var CPP = hljs.getLanguage('cpp').exports; .....

seems to have following problem "Cannot read property 'exports' of undefined". Could it be that it has to do with Babel 6 features for export -> webpack/webpack#706? I tried to fix this myself, but failed until now. Maybe you can tell me whats this about? Would be awesome. Thanks!

EDIT:
Possible solution
http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default
https://www.npmjs.com/package/babel-plugin-add-module-exports

kind regards,
defuex

`wrapLines` sometimes wraps more than one line

<SyntaxHighlighter language="javascript" wrapLines={true} lineStyle={{border: '1px dotted green'}}>
  {"ReactDOM.render(\n  <MyComponent />,\n  document.getElementById('application')\n);"}
</SyntaxHighlighter>

Actual

Line 2,3, and 4 share a common highlighter line border.

Expected

Each of the four lines gets its own border.

Provide ability to make contentEditable

This is probably one of the cleanest methods of syntax highlighting I've found that is easy to style and uses popular open source technology.
My biggest issue is that I can't use it because I need to be able to edit the highlighted code.

Maybe one day I'll find myself with enough free time to dig into that, but if you have that in the works, it would be an awesome introduction to this library.

Unknown language breaks highlighter

I'm using react-syntax-highlighter to highlight markdown user input. The user can manually type in the language that is being used in fenced code block style, e.g.

```js

The problem is that when the user is typing in the language, i.e. they hit j in the example above, react-syntax-highlighter tries to render in the language j, which means it throws an "unknown language" error and stops rendering correctly:

Uncaught Error: Unknown language: `j` is not registered
    at coreHighlight (core.js:159)

Is there a way to either ignore these errors (and just revert to autodetection if the language is not loaded) or to find out whether a given language is registered prior to setting it as a prop?

light build highlight.js version

I really appreciate this project, and the light build directions.

With

import js from 'highlight.js/lib/languages/javascript';

though I'm ending up with different versions of highlight.js in my project's node_modules vs react-syntax-highlighter's node_modules. It works, but I wonder if you could make a script similar to build-stylesheets.js for the languages, so that the import could look more like:

import js from 'react-syntax-highlighter/dist/languages/javascript';

& maybe not need the registerLanguage('javascript', js);. I'd be happy to give it a shot if that sounds good to you.

Add support for line numbers

You should be able to automatically show line numbers via a showLineNumbers prop, and, additionally, you should be able to say what line number to start at (firstLineNumber?).

This would be very useful for when you want to show an entire file, but you want to put other things (like code review comments perhaps) between lines.

Error rendering in core.js

Love the library, I agree with all the underlying principals! Having issues (at least in the lite version):

Uncaught (in promise) Error: Expected `string` for value, got `var a = "wat";`

I think this is due to children being an array and being passed directly through? Here's my usage:

import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/dist/light';
import js from 'react-syntax-highlighter/dist/languages/javascript';
import docco from 'react-syntax-highlighter/dist/styles/docco';

registerLanguage('javascript', js);

// render() =>
<SyntaxHighlighter language="javascript" style={docco}>
{ 'var a = "wat";' }
</SyntaxHighlighter>

IE and Android support

Bundle Not working on IE 9-11 and Android 4.4 webview.

react-syntax-highlighter/dist/highlight.js line 31, Object doesn't support property or method 'assign'

// line 7
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

// line 31
var preProps = useInlineStyles ? Object.assign({}, rest, { style: Object.assign({}, defaultPreStyle, customStyle) }) : Object.assign({}, rest, { className: 'hljs' });

Build size - Lowlight dependency

I was analyzing my bundle size with this tool: https://chrisbateman.github.io/webpack-visualizer/

It results show that node_modules are around 95% of my bundle size, and out of that 95%, 28% is taken by lowlight.

I see that you have a Light Build option, but im not sure how this would impact my final bundle size if i still have to load lowlight for it to work.

Is there a big reduction related to how lowlight manages the code if i choose the Light Build way ?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.