├── .babelrc
├── .eslintrc
├── .gitignore
├── .npmignore
├── .travis.yml
├── LICENSE
├── README.md
├── index.js
├── package.json
├── src
├── index.js
└── optimized.js
└── test
└── highlight.test.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "env",
4 | "react"
5 | ]
6 | }
7 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "rules": {
3 | "no-extra-parens": 0,
4 | "react/jsx-uses-vars": 1,
5 | "global-strict": 1,
6 | "quotes": [2, "single"],
7 | "no-underscore-dangle": 0,
8 | "space-infix-ops": 0,
9 | "no-alert": 0
10 | },
11 | "ecmaFeatures": {
12 | "jsx": true,
13 | },
14 | "env": {
15 | "node": true,
16 | "browser": true,
17 | "es6": true,
18 | "jasmine": true
19 | },
20 | "parser": "babel-eslint",
21 | "plugins": [
22 | "react"
23 | ]
24 | }
25 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # Compiled binary addons (http://nodejs.org/api/addons.html)
20 | build/Release
21 |
22 | # Dependency directory
23 | # Commenting this out is preferred by some people, see
24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
25 | node_modules
26 |
27 | # Users Environment Variables
28 | .lock-wscript
29 | .sass-cache
30 | build
31 | lib
32 | package-lock.json
33 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | build
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "stable"
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Kiran Abburi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-highlight
2 |
3 | React component for syntax highlighting using highlight.js
4 |
5 | 
6 |
7 | ### Latest version
8 |
9 | `0.11.1`
10 |
11 | ## [Documentation](https://react-highlight.neostack.com/)
12 |
13 | ### CodeSandbox Example
14 |
15 | [](https://codesandbox.io/s/mj6wlmor9p)
16 |
17 | ### Installation
18 |
19 | ```bash
20 | npm install react-highlight --save
21 | ```
22 |
23 | ### Usage
24 |
25 | #### Importing component
26 |
27 | ```js
28 | import Highlight from 'react-highlight'
29 | ```
30 |
31 | #### Adding styles
32 |
33 | Choose the [theme](https://highlightjs.org/static/demo/) for syntax highlighting and add corresponding styles of highlight.js
34 |
35 | ```css
36 |
37 | ```
38 |
39 | The styles will most likely be in `node_modules/highlight.js/styles` folder.
40 |
41 | Props:
42 |
43 | * className: custom class name
44 | * innerHTML: enable to render markup with dangerouslySetInnerHTML
45 | * element: render code snippet inside specified element
46 |
47 | #### Syntax highlighting of single code snippet
48 |
49 | Code snippet that requires syntax highlighting should be passed as children to Highlight component in string format. Language name of code snippet should be specified as className.
50 |
51 | ```html
52 |
{children}
;
45 | }
46 | }
47 |
48 | Highlight.defaultProps = {
49 | innerHTML: false,
50 | className: null,
51 | element: null,
52 | };
53 |
54 | export default Highlight;
55 |
--------------------------------------------------------------------------------
/src/optimized.js:
--------------------------------------------------------------------------------
1 | import hljs from'highlight.js/lib/highlight';
2 | import React from'react';
3 |
4 | class Highlight extends React.Component {
5 | componentDidMount() {
6 | this.highlightCode();
7 | }
8 |
9 | componentDidUpdate() {
10 | this.highlightCode();
11 | }
12 |
13 | highlightCode() {
14 | const {className, languages} = this.props;
15 | const nodes = this.el.querySelectorAll('pre code');
16 |
17 | if ((languages.length === 0) && className) {
18 | languages.push(className);
19 | }
20 |
21 | languages.forEach(lang => {
22 | hljs.registerLanguage(lang, require('highlight.js/lib/languages/' + lang));
23 | });
24 |
25 | for (let i = 0; i < nodes.length; i++) {
26 | hljs.highlightBlock(nodes[i])
27 | }
28 | }
29 |
30 | setEl = (el) => {
31 | this.el = el;
32 | };
33 |
34 | render() {
35 | const {children, className, element: Element, innerHTML} = this.props;
36 | const props = { ref: this.setEl, className };
37 |
38 | if (innerHTML) {
39 | props.dangerouslySetInnerHTML = { __html: children };
40 | if (Element) {
41 | return {children}
;
50 | }
51 | }
52 |
53 | Highlight.defaultProps = {
54 | innerHTML: false,
55 | className: '',
56 | languages: [],
57 | };
58 |
59 | export default Highlight;
60 |
--------------------------------------------------------------------------------
/test/highlight.test.js:
--------------------------------------------------------------------------------
1 | import Highlight from '../src'
2 | import ReactDOM from 'react-dom'
3 | import TestUtils from 'react-dom/test-utils'
4 | import ReactDOMServer from 'react-dom/server'
5 | import React from 'react'
6 |
7 | describe('highlight', () => {
8 | test('should display text inside it', () => {
9 | const text = TestUtils.renderIntoDocument(Some text
')
20 | })
21 |
22 | test('should assign className prop', () => {
23 | const text = ReactDOMServer.renderToStaticMarkup(
24 | Some text
')
28 | })
29 |
30 | test('should render children in span', () => {
31 | const text = ReactDOMServer.renderToStaticMarkup(
32 |