This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for highlight code sections
- Loading branch information
1 parent
5423d96
commit 19bf7ea
Showing
11 changed files
with
93 additions
and
25 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'prismjs/components/prism-jsx' | ||
|
||
import React, { PureComponent } from 'react' | ||
import cx from 'classnames' | ||
import prism from 'prismjs' | ||
import styled from 'react-emotion' | ||
|
||
import * as colors from '../styles/colors' | ||
|
||
const PreStyled = styled('pre')` | ||
border: 1px solid ${colors.border}; | ||
` | ||
|
||
interface PreProps { | ||
className: string | ||
children: any | ||
} | ||
|
||
export class Pre extends PureComponent<PreProps> { | ||
private el: any | ||
|
||
public render(): JSX.Element { | ||
const { children } = this.props | ||
const childrenProps = children.props.props | ||
const childrenClassName = childrenProps && childrenProps.className | ||
|
||
const className = cx('react-prism', this.props.className, childrenClassName) | ||
|
||
return ( | ||
<PreStyled innerRef={ref => (this.el = ref)} className={className}> | ||
<code>{children.props.children}</code> | ||
</PreStyled> | ||
) | ||
} | ||
|
||
public componentDidMount(): void { | ||
this.highlightCode() | ||
} | ||
|
||
public componentDidUpdate(): void { | ||
this.highlightCode() | ||
} | ||
|
||
private highlightCode(): void { | ||
prism.highlightElement(this.el) | ||
} | ||
} |
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
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