-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHORE Allow markdown component to open links in new tabs
Since the current markdown parsing package has a known issue with rendering inline html (rexxars/commonmark-react-renderer#9) which has been an issue for 3 years+, we need a new markdown package. The react-markdown package has an option to render links with target="_blank" so this commit allows the component to pass that option through
- Loading branch information
Dom Batten
committed
Jan 27, 2020
1 parent
4b736a0
commit b2f0c8b
Showing
4 changed files
with
204 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, { Component } from 'react'; | ||
import CommonMark from 'commonmark'; | ||
import ReactRenderer from 'commonmark-react-renderer'; | ||
import cx from 'classnames'; | ||
import ReactMarkdown from 'react-markdown'; | ||
|
||
import css from './Markdown.css'; | ||
|
||
const parser = new CommonMark.Parser(); | ||
const renderer = new ReactRenderer(); | ||
|
||
export default class Markdown extends Component { | ||
static propTypes = { | ||
children: PropTypes.string.isRequired, | ||
className: PropTypes.string, | ||
overrideClassname: PropTypes.bool, | ||
targetBlank: PropTypes.bool, | ||
}; | ||
|
||
static defaultProps = { | ||
overrideClassname: false, | ||
targetBlank: false, | ||
}; | ||
|
||
render() { | ||
const { children, className, overrideClassname, ...rest } = this.props; | ||
|
||
const ast = parser.parse(children); | ||
const { children, className, overrideClassname, targetBlank, ...rest } = this.props; | ||
|
||
const props = { | ||
className: overrideClassname ? className : cx(css.root, className), | ||
...rest, | ||
}; | ||
|
||
return React.createElement('div', props, renderer.render(ast)); | ||
return React.createElement( | ||
'div', | ||
props, | ||
<ReactMarkdown source={children} linkTarget={targetBlank ? "_blank" : "_self"} /> | ||
); | ||
} | ||
} |
Oops, something went wrong.