Skip to content
Open
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
2 changes: 2 additions & 0 deletions components/dash-core-components/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 components/dash-core-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 <dashmathjax>. Must run before rehype-raw.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
},
},
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() == []