Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: akiran/react-highlight
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 715f1b5613c0b80117fbe32460c61751d02b2656
Choose a base ref
...
head repository: akiran/react-highlight
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d22569ade76ed5230c8b8308568f62e5938dcb42
Choose a head ref

Commits on Mar 6, 2015

  1. Update README.md

    Add the location of the styles folder
    NothingEverHappens committed Mar 6, 2015
    Copy the full SHA
    9eb5718 View commit details

Commits on Jul 12, 2015

  1. Verified

    This commit was signed with the committer’s verified signature.
    targos Michaël Zasso
    Copy the full SHA
    bce537e View commit details
  2. updated version

    kiran committed Jul 12, 2015
    Copy the full SHA
    08aadbf View commit details
  3. Added karma config for tests

    kiran committed Jul 12, 2015
    Copy the full SHA
    5609000 View commit details
  4. Added travis ci config

    kiran committed Jul 12, 2015
    Copy the full SHA
    5020fec View commit details
  5. Update karma config

    kiran committed Jul 12, 2015
    Copy the full SHA
    addded9 View commit details
  6. Added basic tests

    kiran committed Jul 12, 2015
    Copy the full SHA
    6267a2f View commit details
  7. dummy commit to see tests run on travis-ci

    kiran committed Jul 12, 2015
    Copy the full SHA
    85b8c50 View commit details
  8. removed few dependencies

    kiran committed Jul 12, 2015
    Copy the full SHA
    2d453f7 View commit details
  9. Added build status link

    kiran committed Jul 12, 2015
    Copy the full SHA
    5b9a2ed View commit details
  10. update

    kiran committed Jul 12, 2015
    Copy the full SHA
    a5a2d2f View commit details

Commits on Oct 10, 2015

  1. Fixed #7

    akiran committed Oct 10, 2015
    Copy the full SHA
    c6dc901 View commit details

Commits on Nov 25, 2015

  1. update to react 0.14

    iamblue committed Nov 25, 2015
    Copy the full SHA
    6bfa5df View commit details
  2. change prepublish to prebuild

    iamblue committed Nov 25, 2015
    Copy the full SHA
    a942018 View commit details
  3. Copy the full SHA
    d7e54fb View commit details
  4. fixed all test error

    iamblue committed Nov 25, 2015
    Copy the full SHA
    27bd5ad View commit details

Commits on Nov 27, 2015

  1. Copy the full SHA
    b492b28 View commit details

Commits on Nov 28, 2015

  1. Merge pull request #11 from Mediatek-Cloud/master

    Upgrade to react 0.14.0 !!! And fixed all test error !!!!
    akiran committed Nov 28, 2015
    Copy the full SHA
    f94ad36 View commit details
  2. Update package.json

    akiran committed Nov 28, 2015
    Copy the full SHA
    d8e78a6 View commit details
  3. updated version

    kiran committed Nov 28, 2015
    Copy the full SHA
    76c9951 View commit details
  4. Added some docs

    kiran committed Nov 28, 2015
    Copy the full SHA
    daaaf5e View commit details
  5. Fixed dependencies

    kiran committed Nov 28, 2015
    Copy the full SHA
    70ee50f View commit details

Commits on Mar 2, 2016

  1. Merge pull request #4 from NothingEverHappens/patch-2

    Update README.md
    akiran committed Mar 2, 2016
    Copy the full SHA
    9e9ec49 View commit details
  2. Fixed #14

    kiran committed Mar 2, 2016
    Copy the full SHA
    5b95db6 View commit details
  3. Version update

    kiran committed Mar 2, 2016
    Copy the full SHA
    49c6404 View commit details

Commits on Apr 17, 2016

  1. Copy the full SHA
    1f9071c View commit details
  2. added react@15.0 in dependencies

    kiran committed Apr 17, 2016
    Copy the full SHA
    f9563ce View commit details
  3. updated docs

    kiran committed Apr 17, 2016
    Copy the full SHA
    29ce35e View commit details
  4. version update

    kiran committed Apr 17, 2016
    Copy the full SHA
    6600bfd View commit details

Commits on Sep 3, 2016

  1. Updating highlight.js dependency from 8.6.0 to 9.6.0 and updating ver…

    …sion for package.
    
    This is in response to this issue. #19 (comment)
    jgunnison authored Sep 3, 2016
    Copy the full SHA
    20a2b68 View commit details

Commits on Sep 4, 2016

  1. Merge pull request #21 from jgunnison/master

    Updating highlight.js dependency from 8.6.0 to 9.6.0 and updating version for package.
    akiran authored Sep 4, 2016
    Copy the full SHA
    cf77857 View commit details
  2. cleanup package.json

    akiran committed Sep 4, 2016
    Copy the full SHA
    d11510d View commit details

Commits on Apr 19, 2017

  1. Copy the full SHA
    84489f6 View commit details
  2. Copy the full SHA
    ffb8f80 View commit details
  3. Copy the full SHA
    d9d1887 View commit details
  4. Refactor to ES6 classes

    MichaelDeBoey committed Apr 19, 2017
    Copy the full SHA
    d20cb12 View commit details
  5. Copy the full SHA
    60319e6 View commit details

Commits on May 4, 2017

  1. Merge pull request #31 from MichaelDeBoey/react-15.5

    Migrate to React 15.5
    akiran authored May 4, 2017
    Copy the full SHA
    f90e0eb View commit details
  2. Copy the full SHA
    dda2963 View commit details

Commits on Oct 7, 2017

  1. upgrade babel

    briancappello committed Oct 7, 2017
    Copy the full SHA
    798da1d View commit details
  2. support react v16

    briancappello committed Oct 7, 2017
    Copy the full SHA
    388c8ec View commit details
  3. fix tests

    briancappello committed Oct 7, 2017
    Copy the full SHA
    12d67cf View commit details

Commits on Oct 22, 2017

  1. Copy the full SHA
    d128058 View commit details

Commits on Nov 11, 2017

  1. Update README.md

    akiran authored Nov 11, 2017
    Copy the full SHA
    9e7d444 View commit details

Commits on Jan 4, 2018

  1. remove codesponsor

    itsjw committed Jan 4, 2018
    Copy the full SHA
    ccd8b36 View commit details
  2. Merge pull request #46 from knewzen/master

    remove codesponsor
    akiran authored Jan 4, 2018
    Copy the full SHA
    433f71c View commit details

Commits on Mar 13, 2018

  1. Copy the full SHA
    9c2712f View commit details
  2. Removed docs

    mbj36 committed Mar 13, 2018
    Copy the full SHA
    bcdec94 View commit details
  3. Added package-lock to gitignore

    mbj36 committed Mar 13, 2018
    Copy the full SHA
    fe96677 View commit details
  4. Merge pull request #51 from akiran/rewrite

    Upgrading to React16
    akiran authored Mar 13, 2018
    Copy the full SHA
    660fff2 View commit details
Showing with 271 additions and 484 deletions.
  1. +6 −0 .babelrc
  2. +24 −0 .eslintrc
  3. +2 −0 .gitignore
  4. +1 −0 .npmignore
  5. +3 −0 .travis.yml
  6. +32 −4 README.md
  7. +0 −152 docs/_default.scss
  8. 0 docs/docs.jsx
  9. +0 −1 docs/docs.scss
  10. +0 −9 docs/index.html
  11. +0 −7 docs/index.jsx
  12. +0 −12 docs/routes.jsx
  13. +0 −5 docs/snippets/html.md
  14. +0 −3 docs/snippets/html.txt
  15. +0 −36 gulpfile.js
  16. +1 −0 index.js
  17. +0 −35 index.jsx
  18. +0 −116 languages.js
  19. +0 −43 optimized.jsx
  20. +34 −25 package.json
  21. +54 −0 src/index.js
  22. +59 −0 src/optimized.js
  23. +55 −0 test/highlight.test.js
  24. +0 −36 webpack.config.js
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"env",
"react"
]
}
24 changes: 24 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"rules": {
"no-extra-parens": 0,
"react/jsx-uses-vars": 1,
"global-strict": 1,
"quotes": [2, "single"],
"no-underscore-dangle": 0,
"space-infix-ops": 0,
"no-alert": 0
},
"ecmaFeatures": {
"jsx": true,
},
"env": {
"node": true,
"browser": true,
"es6": true,
"jasmine": true
},
"parser": "babel-eslint",
"plugins": [
"react"
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -28,3 +28,5 @@ node_modules
.lock-wscript
.sass-cache
build
lib
package-lock.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "stable"
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
# react-highlight

React component for syntax highlighting using highlight.js

![Build Status](https://travis-ci.org/akiran/react-highlight.svg?branch=master)

### Latest version

`0.11.1`

## [Documentation](https://react-highlight.neostack.com/)

### CodeSandbox Example

[![Edit new](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/mj6wlmor9p)

### Installation

```bash
npm install react-highlight --save
```
```

### Usage

#### Importing component

```js
var Highlight = require('react-highlight');
import Highlight from 'react-highlight'
```

#### Adding styles

Choose the [theme](https://highlightjs.org/static/demo/) for syntax highlighting and add corresponding styles of highlight.js

```css
<link rel="stylesheet" href="/path/to/styles/theme-name.css">
```

The styles will most likely be in `node_modules/highlight.js/styles` folder.

Props:

* className: custom class name
* innerHTML: enable to render markup with dangerouslySetInnerHTML
* element: render code snippet inside specified element

#### Syntax highlighting of single code snippet

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.

```html
@@ -28,13 +55,14 @@ Code snippet that requires syntax highlighting should be passed as children to H
```

#### Syntax highlighting of mutiple code snippets

Set `innerHTML=true` to highlight multiple code snippets at a time.
This is especially usefull if html with multiple code snippets is generated from preprocesser tools like markdown.

**Warning:** If innerHTML is set to true, make sure the html generated with code snippets is from trusted source.

```html
<Highlight innerHTML={true}>
{"html with multiple code snippets"}
{"html with multiple code snippets"}
</Highlight>
```
```
152 changes: 0 additions & 152 deletions docs/_default.scss

This file was deleted.

Empty file removed docs/docs.jsx
Empty file.
1 change: 0 additions & 1 deletion docs/docs.scss

This file was deleted.

9 changes: 0 additions & 9 deletions docs/index.html

This file was deleted.

7 changes: 0 additions & 7 deletions docs/index.jsx

This file was deleted.

12 changes: 0 additions & 12 deletions docs/routes.jsx

This file was deleted.

5 changes: 0 additions & 5 deletions docs/snippets/html.md

This file was deleted.

3 changes: 0 additions & 3 deletions docs/snippets/html.txt

This file was deleted.

36 changes: 0 additions & 36 deletions gulpfile.js

This file was deleted.

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib');
35 changes: 0 additions & 35 deletions index.jsx

This file was deleted.

116 changes: 0 additions & 116 deletions languages.js

This file was deleted.

43 changes: 0 additions & 43 deletions optimized.jsx

This file was deleted.

59 changes: 34 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "react-highlight",
"version": "0.4.1",
"version": "0.14.0",
"description": "React component for syntax highlighting",
"main": "index.jsx",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/gulp server"
"prepublish": "babel ./src --out-dir ./lib --plugins=transform-class-properties,transform-react-jsx --presets=env",
"test": "BABEL_ENV=test jest"
},
"repository": {
"type": "git",
@@ -23,29 +24,37 @@
"url": "https://github.com/akiran/react-highlight/issues"
},
"homepage": "https://github.com/akiran/react-highlight",
"dependencies": {
"highlight.js": "^10.5.0"
},
"devDependencies": {
"autoprefixer-core": "^4.0.2",
"css-loader": "^0.9.0",
"gulp": "^3.8.10",
"gulp-ruby-sass": "^0.7.1",
"highlight.js": "^8.4.x",
"html-loader": "^0.2.3",
"jsx-loader": "^0.12.2",
"markdown-loader": "^0.1.2",
"multiline": "^1.0.1",
"autoprefixer": "^6.7.7",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^6.1.2",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"html-loader": "^0.4.5",
"jest": "^22.4.2",
"jsx-loader": "^0.13.2",
"markdown-loader": "^2.0.0",
"multiline": "^1.0.2",
"node-libs-browser": "^2.0.0",
"raw-loader": "^0.5.1",
"react": "*",
"react-hot-loader": "^0.5.0",
"sass-loader": "^0.3.1",
"style-loader": "^0.8.2",
"webpack": "^1.4.13",
"webpack-dev-server": "^1.6.6"
},
"peerDependencies": {
"highlight.js": "^8.4.x",
"react": "*"
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-test-renderer": "^16.2.0",
"regenerator-runtime": "^0.11.1",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
},
"browserify": {
"transform": "reactify"
}
"peerDependencies": {}
}
54 changes: 54 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import hljs from 'highlight.js';
import React from 'react';

class Highlight extends React.Component {
constructor(props) {
super(props)
this.setEl = this.setEl.bind(this)
}
componentDidMount() {
this.highlightCode();
}

componentDidUpdate() {
this.highlightCode();
}

highlightCode() {
const nodes = this.el.querySelectorAll('pre code');

for (let i = 0; i < nodes.length; i++) {
hljs.highlightBlock(nodes[i])
}
}

setEl(el) {
this.el = el;
};

render() {
const {children, className, element: Element, innerHTML} = this.props;
const props = { ref: this.setEl, className };

if (innerHTML) {
props.dangerouslySetInnerHTML = { __html: children };
if (Element) {
return <Element {...props} />;
}
return <div {...props} />;
}

if (Element) {
return <Element {...props}>{children}</Element>;
}
return <pre ref={this.setEl}><code className={className}>{children}</code></pre>;
}
}

Highlight.defaultProps = {
innerHTML: false,
className: null,
element: null,
};

export default Highlight;
59 changes: 59 additions & 0 deletions src/optimized.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import hljs from'highlight.js/lib/highlight';
import React from'react';

class Highlight extends React.Component {
componentDidMount() {
this.highlightCode();
}

componentDidUpdate() {
this.highlightCode();
}

highlightCode() {
const {className, languages} = this.props;
const nodes = this.el.querySelectorAll('pre code');

if ((languages.length === 0) && className) {
languages.push(className);
}

languages.forEach(lang => {
hljs.registerLanguage(lang, require('highlight.js/lib/languages/' + lang));
});

for (let i = 0; i < nodes.length; i++) {
hljs.highlightBlock(nodes[i])
}
}

setEl = (el) => {
this.el = el;
};

render() {
const {children, className, element: Element, innerHTML} = this.props;
const props = { ref: this.setEl, className };

if (innerHTML) {
props.dangerouslySetInnerHTML = { __html: children };
if (Element) {
return <Element {...props} />;
}
return <div {...props} />;
}

if (Element) {
return <Element {...props}>{children}</Element>;
}
return <pre ref={this.setEl}><code className={className}>{children}</code></pre>;
}
}

Highlight.defaultProps = {
innerHTML: false,
className: '',
languages: [],
};

export default Highlight;
55 changes: 55 additions & 0 deletions test/highlight.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Highlight from '../src'
import ReactDOM from 'react-dom'
import TestUtils from 'react-dom/test-utils'
import ReactDOMServer from 'react-dom/server'
import React from 'react'

describe('highlight', () => {
test('should display text inside it', () => {
const text = TestUtils.renderIntoDocument(<Highlight>Some text</Highlight>)

expect(ReactDOM.findDOMNode(text).textContent).toBe('Some text')
})

test('should have pre and code tags in markup', () => {
const text = ReactDOMServer.renderToStaticMarkup(
<Highlight>Some text</Highlight>
)

expect(text).toBe('<pre><code>Some text</code></pre>')
})

test('should assign className prop', () => {
const text = ReactDOMServer.renderToStaticMarkup(
<Highlight className="html">Some text</Highlight>
)

expect(text).toBe('<pre><code class="html">Some text</code></pre>')
})

test('should render children in span', () => {
const text = ReactDOMServer.renderToStaticMarkup(
<Highlight element="span">Some text</Highlight>
)

expect(text).toBe('<span>Some text</span>')
})

test('should render innerHTML in span', () => {
const text = ReactDOMServer.renderToStaticMarkup(
<Highlight innerHTML={true} element="span">
Some text
</Highlight>
)

expect(text).toBe('<span>Some text</span>')
})

test('should accept innerHTML prop', () => {
const text = TestUtils.renderIntoDocument(
<Highlight innerHTML={true}>{'<div>Sometext</div>'}</Highlight>
)

expect(ReactDOM.findDOMNode(text).textContent).toBe('Sometext')
})
})
36 changes: 0 additions & 36 deletions webpack.config.js

This file was deleted.