diff --git a/src/ui/public/markdown/markdown.js b/src/ui/public/markdown/markdown.js index 2e2b47e37c7bc..d34a387af4cdf 100644 --- a/src/ui/public/markdown/markdown.js +++ b/src/ui/public/markdown/markdown.js @@ -48,10 +48,8 @@ export class Markdown extends Component { this.markdownIt = markdownFactory(this.props.whiteListedRules, this.props.openLinksInNewTab); this.state = { - renderedMarkdown: this.transformMarkdown(this.props) + renderedMarkdown: this.transformMarkdown(this.props), }; - - } /** @@ -68,9 +66,16 @@ export class Markdown extends Component { } componentWillReceiveProps(props) { - if (props.markdown !== this.props.markdown) { + const hasOpenLinksInNewTabChanged = props.openLinksInNewTab !== this.props.openLinksInNewTab; + const hasMarkdownChanged = props.markdown !== this.props.markdown; + const hasWhiteListerRulesChanged = props.whiteListedRules !== this.props.whiteListedRules; + + if (hasOpenLinksInNewTabChanged || hasWhiteListerRulesChanged) { + this.markdownIt = markdownFactory(props.whiteListedRules, props.openLinksInNewTab); + } + if (hasMarkdownChanged || hasOpenLinksInNewTabChanged || hasWhiteListerRulesChanged) { this.setState({ - renderedMarkdown: this.transformMarkdown(props) + renderedMarkdown: this.transformMarkdown(props), }); } } @@ -84,10 +89,7 @@ export class Markdown extends Component { ...rest } = this.props; - const classes = classNames( - 'markdown-body', - className - ); + const classes = classNames('markdown-body', className); return (
{ />); expect(component).toMatchSnapshot(); // eslint-disable-line }); + + test('should update markdown when openLinksInNewTab prop change', () => { + const component = shallow(); + expect(component.render().find('a').prop('target')).not.toBe('_blank'); + component.setProps({ openLinksInNewTab: true }); + expect(component.render().find('a').prop('target')).toBe('_blank'); + }); + + test('should update markdown when whiteListedRules prop change', () => { + const markdown = '*emphasis* `backticks`'; + const component = shallow(); + expect(component.render().find('em')).toHaveLength(1); + expect(component.render().find('code')).toHaveLength(1); + component.setProps({ whiteListedRules: ['backticks'] }); + expect(component.render().find('code')).toHaveLength(1); + expect(component.render().find('em')).toHaveLength(0); + }); });