Skip to content

Commit

Permalink
CHORE Allow markdown component to open links in new tabs
Browse files Browse the repository at this point in the history
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 1dcf9b3
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 65 deletions.
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
"@appearhere/react-input-range": "^1.2.0",
"@appearhere/react-stickynode": "^1.0.1",
"classnames": "^2.2.5",
"commonmark": "^0.28.1",
"commonmark-react-renderer": "^4.3.4",
"dedent": "^0.7.0",
"file-loader": "^1.1.11",
"key-mirror": "^1.0.1",
Expand All @@ -38,6 +36,7 @@
"react-autosuggest": "^9.3.4",
"react-container-query": "^0.11.0",
"react-html5video": "^2.5.1",
"react-markdown": "^4.3.1",
"react-moment-proptypes": "^1.5.0",
"react-motion": "^0.5.2",
"react-on-visible": "^1.5.0",
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/components/Markdown/Markdown.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
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"}/>);
}
}
Loading

0 comments on commit 1dcf9b3

Please sign in to comment.