list item improvements

This commit is contained in:
Will McGugan
2023-02-18 23:15:15 +00:00
parent 104f784a20
commit 5a6d718344
2 changed files with 35 additions and 2 deletions

View File

@@ -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`. 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 ## 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. 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.

View File

@@ -345,7 +345,7 @@ class MarkdownBullet(Widget):
} }
""" """
symbol = reactive("●​ ") symbol = reactive("●​")
"""The symbol for the bullet.""" """The symbol for the bullet."""
def render(self) -> Text: def render(self) -> Text:
@@ -381,6 +381,14 @@ class MarkdownListItem(MarkdownBlock):
self._blocks.clear() self._blocks.clear()
class MarkdownOrderedListItem(MarkdownListItem):
pass
class MarkdownUnorderedListItem(MarkdownListItem):
pass
class MarkdownFence(MarkdownBlock): class MarkdownFence(MarkdownBlock):
"""A fence Markdown block.""" """A fence Markdown block."""
@@ -452,6 +460,8 @@ class Markdown(Widget):
""" """
COMPONENT_CLASSES = {"em", "strong", "s", "code_inline"} COMPONENT_CLASSES = {"em", "strong", "s", "code_inline"}
BULLETS = ["", "", "", ""]
def __init__( def __init__(
self, self,
markdown: str | None = None, markdown: str | None = None,
@@ -548,8 +558,15 @@ class Markdown(Widget):
elif token.type == "ordered_list_open": elif token.type == "ordered_list_open":
stack.append(MarkdownOrderedList()) stack.append(MarkdownOrderedList())
elif token.type == "list_item_open": elif token.type == "list_item_open":
item_count = sum(
1 for block in stack if isinstance(block, MarkdownUnorderedListItem)
)
stack.append( 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": elif token.type == "table_open":
stack.append(MarkdownTable()) stack.append(MarkdownTable())