From 36ad1bd1e2e833c0c4c05a8eb9514cf1889fad67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:03:54 +0000 Subject: [PATCH 1/5] Escape markup in markdown headings. The markup would already be 'escaped' (ignored, really) in the markdown document itself, but it would be processed when building the table of contents because of the way the widget 'Tree' internally processes labels. This was changed, so that we create our own 'Text' instances for the labels, which means we get to avoid markup processing. Related issue: #3689. --- CHANGELOG.md | 1 + src/textual/widgets/_markdown.py | 3 ++- tests/test_markdownviewer.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d269fc123..ebff906cd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - CSS error reporting will no longer provide links to the files in question https://github.com/Textualize/textual/pull/3582 - inline CSS error reporting will report widget/class variable where the CSS was read from https://github.com/Textualize/textual/pull/3582 - Breaking change: Setting `Select.value` to `None` no longer clears the selection (See `Select.BLANK` and `Select.clear`) https://github.com/Textualize/textual/pull/3614 +- Markup in markdown headings is now escaped when building the TOC https://github.com/Textualize/textual/issues/3689 ## [0.41.0] - 2023-10-31 diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index ac6de4f4bc..96b81cc6eb 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -937,7 +937,8 @@ def set_table_of_contents(self, table_of_contents: TableOfContentsType) -> None: node.allow_expand = True else: node = node.add(NUMERALS[level], expand=True) - node.add_leaf(f"[dim]{NUMERALS[level]}[/] {name}", {"block_id": block_id}) + node_label = Text.from_markup(f"[dim]{NUMERALS[level]}[/] ") + Text(name) + node.add_leaf(node_label, {"block_id": block_id}) async def _on_tree_node_selected(self, message: Tree.NodeSelected) -> None: node_data = message.node.data diff --git a/tests/test_markdownviewer.py b/tests/test_markdownviewer.py index 27ccf0da99..8d94d4b946 100644 --- a/tests/test_markdownviewer.py +++ b/tests/test_markdownviewer.py @@ -1,10 +1,12 @@ from pathlib import Path import pytest +from rich.text import Text +import textual.widgets._markdown as MD from textual.app import App, ComposeResult from textual.geometry import Offset -from textual.widgets import Markdown, MarkdownViewer +from textual.widgets import Markdown, MarkdownViewer, Tree TEST_MARKDOWN = """\ * [First]({{file}}#first) @@ -45,8 +47,12 @@ async def test_markdown_file_viewer_anchor_link(tmp_path, link: int) -> None: class MarkdownStringViewerApp(App[None]): + def __init__(self, markdown_string: str) -> None: + self.markdown_string = markdown_string + super().__init__() + def compose(self) -> ComposeResult: - yield MarkdownViewer(TEST_MARKDOWN.replace("{{file}}", "")) + yield MarkdownViewer(self.markdown_string) async def on_mount(self) -> None: self.query_one(MarkdownViewer).show_table_of_contents = False @@ -57,8 +63,27 @@ async def test_markdown_string_viewer_anchor_link(link: int) -> None: """Test https://github.com/Textualize/textual/issues/3094 Also https://github.com/Textualize/textual/pull/3244#issuecomment-1710278718.""" - async with MarkdownStringViewerApp().run_test() as pilot: + async with MarkdownStringViewerApp( + TEST_MARKDOWN.replace("{{file}}", "") + ).run_test() as pilot: # There's not really anything to test *for* here, but the lack of an # exception is the win (before the fix this is testing it would have # been FileNotFoundError). await pilot.click(Markdown, Offset(2, link)) + + +@pytest.mark.parametrize("text", ["Hey [[/test]]", "[i]Hey there[/i]"]) +async def test_headings_that_look_like_they_contain_markup(text: str) -> None: + """Regression test for https://github.com/Textualize/textual/issues/3689. + + Things that look like markup are escaped in markdown headings in the table of contents. + """ + + document = f"# {text}" + async with MarkdownStringViewerApp(document).run_test() as pilot: + assert pilot.app.query_one(MD.MarkdownH1)._text == Text(text) + toc_tree = pilot.app.query_one(MD.MarkdownTableOfContents).query_one(Tree) + # The toc label looks like "I {text}" but the I is styled so we drop it. + toc_label = toc_tree.root.children[0].label + _, text_label = toc_label.divide([2]) + assert text_label == Text(text) From 5230e016b630fd2a425b3897159565b60e7b1ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Mon, 20 Nov 2023 09:52:12 +0000 Subject: [PATCH 2/5] Update CHANGELOG.md Co-authored-by: Dave Pearson --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebff906cd1..46fa7b788f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - CSS error reporting will no longer provide links to the files in question https://github.com/Textualize/textual/pull/3582 - inline CSS error reporting will report widget/class variable where the CSS was read from https://github.com/Textualize/textual/pull/3582 - Breaking change: Setting `Select.value` to `None` no longer clears the selection (See `Select.BLANK` and `Select.clear`) https://github.com/Textualize/textual/pull/3614 -- Markup in markdown headings is now escaped when building the TOC https://github.com/Textualize/textual/issues/3689 +- Rich markup in markdown headings is now escaped when building the TOC https://github.com/Textualize/textual/issues/3689 ## [0.41.0] - 2023-10-31 From aeee036f3236f6cfc15d063d3fff585b0ea96a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:26:54 +0000 Subject: [PATCH 3/5] Optimisation. Related review comment: https://github.com/Textualize/textual/pull/3697/#discussion_r1397241681 --- src/textual/widgets/_markdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index 96b81cc6eb..c33c188637 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -937,7 +937,7 @@ def set_table_of_contents(self, table_of_contents: TableOfContentsType) -> None: node.allow_expand = True else: node = node.add(NUMERALS[level], expand=True) - node_label = Text.from_markup(f"[dim]{NUMERALS[level]}[/] ") + Text(name) + node_label = Text.assemble((f"{NUMERALS[level]} ", Style(dim=True)), name) node.add_leaf(node_label, {"block_id": block_id}) async def _on_tree_node_selected(self, message: Tree.NodeSelected) -> None: From 5ae7b27992f50ed64b4bd432be4e949afb5fa225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:32:27 +0000 Subject: [PATCH 4/5] Optimisation. Related review comment: https://github.com/Textualize/textual/pull/3697#discussion_r1398983336 --- src/textual/widgets/_markdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index c33c188637..de8e75059a 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -937,7 +937,7 @@ def set_table_of_contents(self, table_of_contents: TableOfContentsType) -> None: node.allow_expand = True else: node = node.add(NUMERALS[level], expand=True) - node_label = Text.assemble((f"{NUMERALS[level]} ", Style(dim=True)), name) + node_label = Text.assemble((f"{NUMERALS[level]} ", "dim"), name) node.add_leaf(node_label, {"block_id": block_id}) async def _on_tree_node_selected(self, message: Tree.NodeSelected) -> None: From 84df9ac38ef622cfdafdae39b30dbe551b48d6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:50:41 +0000 Subject: [PATCH 5/5] Update snapshot. --- .../__snapshots__/test_snapshots.ambr | 138 +++++++++--------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 690439b32d..a88f5b70a1 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -21069,145 +21069,145 @@ font-weight: 700; } - .terminal-1944007215-matrix { + .terminal-3017552431-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-1944007215-title { + .terminal-3017552431-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-1944007215-r1 { fill: #c5c8c6 } - .terminal-1944007215-r2 { fill: #24292f } - .terminal-1944007215-r3 { fill: #121212 } - .terminal-1944007215-r4 { fill: #e1e1e1 } - .terminal-1944007215-r5 { fill: #e2e3e3 } - .terminal-1944007215-r6 { fill: #96989b } - .terminal-1944007215-r7 { fill: #0053aa } - .terminal-1944007215-r8 { fill: #008139 } - .terminal-1944007215-r9 { fill: #dde8f3;font-weight: bold } - .terminal-1944007215-r10 { fill: #939393;font-weight: bold } - .terminal-1944007215-r11 { fill: #14191f } - .terminal-1944007215-r12 { fill: #e2e3e3;font-weight: bold } - .terminal-1944007215-r13 { fill: #4ebf71;font-weight: bold } - .terminal-1944007215-r14 { fill: #e1e1e1;font-style: italic; } - .terminal-1944007215-r15 { fill: #e1e1e1;font-weight: bold } + .terminal-3017552431-r1 { fill: #c5c8c6 } + .terminal-3017552431-r2 { fill: #24292f } + .terminal-3017552431-r3 { fill: #121212 } + .terminal-3017552431-r4 { fill: #e1e1e1 } + .terminal-3017552431-r5 { fill: #e2e3e3 } + .terminal-3017552431-r6 { fill: #96989b } + .terminal-3017552431-r7 { fill: #0053aa } + .terminal-3017552431-r8 { fill: #008139 } + .terminal-3017552431-r9 { fill: #dde8f3;font-weight: bold } + .terminal-3017552431-r10 { fill: #939393;font-weight: bold } + .terminal-3017552431-r11 { fill: #14191f } + .terminal-3017552431-r12 { fill: #e2e3e3;font-weight: bold } + .terminal-3017552431-r13 { fill: #4ebf71;font-weight: bold } + .terminal-3017552431-r14 { fill: #e1e1e1;font-style: italic; } + .terminal-3017552431-r15 { fill: #e1e1e1;font-weight: bold } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - MarkdownExampleApp + MarkdownExampleApp - - - - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ - ▼  Markdown Viewer - ├──  FeaturesMarkdown Viewer - ├──  Tables - └──  Code Blocks▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ - This is an example of Textual's MarkdownViewer - widget. - - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▅ - -                  Features                  - - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ - Markdown syntax and extensions are supported. - - ● Typography emphasisstronginline code - etc. - ● Headers - ● Lists (bullet and ordered) - ● Syntax highlighted code blocks - ● Tables! - - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ - + + + + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + ▼ Ⅰ Markdown Viewer + ├── Ⅱ FeaturesMarkdown Viewer + ├── Ⅱ Tables + └── Ⅱ Code Blocks▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + This is an example of Textual's MarkdownViewer + widget. + + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▅ + +                  Features                  + + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + Markdown syntax and extensions are supported. + + ● Typography emphasisstronginline code + etc. + ● Headers + ● Lists (bullet and ordered) + ● Syntax highlighted code blocks + ● Tables! + + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ +