Skip to content

Commit

Permalink
Merge pull request #2043 from nickmelnikov82/fix-markdown-mathjax-in-…
Browse files Browse the repository at this point in the history
…html

Fix markdown mathjax in html
  • Loading branch information
alexcjohnson authored May 12, 2022
2 parents e774908 + 93a392a commit cebad07
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
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
20 changes: 19 additions & 1 deletion components/dash-core-components/src/fragments/Markdown.react.js
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)

0 comments on commit cebad07

Please sign in to comment.