-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build react-highlight for node module and browser (#2)
- react-highlight.js - react-highlight.browser.js
- Loading branch information
Showing
5 changed files
with
109 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,6 @@ node_modules | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# build files | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env node | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// create folder | ||
const dist = path.resolve(__dirname, '../dist'); | ||
try { | ||
fs.mkdirSync(dist); | ||
} catch (e) { | ||
if (e.code !== 'EEXIST') { | ||
throw e; | ||
} | ||
} | ||
|
||
// read source file | ||
let file = path.resolve(__dirname, '../src/HighLight.js'); | ||
const src = fs.readFileSync(file, 'utf8'); | ||
|
||
// 1. node module | ||
file = path.resolve(__dirname, dist, 'react-highlight.js'); | ||
let data = `const React = require('react'); | ||
const ReactDOM = require('react-dom'); | ||
const PropTypes = require('prop-types'); | ||
const hljs = require('highlight.js'); | ||
${src} | ||
module.exports = HighLight; | ||
`; | ||
fs.writeFileSync(file, data, 'utf8'); | ||
console.log(`${file}`); | ||
|
||
// 2. browser | ||
file = path.resolve(__dirname, dist, 'react-highlight.browser.js'); | ||
data = `(function () { | ||
${src} | ||
window.HighLight = window.HighLight || HighLight; | ||
})(); | ||
`; | ||
fs.writeFileSync(file, data, 'utf8'); | ||
console.log(`${file}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// React: 'react' | ||
// ReactDOM: 'react-dom' | ||
// PropTypes: 'prop-types' | ||
// hljs: 'highlight.js' | ||
|
||
class HighLight extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.componentDidMount = this.componentDidMount.bind(this); | ||
this.componentDidUpdate = this.componentDidUpdate.bind(this); | ||
this.updateCodeBlockDOM = this.updateCodeBlockDOM.bind(this); | ||
this.render = this.render.bind(this); | ||
} | ||
|
||
componentDidMount () { | ||
// this will only be called once after first time render | ||
this.updateCodeBlockDOM(); | ||
} | ||
|
||
componentDidUpdate () { | ||
// whenever component is updated | ||
this.updateCodeBlockDOM(); | ||
} | ||
|
||
render () { | ||
/* | ||
* <pre inherit_all_the_props_from_parent > | ||
* <code ref='code' className={'hljs ' + this.props.lang}> | ||
* to_be_rendered_by_highlight.js | ||
* </code> | ||
* </pre> | ||
* | ||
*/ | ||
const props = Object.assign({}, this.props); | ||
delete props.lang; | ||
delete props.value; | ||
|
||
return ( | ||
React.createElement('pre', props, | ||
React.createElement('code', { | ||
ref: 'code', | ||
className: 'hljs ' + this.props.lang | ||
}) | ||
) | ||
); | ||
} | ||
|
||
updateCodeBlockDOM () { | ||
// update real DOM element after component render | ||
const ele = ReactDOM.findDOMNode(this.refs.code); | ||
|
||
try { | ||
ele.innerHTML = hljs.highlight(this.props.lang, this.props.value, true).value; | ||
} catch (e) { | ||
console.warn(e); | ||
ele.innerHTML = this.props.value; // remove syntax highlight | ||
} | ||
} | ||
} | ||
|
||
HighLight.propTypes = { | ||
lang: PropTypes.string.isRequired, | ||
value: PropTypes.string.isRequired | ||
}; |