Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Issue 250 - Support for rendering links inside dcc.Markdown as dcc.Link #711

Merged
merged 24 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- [#711](https://github.com/plotly/dash-core-components/pull/711) Added support for `dcc.Link` (dccLink) and nested `dcc.Markdown` (dccMarkdown) react components inside of `dcc.Markdown`
- [#706](https://github.com/plotly/dash-core-components/pull/706)
- Added new `responsive` property that overrides the underlying Plotly.js graph responsiveness from Dash-land
- Added responsiveness on graph parent element resize (previously only worked on window.resize)
Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
"prettier": "^1.14.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-jsx-parser": "^1.21.0",
"react-resize-detector": "^4.2.1",
"style-loader": "^0.23.1",
"styled-jsx": "^3.1.1",
"terser-webpack-plugin": "^2.3.0",
Expand Down
29 changes: 28 additions & 1 deletion src/fragments/Markdown.react.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React, {Component} from 'react';
import {type} from 'ramda';
import {mergeDeepRight, pick, type} from 'ramda';
import JsxParser from 'react-jsx-parser';
import Markdown from 'react-markdown';

import MarkdownHighlighter from '../utils/MarkdownHighlighter';
import {propTypes, defaultProps} from '../components/Markdown.react';

import DccLink from './../components/Link.react';

export default class DashMarkdown extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -91,6 +94,18 @@ export default class DashMarkdown extends Component {
const displayText =
dedent && textProp ? this.dedent(textProp) : textProp;

const componentTransforms = {
dccLink: props => <DccLink {...props} />,
dccMarkdown: props => (
<Markdown
{...mergeDeepRight(
pick(['dangerously_allow_html', 'dedent'], this.props),
pick(['children'], props)
)}
/>
),
};
Copy link
Contributor Author

@Marc-Andre-Rivet Marc-Andre-Rivet Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we define the list of components we want to support are their name in markdown + props customization as needed.


return (
<div
id={id}
Expand All @@ -116,6 +131,18 @@ export default class DashMarkdown extends Component {
<Markdown
source={displayText}
escapeHtml={!dangerously_allow_html}
renderers={{
html: props =>
props.escapeHtml ? (
props.value
) : (
<JsxParser
jsx={props.value}
components={componentTransforms}
renderInWrapper={false}
/>
),
}}
/>
</div>
);
Expand Down
Binary file added tests/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions tests/integration/markdown/test_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os
import pytest
from selenium.common.exceptions import WebDriverException

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html


def test_mkdw001_img(dash_dcc):
app = dash.Dash(__name__, eager_loading=True)

app.layout = html.Div(
[
html.Div('Markdown img'),
dcc.Markdown(
['<img src="assets/image.png" />'], dangerously_allow_html=True
),
html.Div('Markdown img - requires dangerously_allow_html'),
dcc.Markdown(['<img src="assets/image.png" />']),
]
)

dash_dcc.start_server(app)
dash_dcc.percy_snapshot("mkdw001 - image display")


def test_mkdw002_dcclink(dash_dcc):
app = dash.Dash(__name__, eager_loading=True)

app.layout = html.Div(
[
html.Div(['Markdown link']),
dcc.Markdown(['[Title](title_crumb)']),
html.Div(['Markdown dccLink']),
dcc.Markdown(
['<dccLink href="title_crumb" children="Title" />'],
dangerously_allow_html=True,
),
html.Div(['Markdown dccLink - explicit children']),
dcc.Markdown(
[
'''
<dccLink href="title_crumb">
Title
</dccLink>
'''
],
dangerously_allow_html=True,
),
html.Div('Markdown dccLink = inlined'),
dcc.Markdown(
[
'This is an inlined <dccLink href="title_crumb" children="Title" /> with text on both sides'
],
dangerously_allow_html=True,
),
html.Div('Markdown dccLink - nested image'),
dcc.Markdown(
[
'''
<dccLink href="title_crumb">
<img src="assets/image.png" />
</dccLink>
'''
],
dangerously_allow_html=True,
),
html.Div('Markdown dccLink - nested markdown'),
dcc.Markdown(
[
'''
<dccLink href="title_crumb">
<dccMarkdown children="## Title" />
</dccLink>
'''
],
dangerously_allow_html=True,
),
html.Div('Markdown dccLink - nested markdown image'),
dcc.Markdown(
[
'''
<dccLink href="title_crumb">
<dccMarkdown children="![Image](assets/image.png)" />
</dccLink>
'''
],
dangerously_allow_html=True,
),
html.Div('Markdown dccLink - requires dangerously_allow_html'),
dcc.Markdown(['<dccLink href="title_crumb" children="Title" />']),
]
)

dash_dcc.start_server(app)
dash_dcc.percy_snapshot("mkdw002 - markdowns display")
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = (env, argv) => {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
exclude: /node_modules\/(?!react-jsx-parser\/)/,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly not transpiled to ES5 so needs to be re-transpiled.

use: {
loader: 'babel-loader',
},
Expand Down