diff --git a/components/dash-core-components/package-lock.json b/components/dash-core-components/package-lock.json index 804f40cb1c..98d742446e 100644 --- a/components/dash-core-components/package-lock.json +++ b/components/dash-core-components/package-lock.json @@ -26,6 +26,7 @@ "highlight.js": "^11.8.0", "js-search": "^2.0.1", "mathjax": "^3.2.2", + "micromark-extension-math": "^3.1.0", "node-polyfill-webpack-plugin": "^2.0.1", "prop-types": "^15.8.1", "ramda": "^0.32.0", @@ -79,6 +80,7 @@ "identity-obj-proxy": "^3.0.0", "jest": "^29.5.0", "jest-environment-jsdom": "^30.2.0", + "micromark-util-types": "^2.0.3", "npm-run-all": "^4.1.5", "prettier": "^2.8.8", "react": "^18", diff --git a/components/dash-core-components/package.json b/components/dash-core-components/package.json index df2520b5dd..a6f0123fb1 100644 --- a/components/dash-core-components/package.json +++ b/components/dash-core-components/package.json @@ -53,6 +53,7 @@ "highlight.js": "^11.8.0", "js-search": "^2.0.1", "mathjax": "^3.2.2", + "micromark-extension-math": "^3.1.0", "node-polyfill-webpack-plugin": "^2.0.1", "prop-types": "^15.8.1", "ramda": "^0.32.0", @@ -106,6 +107,7 @@ "identity-obj-proxy": "^3.0.0", "jest": "^29.5.0", "jest-environment-jsdom": "^30.2.0", + "micromark-util-types": "^2.0.3", "npm-run-all": "^4.1.5", "prettier": "^2.8.8", "react": "^18", diff --git a/components/dash-core-components/src/utils/markdown/plugins/index.ts b/components/dash-core-components/src/utils/markdown/plugins/index.ts index cd1ab94105..fd41b8e097 100644 --- a/components/dash-core-components/src/utils/markdown/plugins/index.ts +++ b/components/dash-core-components/src/utils/markdown/plugins/index.ts @@ -1,6 +1,7 @@ import type {PluggableList} from 'unified'; import remarkGfm from 'remark-gfm'; import remarkMath from 'remark-math'; +import remarkMathEscapedDollar from './remarkMathEscapedDollar'; import rehypeRaw from 'rehype-raw'; import rehypeBlockCode from './rehypeBlockCode'; @@ -13,9 +14,11 @@ import rehypeStripTags from './rehypeStripTags'; // task lists, autolinks, etc. export const remarkGfmPlugins: PluggableList = [remarkGfm]; -// remark plugin for the `mathjax` prop: remark-math parses `$...$`/`$$...$$` in -// Markdown at the token level, so it leaves `\$` and `$` as literal dollars. -export const remarkMathPlugins: PluggableList = [remarkMath]; +// Parse Markdown math and keep escaped dollars inside math expressions. +export const remarkMathPlugins: PluggableList = [ + remarkMath, + remarkMathEscapedDollar, +]; // rehype plugin for the `mathjax` prop: routes both Markdown and raw-HTML math // into . Must run before rehype-raw. diff --git a/components/dash-core-components/src/utils/markdown/plugins/remarkMathEscapedDollar.ts b/components/dash-core-components/src/utils/markdown/plugins/remarkMathEscapedDollar.ts new file mode 100644 index 0000000000..ddac29ec91 --- /dev/null +++ b/components/dash-core-components/src/utils/markdown/plugins/remarkMathEscapedDollar.ts @@ -0,0 +1,72 @@ +import type {Processor} from 'unified'; +import type {Code, Construct, Extension, State} from 'micromark-util-types'; +import {math} from 'micromark-extension-math'; + +const DOLLAR = '$'.codePointAt(0) as number; +const BACKSLASH = '\\'.codePointAt(0) as number; +const ORDINARY_CHAR = 'A'.codePointAt(0) as number; + +const original = math().text?.[DOLLAR] as Construct; + +// Let remark-math parse normally, except escaped dollars inside math. +export default function remarkMathEscapedDollar(this: Processor) { + // eslint-disable-next-line no-invalid-this + const data = this.data() as {micromarkExtensions?: Extension[]}; + + if (!data.micromarkExtensions) { + data.micromarkExtensions = []; + } + + data.micromarkExtensions.push({ + text: { + 36: { + ...original, + tokenize(effects, ok, nok) { + let slashes = 0; + let changed = false; + let mapped = false; + let finished = false; + + const proxy = { + ...effects, + consume(code: Code) { + effects.consume(mapped ? DOLLAR : code); + }, + }; + const success = (code: Code) => { + finished = true; + return (changed ? ok : nok)(code); + }; + const failure = (code: Code) => { + finished = true; + return nok(code); + }; + + let state = original.tokenize.call( + this, + proxy, + success, + failure + ); + + function next(code: Code): State | undefined { + mapped = code === DOLLAR && slashes % 2 === 1; + if (mapped) { + changed = true; + } + + // Treat escaped '$' as ordinary text while + // consuming the original character. + state = state(mapped ? ORDINARY_CHAR : code); + slashes = code === BACKSLASH ? slashes + 1 : 0; + mapped = false; + + return finished ? state : next; + } + + return next; + }, + }, + }, + }); +} diff --git a/components/dash-core-components/tests/integration/markdown/test_markdown.py b/components/dash-core-components/tests/integration/markdown/test_markdown.py index 62d94800e6..bb4f4db97b 100644 --- a/components/dash-core-components/tests/integration/markdown/test_markdown.py +++ b/components/dash-core-components/tests/integration/markdown/test_markdown.py @@ -760,3 +760,64 @@ def test_mkdw024_renders_without_es2022_builtins(dash_dcc): dash_dcc.wait_for_text_to_equal("#md h1", "Title") assert dash_dcc.get_logs() == [] + + +def test_mkdw025_escaped_dollar_stays_inline(dash_dcc): + app = Dash(__name__) + app.layout = dcc.Markdown( + r"a $x=\$3$ b", + mathjax=True, + id="md", + ) + + dash_dcc.start_server(app) + dash_dcc.wait_for_element("#md .MathJax svg") + + result = dash_dcc.driver.execute_script( + """ + const root = document.getElementById("md"); + const paragraph = root.querySelector("p"); + const math = paragraph.querySelector("mjx-container.MathJax"); + + return { + paragraphCount: root.querySelectorAll("p").length, + inline: Boolean(math) && !math.hasAttribute("display"), + svgCount: math ? math.querySelectorAll("svg").length : 0, + text: paragraph.textContent + }; + """ + ) + + assert result == { + "paragraphCount": 1, + "inline": True, + "svgCount": 1, + "text": "a x=$3 b", + } + + assert dash_dcc.get_logs() == [] + + +def test_mkdw026_escaped_dollars_outside_math(dash_dcc): + app = Dash(__name__) + app.layout = html.Div( + [ + dcc.Markdown(r"`$x=\$3$`", mathjax=True, id="inline-code"), + dcc.Markdown("```\n$x=\\$3$\n```", mathjax=True, id="fenced-code"), + dcc.Markdown( + r"[link](https://example.org/$x=\$3$)", + mathjax=True, + id="link", + ), + ] + ) + + dash_dcc.start_server(app) + + assert dash_dcc.wait_for_element("#inline-code code").text == r"$x=\$3$" + assert dash_dcc.wait_for_element("#fenced-code pre code").text == r"$x=\$3$" + assert ( + dash_dcc.wait_for_element("#link a").get_attribute("href") + == "https://example.org/$x=$3$" + ) + assert dash_dcc.get_logs() == []