Skip to content

Commit b9726bb

Browse files
committed
Auto-increment ordered lists within Markdown
Addresses #2002.
1 parent 0a9b960 commit b9726bb

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [0.18.0] - Unreleased
9+
10+
### Fixed
11+
12+
- The `Markdown` widget now auto-increments ordered lists https://github.com/Textualize/textual/issues/2002
13+
814
## [0.17.1] - 2023-03-30
915

1016
### Fixed

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)