Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix markdown mathjax in html #2043

Merged
merged 4 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

### Fixed

- [#2043](https://github.com/plotly/dash/pull/2043) Fix bug
[#2003](https://github.com/plotly/dash/issues/2003) in which
`dangerously_allow_html=True` + `mathjax=True` works in some cases, and in some cases not.

## [2.4.1] - 2022-05-11

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ export default class DashMarkdown extends Component {
)}
/>
),
dashMathjax: props => (
<Math tex={props.value} inline={props.inline} />
),
};

const regexMath = value => {
const newValue = value.replace(
/(\${1,2})((?:\\.|[^$])+)\1/g,
function (m, tag, src) {
const inline = tag.length === 1 || src.indexOf('\n') === -1;
return `<dashMathjax value='${src}' inline='${inline}'/>`;
}
);
return newValue;
};

return (
Expand Down Expand Up @@ -151,7 +165,11 @@ export default class DashMarkdown extends Component {
props.value
) : (
<JsxParser
jsx={props.value}
jsx={
mathjax
? regexMath(props.value)
: props.value
}
components={componentTransforms}
renderInWrapper={false}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,43 @@ def test_mkdw009_target_blank_links(dash_dcc):
dash_dcc.find_element("a").click()

until(lambda: len(dash_dcc.driver.window_handles) == 2, timeout=1)


def test_mkdw010_mathjax_with_html(dash_dcc):

app = Dash(__name__)

CONTENT = [
"""
<details>
<summary>Topic</summary>
Some details
</details>

$E = mc^2$
""",
"""
<p>Some paragraph</p>

$E = mc^2$
""",
"""
<p>Some paragraph</p>
$E = mc^2$
""",
"""
<p>Some paragraph</p> $E = mc^2$
""",
"""
<p>Some paragraph with $E = mc^2$ inline math</p>
""",
]

app.layout = html.Div(
[dcc.Markdown(c, dangerously_allow_html=True, mathjax=True) for c in CONTENT]
)

dash_dcc.start_server(app)

dash_dcc.wait_for_element(".MathJax")
assert len(dash_dcc.find_elements((".MathJax"))) == len(CONTENT)