Skip to content

Commit ef01653

Browse files
committed
Fix custom hljs distribution
1 parent 36e696a commit ef01653

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ or
2121

2222
```js
2323
import React, { Component } from 'react';
24-
import { Highlight } from 'react-fast-highlight';
24+
import Highlight from 'react-fast-highlight';
2525

2626
class App extends Component {
2727

@@ -66,9 +66,9 @@ A custom distribution might look like this
6666
import hljs from 'highlight.js/lib/highlight';
6767

6868
// Lets only register javascript, scss, html/xml
69-
hljs.registerLanguage('scss', require('highlight.js/languages/scss'));
70-
hljs.registerLanguage('javascript', require('highlight.js/languages/javascript'));
71-
hljs.registerLanguage('xml', require('highlight.js/languages/xml'));
69+
hljs.registerLanguage('scss', require('highlight.js/lib/languages/scss'));
70+
hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'));
71+
hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));
7272

7373
export default hljs;
7474
```
@@ -78,7 +78,7 @@ you can build your wrapper for highlighting code.
7878
7979
```js
8080
import React, { Component } from 'react';
81-
import { BareHighlight } from 'react-fast-highlight';
81+
import BareHighlight from 'react-fast-highlight/lib/BareHighlight';
8282
import hljs from './customhljs';
8383

8484
class CustomHighlight extends Component {
@@ -93,7 +93,10 @@ class CustomHighlight extends Component {
9393
```
9494
9595
Now you can use this wrapper the same way as the default `Highlight` component with only support for
96-
certain languages.
96+
certain languages included.
97+
98+
> In case you also want to use a webworker you should not use the supplied worker, as it includes all
99+
> languages. Instead you will need to copy the worker from this repo and adjust the `highlight.js` import.
97100
98101
#### Webworker
99102

src/components/BareHighlight.js renamed to src/BareHighlight.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ export default class BareHighlight extends PureComponent {
6262
}
6363

6464
_highlightCode() {
65-
const { highlightjs, languages, worker } = this.props;
65+
const { languages, worker } = this.props;
6666

6767
if (worker) {
6868
worker.onmessage = event => this.setState({
6969
highlightedCode: event.data.value,
7070
language: event.data.language,
7171
});
72-
worker.postMessage({ code: this.getInitialCode(), languages, highlightjs });
72+
worker.postMessage({ code: this.getInitialCode(), languages });
7373
} else {
7474
this.getHighlightPromise()
7575
.then(
File renamed without changes.

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* @flow */
2-
import BareHighlight from './components/BareHighlight';
3-
import Highlight from './components/Highlight';
2+
import Highlight from './Highlight';
43

5-
export { BareHighlight, Highlight };
4+
export default Highlight;

src/worker.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/* eslint-env worker */
22
/* @flow */
3+
import hjs from 'highlight.js';
34

45
declare function postMessage(result: Object): void;
56

67
onmessage = (event) => {
7-
const { code, languages, highlightjs } = event.data;
8+
const { code, languages } = event.data;
89
let result;
910
if (languages && languages.length === 1) {
10-
result = highlightjs.highlight(languages[0], code, true);
11+
result = hjs.highlight(languages[0], code, true);
1112
} else {
12-
result = highlightjs.highlightAuto(code, languages);
13+
result = hjs.highlightAuto(code, languages);
1314
}
1415

1516
postMessage(result);

test/components/BareHighlight.js renamed to test/BareHighlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import td from 'testdouble';
33
import test from 'ava';
44
import { mount, shallow } from 'enzyme';
5-
import BareHighlight from '../../src/components/BareHighlight';
5+
import BareHighlight from '../src/BareHighlight';
66

77
test.cb('no language - calls correct highlightCall', (t) => {
88
const hjs = td.replace('highlight.js');

test/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import React from 'react';
22
import { shallow } from 'enzyme';
33
import test from 'ava';
4-
import { BareHighlight, Highlight } from '../src/index';
4+
import Highlight from '../src/index';
55

6-
test('exports Highlight component', (t) => {
7-
t.notThrows(() => shallow(<Highlight>test</Highlight>));
8-
});
9-
10-
test('exports BareHighlight component', (t) => {
6+
test('exports default component', (t) => {
117
t.notThrows(() => shallow(<Highlight>test</Highlight>));
128
});

0 commit comments

Comments
 (0)