From 5a6d7183443551e4981d60a5fddc4a5c2c715820 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 18 Feb 2023 23:15:15 +0000 Subject: [PATCH] list item improvements --- examples/example.md | 16 ++++++++++++++++ src/textual/widgets/_markdown.py | 21 +++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/examples/example.md b/examples/example.md index 83495ba2f..92a6e013a 100644 --- a/examples/example.md +++ b/examples/example.md @@ -42,6 +42,22 @@ Two tildes indicates strikethrough, e.g. `~~cross out~~` render ~~cross out~~. Inline code is indicated by backticks. e.g. `import this`. +## Lists + +1. Lists can be ordered +2. Lists can be unordered + - Foo + - Bar + - Jessica + - Reg + - Green + - January + - February + - March + - Blue + - Paul + - Baz + ## Fences Fenced code blocks are introduced with three back-ticks and the optional parser. Here we are rendering the code in a sub-widget with syntax highlighting and indent guides. diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index 1328fa5b2..93bb7e306 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -345,7 +345,7 @@ class MarkdownBullet(Widget): } """ - symbol = reactive("●​ ") + symbol = reactive("●​") """The symbol for the bullet.""" def render(self) -> Text: @@ -381,6 +381,14 @@ class MarkdownListItem(MarkdownBlock): self._blocks.clear() +class MarkdownOrderedListItem(MarkdownListItem): + pass + + +class MarkdownUnorderedListItem(MarkdownListItem): + pass + + class MarkdownFence(MarkdownBlock): """A fence Markdown block.""" @@ -452,6 +460,8 @@ class Markdown(Widget): """ COMPONENT_CLASSES = {"em", "strong", "s", "code_inline"} + BULLETS = ["⏺ ", "■ ", "• ", "‣ "] + def __init__( self, markdown: str | None = None, @@ -548,8 +558,15 @@ class Markdown(Widget): elif token.type == "ordered_list_open": stack.append(MarkdownOrderedList()) elif token.type == "list_item_open": + item_count = sum( + 1 for block in stack if isinstance(block, MarkdownUnorderedListItem) + ) stack.append( - MarkdownListItem(f"{token.info}. " if token.info else "● ") + MarkdownOrderedListItem(f" {token.info}. ") + if token.info + else MarkdownUnorderedListItem( + self.BULLETS[item_count % len(self.BULLETS)] + ) ) elif token.type == "table_open": stack.append(MarkdownTable())