npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@seafront/react-fast-highlight

v4.1.1

Published

A fast react component wrapper for highlight.js

Downloads

9

Readme

react-fast-highlight

A fast react component wrapper for highlight.js

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.