react-fast-highlight

WebJar for react-fast-highlight

License

License

MIT
Categories

Categories

React User Interface Web Frameworks
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

react-fast-highlight
Last Version

Last Version

2.2.2
Release Date

Release Date

Type

Type

jar
Description

Description

react-fast-highlight
WebJar for react-fast-highlight
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/researchgate/react-fast-highlight

Download react-fast-highlight

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/react-fast-highlight/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>react-fast-highlight</artifactId>
    <version>2.2.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/react-fast-highlight/
implementation 'org.webjars.npm:react-fast-highlight:2.2.2'
// https://jarcasting.com/artifacts/org.webjars.npm/react-fast-highlight/
implementation ("org.webjars.npm:react-fast-highlight:2.2.2")
'org.webjars.npm:react-fast-highlight:jar:2.2.2'
<dependency org="org.webjars.npm" name="react-fast-highlight" rev="2.2.2">
  <artifact name="react-fast-highlight" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='react-fast-highlight', version='2.2.2')
)
libraryDependencies += "org.webjars.npm" % "react-fast-highlight" % "2.2.2"
[org.webjars.npm/react-fast-highlight "2.2.2"]

Dependencies

compile (3)

Group / Artifact Type Version
org.webjars.npm : classnames jar [2.2.3,3)
org.webjars.npm : highlight.js jar [9.0.0,10)
org.webjars.npm : prop-types jar [15.5.6,16)

Project Modules

There are no modules declared in this project.

react-fast-highlight

A fast react component wrapper for highlight.js

Build Status codecov

Requirements

  • Requires react version 16
  • Requires highlight.js version 9 or 10

Install

npm install --save react-fast-highlight react highlight.js

or

yarn add react-fast-highlight react highlight.js

Usage

import React, { Component } from 'react';
import { Highlight } from 'react-fast-highlight';

class App extends Component {

  render() {
    const code = 'let t = 0.5 * 5 * 4;';

    return (
      {/*
            `languages` is an optional property to set the languages that highlight.js should pick from.
            By default it is empty, which means highlight.js will pick from all available languages.
            If only one language is provided, this language will be used without doing checks beforehand.
            (Be aware that automatic language detection is not as fast as when specifing a language.)

            `worker` is used to take advantage of the possibility to do the highlighting work in a
            web-worker. As different module-bundlers use different ways to load web-workers, it is up
            to you to load the webworker and supply it to the highlight component. (see example)
            Currently every instance of the highlight component needs its own web-worker.

            `className` sets additional class names on the generated html markup.
       */}
      <Highlight
        languages={['js']}
        className="my-class"
      >
        {code}
      </Highlight>
    );
}

Advanced Usage

Custom highlight.js distribution

In cases where you bundle this component with a module bundler such as webpack, rollup or browserify and you know upfront which languages you want to support you can supply a custom distribution of highlight.js. This ensures you are not bundling all available languages of highlight.js which reduces the size of your bundle.

A custom distribution might look like this

import hljs from 'highlight.js/lib/highlight';

// Lets only register javascript, scss, html/xml
hljs.registerLanguage('scss', require('highlight.js/lib/languages/scss'));
hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'));
hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));

export default hljs;

To actually use a custom distribution you need to use the BareHighlight component. With it you can build your wrapper for highlighting code.

import React, { Component } from 'react';
import BareHighlight from 'react-fast-highlight/lib/js/BareHighlight';
import hljs from './customhljs';

class CustomHighlight extends Component {
  render() {
    const { children, ...props } = this.props;
    return (
      <BareHighlight {...props} highlightjs={hljs}>
        {children}
      </BareHighlight>
    );
}

Now you can use this wrapper the same way as the default Highlight component with only support for certain languages included.

In case you also want to use a webworker you should not use the supplied worker, as it includes all languages. Instead you will need to copy the worker from this repo and adjust the highlight.js import.

Webworker

It wasn't tested with browserify and rollup but it should work. If you managed to get it working please open a PR with the necessary changes and the documentation.

Webpack

To make web-workers working with webpack you additionally need to install worker-loader.

npm install --save worker-loader react-fast-highlight

import React from 'react';
import { Highlight } from 'react-fast-highlight';
import Worker from 'worker!react-fast-highlight/lib/js/worker';

class App extends React.Component {

  render() {
    const code = 'let t = 0.5 * 5 * 4;';

    return (
      <Highlight
        languages={['js']}
        worker={new Worker}
      >
        {code}
      </Highlight>
    );
}

License

react-fast-highlight is licensed under the MIT license.

org.webjars.npm

ResearchGate

Connect the world of science. Make research open to all.

Versions

Version
2.2.2