Skip to content

Commit cebad07

Browse files
authored
Merge pull request #2043 from nickmelnikov82/fix-markdown-mathjax-in-html
Fix markdown mathjax in html
2 parents e774908 + 93a392a commit cebad07

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

Diff for: CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
All notable changes to `dash` will be documented in this file.
33
This project adheres to [Semantic Versioning](https://semver.org/).
44

5+
## [Unreleased]
6+
7+
### Fixed
8+
9+
- [#2043](https://github.com/plotly/dash/pull/2043) Fix bug
10+
[#2003](https://github.com/plotly/dash/issues/2003) in which
11+
`dangerously_allow_html=True` + `mathjax=True` works in some cases, and in some cases not.
12+
513
## [2.4.1] - 2022-05-11
614

715
### Fixed

Diff for: components/dash-core-components/src/fragments/Markdown.react.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ export default class DashMarkdown extends Component {
108108
)}
109109
/>
110110
),
111+
dashMathjax: props => (
112+
<Math tex={props.value} inline={props.inline} />
113+
),
114+
};
115+
116+
const regexMath = value => {
117+
const newValue = value.replace(
118+
/(\${1,2})((?:\\.|[^$])+)\1/g,
119+
function (m, tag, src) {
120+
const inline = tag.length === 1 || src.indexOf('\n') === -1;
121+
return `<dashMathjax value='${src}' inline='${inline}'/>`;
122+
}
123+
);
124+
return newValue;
111125
};
112126

113127
return (
@@ -151,7 +165,11 @@ export default class DashMarkdown extends Component {
151165
props.value
152166
) : (
153167
<JsxParser
154-
jsx={props.value}
168+
jsx={
169+
mathjax
170+
? regexMath(props.value)
171+
: props.value
172+
}
155173
components={componentTransforms}
156174
renderInWrapper={false}
157175
/>

Diff for: components/dash-core-components/tests/integration/markdown/test_markdown.py

+40
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,43 @@ def test_mkdw009_target_blank_links(dash_dcc):
388388
dash_dcc.find_element("a").click()
389389

390390
until(lambda: len(dash_dcc.driver.window_handles) == 2, timeout=1)
391+
392+
393+
def test_mkdw010_mathjax_with_html(dash_dcc):
394+
395+
app = Dash(__name__)
396+
397+
CONTENT = [
398+
"""
399+
<details>
400+
<summary>Topic</summary>
401+
Some details
402+
</details>
403+
404+
$E = mc^2$
405+
""",
406+
"""
407+
<p>Some paragraph</p>
408+
409+
$E = mc^2$
410+
""",
411+
"""
412+
<p>Some paragraph</p>
413+
$E = mc^2$
414+
""",
415+
"""
416+
<p>Some paragraph</p> $E = mc^2$
417+
""",
418+
"""
419+
<p>Some paragraph with $E = mc^2$ inline math</p>
420+
""",
421+
]
422+
423+
app.layout = html.Div(
424+
[dcc.Markdown(c, dangerously_allow_html=True, mathjax=True) for c in CONTENT]
425+
)
426+
427+
dash_dcc.start_server(app)
428+
429+
dash_dcc.wait_for_element(".MathJax")
430+
assert len(dash_dcc.find_elements((".MathJax"))) == len(CONTENT)

0 commit comments

Comments
 (0)