-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Highlight to be a functional component, fix not re-rendering…
… on content change
- Loading branch information
1 parent
cb6e53e
commit d993928
Showing
3 changed files
with
23 additions
and
75 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 |
---|---|---|
@@ -1,73 +1,23 @@ | ||
import React, { Component } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
import hljs from "highlight.js"; | ||
import "highlight.js/styles/monokai-sublime.css"; | ||
|
||
const registeredLanguages = {}; | ||
|
||
class Highlight extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { loaded: false }; | ||
this.codeNode = React.createRef(); | ||
} | ||
|
||
componentDidMount() { | ||
const { language } = this.props; | ||
|
||
if (language && !registeredLanguages[language]) { | ||
try { | ||
const newLanguage = require(`highlight.js/lib/languages/${language}`); | ||
hljs.registerLanguage(language, newLanguage); | ||
registeredLanguages[language] = true; | ||
|
||
this.setState({ loaded: true }, this.highlight); | ||
} catch (e) { | ||
console.error(e); | ||
throw Error(`Cannot register the language ${language}`); | ||
} | ||
} else { | ||
this.setState({ loaded: true }); | ||
} | ||
} | ||
|
||
componentDidUpdate() { | ||
this.highlight(); | ||
} | ||
|
||
highlight = () => { | ||
this.codeNode && | ||
this.codeNode.current && | ||
hljs.highlightBlock(this.codeNode.current); | ||
}; | ||
|
||
render() { | ||
const { language, children } = this.props; | ||
const { loaded } = this.state; | ||
|
||
if (!loaded) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<pre className="rounded"> | ||
<code ref={this.codeNode} className={language}> | ||
{children} | ||
</code> | ||
</pre> | ||
); | ||
} | ||
} | ||
|
||
Highlight.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
language: PropTypes.string, | ||
}; | ||
|
||
Highlight.defaultProps = { | ||
language: "json", | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
import hljs from 'highlight.js'; | ||
import 'highlight.js/styles/monokai-sublime.css'; | ||
|
||
const Highlight = (props) => { | ||
const { text, language = 'json' } = props; | ||
|
||
const codeNode = useRef(null); | ||
useEffect(() => { | ||
hljs.highlightElement(codeNode.current); | ||
}, [text]); | ||
|
||
return ( | ||
<pre className="rounded"> | ||
<code ref={codeNode} className={language}> | ||
{text} | ||
</code> | ||
</pre> | ||
); | ||
}; | ||
|
||
export default Highlight; | ||
export default Highlight; |
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 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