Skip to content

Commit 4fcf44c

Browse files
authored
Merge pull request #2177 from davep/markdown-number-list
Auto-increment ordered lists within Markdown
2 parents 0750bc2 + 19a0846 commit 4fcf44c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fixed bindings persistance https://github.com/Textualize/textual/issues/1613
13+
- The `Markdown` widget now auto-increments ordered lists https://github.com/Textualize/textual/issues/2002
1314

1415
## [0.17.1] - 2023-03-30
1516

src/textual/widgets/_markdown.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from pathlib import Path, PurePath
4-
from typing import Iterable, Callable
4+
from typing import Callable, Iterable
55

66
from markdown_it import MarkdownIt
77
from rich import box
@@ -304,15 +304,22 @@ class MarkdownOrderedList(MarkdownList):
304304
"""
305305

306306
def compose(self) -> ComposeResult:
307+
suffix = ". "
308+
start = 1
309+
if self._blocks and isinstance(self._blocks[0], MarkdownOrderedListItem):
310+
try:
311+
start = int(self._blocks[0].bullet)
312+
except ValueError:
313+
pass
307314
symbol_size = max(
308-
len(block.bullet)
309-
for block in self._blocks
315+
len(f"{number}{suffix}")
316+
for number, block in enumerate(self._blocks, start)
310317
if isinstance(block, MarkdownListItem)
311318
)
312-
for block in self._blocks:
319+
for number, block in enumerate(self._blocks, start):
313320
if isinstance(block, MarkdownListItem):
314321
bullet = MarkdownBullet()
315-
bullet.symbol = block.bullet.rjust(symbol_size + 1)
322+
bullet.symbol = f"{number}{suffix}".rjust(symbol_size + 1)
316323
yield Horizontal(bullet, VerticalScroll(*block._blocks))
317324

318325
self._blocks.clear()
@@ -636,7 +643,7 @@ async def update(self, markdown: str) -> None:
636643
stack.append(MarkdownOrderedList())
637644
elif token.type == "list_item_open":
638645
if token.info:
639-
stack.append(MarkdownOrderedListItem(f"{token.info}. "))
646+
stack.append(MarkdownOrderedListItem(token.info))
640647
else:
641648
item_count = sum(
642649
1

0 commit comments

Comments
 (0)