Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#4184](https://github.com/plotly/dash/pull/4184) Fix ASGI streaming callbacks being cancelled after 10 seconds under load, when a browser's previous downlink closed late and marked its new one closed.
- [#4185](https://github.com/plotly/dash/pull/4185) Fix `RedisSharedStorage` streaming callbacks failing under load with redis-py 8 (`MaxConnectionsError: Too many connections`): calls now wait for a free connection.
- [#4186](https://github.com/plotly/dash/pull/4186) Fix `LocalSharedStorage` electing a second, empty owner under load when the owner was too busy to accept connections, which reset every open stream.
- [#4194](https://github.com/plotly/dash/pull/4194) Fix `dcc.Markdown` rendering numeric character references above U+FFFF, such as `😀` emoji, as blank (regression in 4.5.0). Upgrades to react-markdown 10.

## [4.5.1] - 2026-11-19

Expand Down
2,719 changes: 1,458 additions & 1,261 deletions components/dash-core-components/package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions components/dash-core-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@
"react-dropzone": "^4.1.2",
"react-fast-compare": "^3.2.2",
"react-input-autosize": "^3.0.0",
"react-markdown": "^8.0.7",
"react-markdown": "^10.1.0",
"react-window": "^1.8.11",
"rehype-raw": "^6.1.1",
"remark-math": "^5.1.1",
"remark-gfm": "^3.0.1",
"rehype-raw": "^7.0.0",
"remark-gfm": "^4.0.1",
"remark-math": "^6.0.0",
"uniqid": "^5.4.0",
"unist-util-visit": "^4.1.2"
"unist-util-visit": "^5.2.0"
},
"devDependencies": {
"@babel/cli": "^7.28.6",
Expand Down
18 changes: 16 additions & 2 deletions components/dash-core-components/src/fragments/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import '../utils/markdown/polyfills';
import React, {useCallback, useMemo} from 'react';
import ReactMarkdown from 'react-markdown';
import ReactMarkdown, {
defaultUrlTransform,
type UrlTransform,
} from 'react-markdown';
import type {PluggableList} from 'unified';

import LoadingElement from '../utils/_LoadingElement';
import {MarkdownContentProps, MarkdownProps} from '../types';
import {components} from '../utils/markdown/components';
import {MarkdownContext} from '../utils/markdown/context';
import {
linkTargetPlugins,
rawHtmlPlugins,
rehypeBlockCodePlugins,
rehypeMathPlugins,
remarkMathPlugins,
remarkGfmPlugins,
} from '../utils/markdown/plugins';
import {dedentText} from '../utils/markdown/text';

// react-markdown 9 dropped `tel:` from its safe protocols; v8 allowed it.
const urlTransform: UrlTransform = url =>
/^tel:/i.test(url) ? url : defaultUrlTransform(url);

const MarkdownContent = React.memo(function MarkdownContent({
children,
dedent,
Expand All @@ -32,11 +42,15 @@ const MarkdownContent = React.memo(function MarkdownContent({
if (dangerously_allow_html) {
rehypePlugins.push(...rawHtmlPlugins);
}
rehypePlugins.push(...rehypeBlockCodePlugins);
if (link_target) {
rehypePlugins.push(...linkTargetPlugins(link_target));
}

return (
<ReactMarkdown
linkTarget={link_target}
remarkPlugins={remarkPlugins}
urlTransform={urlTransform}
rehypePlugins={rehypePlugins}
components={components}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function HighlightedCode({className, children}: ReactMarkdownCodeProps) {
}

export default function CodeRenderer({
inline,
node,
className,
children,
}: ReactMarkdownCodeProps) {
if (inline) {
if (!node?.data?.block) {
return <code className={className}>{children}</code>;
}
return <HighlightedCode className={className}>{children}</HighlightedCode>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import type {Element} from 'hast';

// Here we declare react-markdown's internal types that it will pass to our
// components. See:
// https://github.com/remarkjs/react-markdown/tree/8.0.7#appendix-b-components
// https://github.com/remarkjs/react-markdown/tree/10.1.0#appendix-b-components
export type ReactMarkdownGenericProps = {
node?: Element;
children?: ReactNode;
};

export type ReactMarkdownCodeProps = ReactMarkdownGenericProps & {
inline?: boolean;
className?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeRaw from 'rehype-raw';

import rehypeBlockCode from './rehypeBlockCode';
import rehypeDashMath from './rehypeDashMath';
import rehypeDccComponents from './rehypeDccComponents';
import rehypeLinkTarget from './rehypeLinkTarget';
import rehypeStripTags from './rehypeStripTags';

// remark plugin for GitHub Flavored Markdown: tables, strikethrough,
Expand All @@ -26,3 +28,9 @@ export const rawHtmlPlugins: PluggableList = [
rehypeStripTags,
rehypeDccComponents,
];

// Always last, so they also see elements that rehype-raw parsed from raw HTML.
export const rehypeBlockCodePlugins: PluggableList = [rehypeBlockCode];
export const linkTargetPlugins = (target: string): PluggableList => [
[rehypeLinkTarget, target],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {Plugin} from 'unified';
import type {Root} from 'hast';
import {visit} from 'unist-util-visit';

declare module 'hast' {
interface ElementData {
block?: boolean;
}
}

// Flags `<code>` inside `<pre>` so the code renderer knows to highlight it.
// Replaces the `inline` prop react-markdown dropped in v9.
const rehypeBlockCode: Plugin<[], Root> = () => tree => {
visit(tree, 'element', (node, _index, parent) => {
if (
node.tagName === 'code' &&
parent?.type === 'element' &&
parent.tagName === 'pre'
) {
node.data = {...node.data, block: true};
}
});
};

export default rehypeBlockCode;
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ function rewriteRawMath(value: string): string {
});
}

// remark-math emits `<span class="math math-inline">` / `<div class="math
// math-display">`; report which (if either) an element is.
// remark-math emits `<code class="language-math math-inline">` and
// `<pre><code class="language-math math-display">`; report which (if either)
// an element is.
function mathKind(node: Element): 'inline' | 'display' | null {
const className = node.properties?.className;
if (Array.isArray(className)) {
Expand Down Expand Up @@ -55,6 +56,18 @@ const rehypeDashMath: Plugin<[], Root> = () => tree => {
return;
}
if (node.type === 'element') {
const [first] = node.children;
if (
node.tagName === 'pre' &&
node.children.length === 1 &&
first.type === 'element' &&
mathKind(first) === 'display'
) {
node.tagName = 'dashmathjax';
node.properties = {inline: 'false'};
node.children = first.children;
return;
}
const kind = mathKind(node);
if (kind) {
node.tagName = 'dashmathjax';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {Plugin} from 'unified';
import type {Root} from 'hast';
import {visit} from 'unist-util-visit';

// Sets `target` on every link, including raw-HTML ones. Replaces the
// `linkTarget` option react-markdown dropped in v9.
const rehypeLinkTarget: Plugin<[string], Root> = target => tree => {
visit(tree, 'element', node => {
if (node.tagName === 'a') {
node.properties.target = target;
}
});
};

export default rehypeLinkTarget;
24 changes: 24 additions & 0 deletions components/dash-core-components/src/utils/markdown/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// The react-markdown 10 stack calls these at render time. They need Safari 15.4
// / Chrome 93, newer than our browserslist floor, and babel does not polyfill
// builtins.
const ObjectWithHasOwn = Object as ObjectConstructor & {
hasOwn?: (obj: object, key: PropertyKey) => boolean;
};
if (!ObjectWithHasOwn.hasOwn) {
ObjectWithHasOwn.hasOwn = (obj, key) =>
Object.prototype.hasOwnProperty.call(obj, key);
}

if (!('at' in Array.prototype)) {

Check warning on line 12 in components/dash-core-components/src/utils/markdown/polyfills.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "indexOf" or "includes" (available from ES2016) instead.

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIT63yDKHR5OEKWxHiN&open=AaIT63yDKHR5OEKWxHiN&pullRequest=4194
// eslint-disable-next-line no-extend-native
Object.defineProperty(Array.prototype, 'at', {
configurable: true,
writable: true,
value: function at<T>(this: T[], index: number) {
const i = Math.trunc(index) || 0;
return this[i < 0 ? this.length + i : i];
},
});
}

export {};
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,120 @@ def test_mkdw018_markdown_table(dash_dcc):
dash_dcc.wait_for_text_to_equal("#md td:nth-child(2)", "$2")

assert dash_dcc.get_logs() == []


@pytest.mark.parametrize(
"markdown,allow_html",
[
("Smile &#x1F600; party &#127881;", False),
("Smile <b>&#x1F600;</b> party &#127881;", True),
],
ids=["markdown", "raw_html"],
)
def test_mkdw019_astral_character_references(dash_dcc, markdown, allow_html):
# Character references above U+FFFF (emoji) must decode to the full code
# point, not a truncated one.
app = Dash()

app.layout = dcc.Markdown(markdown, dangerously_allow_html=allow_html, id="md")

dash_dcc.start_server(app)

dash_dcc.wait_for_text_to_equal("#md", "Smile \U0001F600 party \U0001F389")

assert dash_dcc.get_logs() == []


def test_mkdw020_only_code_blocks_are_highlighted(dash_dcc):
app = Dash()

app.layout = dcc.Markdown(
"""
Inline `x = 1` here.

```python
y = 2
```
""",
id="md",
)

dash_dcc.start_server(app)

dash_dcc.wait_for_element("#md pre code.hljs")
dash_dcc.wait_for_text_to_equal("#md p code", "x = 1")
assert "hljs" not in dash_dcc.find_element("#md p code").get_attribute("class")

assert dash_dcc.get_logs() == []


def test_mkdw021_block_math_is_not_wrapped_in_pre(dash_dcc):
app = Dash()

app.layout = dcc.Markdown(BLOCK_MATH, mathjax=True, id="md")

dash_dcc.start_server(app)

dash_dcc.wait_for_element("#md svg")
dash_dcc.wait_for_no_elements("#md pre")

assert dash_dcc.get_logs() == []


def test_mkdw022_link_target_applies_to_raw_html_links(dash_dcc):
app = Dash()

app.layout = dcc.Markdown(
'[md](https://example.com) <a href="https://example.com">raw</a>',
link_target="_blank",
dangerously_allow_html=True,
id="md",
)

dash_dcc.start_server(app)

dash_dcc.wait_for_text_to_equal("#md a:nth-of-type(2)", "raw")
for link in dash_dcc.find_elements("#md a"):
assert link.get_attribute("target") == "_blank"

assert dash_dcc.get_logs() == []


def test_mkdw023_tel_links_are_kept(dash_dcc):
app = Dash()

app.layout = dcc.Markdown(
'[md](tel:+15551234) <a href="tel:+15551234">raw</a>',
dangerously_allow_html=True,
id="md",
)

dash_dcc.start_server(app)

dash_dcc.wait_for_text_to_equal("#md a:nth-of-type(2)", "raw")
for link in dash_dcc.find_elements("#md a"):
assert link.get_attribute("href") == "tel:+15551234"

assert dash_dcc.get_logs() == []


def test_mkdw024_renders_without_es2022_builtins(dash_dcc):
# Simulate an older browser (Safari < 15.4) that lacks these builtins.
app = Dash()
app.index_string = app.index_string.replace(
"{%metas%}",
"{%metas%}<script>delete Object.hasOwn; delete Array.prototype.at;</script>",
)

app.layout = dcc.Markdown(
"# Title\n\nSome <b>raw</b> text &#x1F600;",
dangerously_allow_html=True,
id="md",
)

dash_dcc.start_server(app)

dash_dcc.wait_for_text_to_equal("#md b", "raw")
dash_dcc.wait_for_text_to_equal("#md h1", "Title")

assert dash_dcc.get_logs() == []
Loading
Loading