Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown improvements #2803

Merged
merged 14 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed indented code blocks not showing up in `Markdown` https://github.com/Textualize/textual/issues/2781
- Fixed inline code blocks in lists showing out of order in `Markdown` https://github.com/Textualize/textual/issues/2676
- Fixed list items in a `Markdown` being added to the focus chain https://github.com/Textualize/textual/issues/2380

### Added

- Added a method of allowing third party code to handle unhandled tokens in `Markdown` https://github.com/Textualize/textual/pull/2803
- Added `MarkdownBlock` as an exported symbol in `textual.widgets.markdown` https://github.com/Textualize/textual/pull/2803

### Changed

- Tooltips are now inherited, so will work with compound widgets
Expand Down
33 changes: 25 additions & 8 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Callable, Iterable

from markdown_it import MarkdownIt
from markdown_it.token import Token
from rich import box
from rich.style import Style
from rich.syntax import Syntax
Expand All @@ -12,7 +13,7 @@
from typing_extensions import TypeAlias

from ..app import ComposeResult
from ..containers import Horizontal, VerticalScroll
from ..containers import Horizontal, Vertical, VerticalScroll
from ..events import Mount
from ..message import Message
from ..reactive import reactive, var
Expand Down Expand Up @@ -269,7 +270,7 @@ class MarkdownBulletList(MarkdownList):
width: 1fr;
}

MarkdownBulletList VerticalScroll {
MarkdownBulletList Vertical {
height: auto;
width: 1fr;
}
Expand All @@ -280,7 +281,7 @@ def compose(self) -> ComposeResult:
if isinstance(block, MarkdownListItem):
bullet = MarkdownBullet()
bullet.symbol = block.bullet
yield Horizontal(bullet, VerticalScroll(*block._blocks))
yield Horizontal(bullet, Vertical(*block._blocks))
self._blocks.clear()


Expand All @@ -298,7 +299,7 @@ class MarkdownOrderedList(MarkdownList):
width: 1fr;
}

MarkdownOrderedList VerticalScroll {
MarkdownOrderedList Vertical {
height: auto;
width: 1fr;
}
Expand All @@ -321,7 +322,7 @@ def compose(self) -> ComposeResult:
if isinstance(block, MarkdownListItem):
bullet = MarkdownBullet()
bullet.symbol = f"{number}{suffix}".rjust(symbol_size + 1)
yield Horizontal(bullet, VerticalScroll(*block._blocks))
yield Horizontal(bullet, Vertical(*block._blocks))

self._blocks.clear()

Expand Down Expand Up @@ -449,7 +450,7 @@ class MarkdownListItem(MarkdownBlock):
height: auto;
}

MarkdownListItem > VerticalScroll {
MarkdownListItem > Vertical {
width: 1fr;
height: auto;
}
Expand Down Expand Up @@ -644,6 +645,18 @@ async def load(self, path: Path) -> bool:
self.update(markdown)
return True

def unhandled_token(self, token: Token) -> MarkdownBlock | None:
"""Process an unhandled token.

Args:
token: The token to handle.

Returns:
Either a widget to be added to the output, or `None`.
"""
del token
davep marked this conversation as resolved.
Show resolved Hide resolved
return None

def update(self, markdown: str) -> None:
"""Update the document with new Markdown.

Expand Down Expand Up @@ -777,14 +790,18 @@ def update(self, markdown: str) -> None:
style_stack.pop()

stack[-1].set_content(content)
elif token.type == "fence":
output.append(
elif token.type in ("fence", "code_block"):
(stack[-1]._blocks if stack else output).append(
MarkdownFence(
self,
token.content.rstrip(),
token.info,
)
)
else:
external = self.unhandled_token(token)
if external is not None:
(stack[-1]._blocks if stack else output).append(external)

self.post_message(Markdown.TableOfContentsUpdated(self, table_of_contents))
with self.app.batch_update():
Expand Down
4 changes: 2 additions & 2 deletions src/textual/widgets/markdown.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from ._markdown import Markdown, MarkdownTableOfContents
from ._markdown import Markdown, MarkdownBlock, MarkdownTableOfContents

__all__ = ["MarkdownTableOfContents", "Markdown"]
__all__ = ["MarkdownTableOfContents", "Markdown", "MarkdownBlock"]
91 changes: 91 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Unit tests for the Markdown widget."""

from __future__ import annotations

from typing import Iterator

import pytest
from markdown_it.token import Token

import textual.widgets._markdown as MD
from textual.app import App, ComposeResult
from textual.widget import Widget
from textual.widgets import Markdown
from textual.widgets.markdown import MarkdownBlock


class UnhandledToken(MarkdownBlock):
def __init__(self, markdown: Markdown, token: Token) -> None:
super().__init__(markdown)
self._token = token

def __repr___(self) -> str:
return self._token.type


class FussyMarkdown(Markdown):
def unhandled_token(self, token: Token) -> MarkdownBlock | None:
return UnhandledToken(self, token)


class MarkdownApp(App[None]):
def __init__(self, markdown: str) -> None:
super().__init__()
self._markdown = markdown

def compose(self) -> ComposeResult:
yield FussyMarkdown(self._markdown)


@pytest.mark.parametrize(
["document", "expected_nodes"],
[
# Basic markup.
("", []),
("# Hello", [MD.MarkdownH1]),
("## Hello", [MD.MarkdownH2]),
("### Hello", [MD.MarkdownH3]),
("#### Hello", [MD.MarkdownH4]),
("##### Hello", [MD.MarkdownH5]),
("###### Hello", [MD.MarkdownH6]),
("---", [MD.MarkdownHorizontalRule]),
("Hello", [MD.MarkdownParagraph]),
("Hello\nWorld", [MD.MarkdownParagraph]),
("> Hello", [MD.MarkdownBlockQuote, MD.MarkdownParagraph]),
("- One\n-Two", [MD.MarkdownBulletList, MD.MarkdownParagraph]),
(
"1. One\n2. Two",
[MD.MarkdownOrderedList, MD.MarkdownParagraph, MD.MarkdownParagraph],
),
(" 1", [MD.MarkdownFence]),
("```\n1\n```", [MD.MarkdownFence]),
("```python\n1\n```", [MD.MarkdownFence]),
("""| One | Two |\n| :- | :- |\n| 1 | 2 |""", [MD.MarkdownTable]),
# Test for https://github.com/Textualize/textual/issues/2676
(
"- One\n```\nTwo\n```\n- Three\n",
[
MD.MarkdownBulletList,
MD.MarkdownParagraph,
MD.MarkdownFence,
MD.MarkdownBulletList,
MD.MarkdownParagraph,
],
),
],
)
async def test_markdown_nodes(
document: str, expected_nodes: list[Widget | list[Widget]]
) -> None:
"""A Markdown document should parse into the expected Textual node list."""

def markdown_nodes(root: Widget) -> Iterator[MarkdownBlock]:
for node in root.children:
if isinstance(node, MarkdownBlock):
yield node
yield from markdown_nodes(node)

async with MarkdownApp(document).run_test() as pilot:
assert [
node.__class__ for node in markdown_nodes(pilot.app.query_one(Markdown))
] == expected_nodes
Loading