Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/researchgate/react-fast-highlight
A fast react component wrapper for highlight.js
https://github.com/researchgate/react-fast-highlight
highlightjs react
Last synced: about 2 months ago
JSON representation
A fast react component wrapper for highlight.js
- Host: GitHub
- URL: https://github.com/researchgate/react-fast-highlight
- Owner: researchgate
- License: mit
- Created: 2016-01-08T22:37:52.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2023-07-18T20:17:22.000Z (over 1 year ago)
- Last Synced: 2024-10-30T04:55:53.284Z (2 months ago)
- Topics: highlightjs, react
- Language: TypeScript
- Homepage:
- Size: 3.63 MB
- Stars: 40
- Watchers: 10
- Forks: 6
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# react-fast-highlight
###### A fast react component wrapper for highlight.js
[![codecov](https://codecov.io/gh/researchgate/react-fast-highlight/branch/master/graph/badge.svg)](https://codecov.io/gh/researchgate/react-fast-highlight)
## 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
```js
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.
*/}
{code}
);
}
```### 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
```js
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.```js
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 (
{children}
);
}
```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`
```js
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 (
{code}
);
}
```## License
react-fast-highlight is licensed under the MIT license.