mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-21 05:02:39 +08:00
35 lines
723 B
JavaScript
35 lines
723 B
JavaScript
import hljs from 'highlight.js';
|
|
import PropTypes from 'prop-types';
|
|
import React from 'react';
|
|
import ReactDOM from 'react-dom';
|
|
|
|
class Highlight extends React.Component {
|
|
componentDidMount() {
|
|
this.highlightCode();
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
this.highlightCode();
|
|
}
|
|
|
|
highlightCode() {
|
|
const domNode = ReactDOM.findDOMNode(this); // eslint-disable-line
|
|
const nodes = domNode.querySelectorAll('pre code');
|
|
|
|
nodes.forEach(node => {
|
|
hljs.highlightBlock(node);
|
|
});
|
|
}
|
|
|
|
render() {
|
|
const { children } = this.props;
|
|
return <div dangerouslySetInnerHTML={{ __html: children }} />;
|
|
}
|
|
}
|
|
|
|
Highlight.propTypes = {
|
|
children: PropTypes.node.isRequired,
|
|
};
|
|
|
|
export default Highlight;
|