Skip to content

Commit

Permalink
Support MDX in the Highlight component
Browse files Browse the repository at this point in the history
  • Loading branch information
kylesuss committed Aug 4, 2020
1 parent 58d18e8 commit 200871b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/components/Highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ export class Highlight extends React.Component {
}

render() {
const { children, language, ...rest } = this.props;
const codeBlock = <div dangerouslySetInnerHTML={{ __html: children }} />;
const { children, language, withHTMLChildren, ...rest } = this.props;
const codeBlock = withHTMLChildren ? (
<div dangerouslySetInnerHTML={{ __html: children }} />
) : (
children
);

return (
<HighlightBlock {...rest}>
Expand All @@ -89,8 +93,10 @@ export class Highlight extends React.Component {
Highlight.propTypes = {
children: PropTypes.node.isRequired,
language: PropTypes.oneOf(languages),
withHTMLChildren: PropTypes.bool,
};

Highlight.defaultProps = {
language: null,
withHTMLChildren: true,
};
14 changes: 13 additions & 1 deletion src/components/Highlight.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default {
component: Highlight,
};

export const Basic = args => <Highlight {...args} />;
export const Basic = (args) => <Highlight {...args} />;
Basic.args = {};

export const Bash = () => (
Expand Down Expand Up @@ -150,3 +150,15 @@ const StyledHighlight = styled(Highlight)`
`;

export const CustomStyling = () => <StyledHighlight language="json">{jsonCode}</StyledHighlight>;

/* eslint-disable */
function WrapperComponent({ children }) {
return <div dangerouslySetInnerHTML={{ __html: children }} />;
}
/* eslint-enable */

export const WithComponentChildren = () => (
<Highlight withHTMLChildren={false}>
<WrapperComponent>{javascriptCodeWithWrappers}</WrapperComponent>
</Highlight>
);

0 comments on commit 200871b

Please sign in to comment.