Skip to content

Commit

Permalink
Auto-increment ordered lists within Markdown
Browse files Browse the repository at this point in the history
Addresses Textualize#2002.
  • Loading branch information
davep committed Mar 30, 2023
1 parent 0a9b960 commit b9726bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.18.0] - Unreleased

### Fixed

- The `Markdown` widget now auto-increments ordered lists https://github.com/Textualize/textual/issues/2002

## [0.17.1] - 2023-03-30

### Fixed
Expand Down
19 changes: 13 additions & 6 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from pathlib import Path, PurePath
from typing import Iterable, Callable
from typing import Callable, Iterable

from markdown_it import MarkdownIt
from rich import box
Expand Down Expand Up @@ -304,15 +304,22 @@ class MarkdownOrderedList(MarkdownList):
"""

def compose(self) -> ComposeResult:
suffix = ". "
start = 1
if self._blocks and isinstance(self._blocks[0], MarkdownOrderedListItem):
try:
start = int(self._blocks[0].bullet)
except ValueError:
pass
symbol_size = max(
len(block.bullet)
for block in self._blocks
len(f"{number}{suffix}")
for number, block in enumerate(self._blocks, start)
if isinstance(block, MarkdownListItem)
)
for block in self._blocks:
for number, block in enumerate(self._blocks, start):
if isinstance(block, MarkdownListItem):
bullet = MarkdownBullet()
bullet.symbol = block.bullet.rjust(symbol_size + 1)
bullet.symbol = f"{number}{suffix}".rjust(symbol_size + 1)
yield Horizontal(bullet, VerticalScroll(*block._blocks))

self._blocks.clear()
Expand Down Expand Up @@ -636,7 +643,7 @@ async def update(self, markdown: str) -> None:
stack.append(MarkdownOrderedList())
elif token.type == "list_item_open":
if token.info:
stack.append(MarkdownOrderedListItem(f"{token.info}. "))
stack.append(MarkdownOrderedListItem(token.info))
else:
item_count = sum(
1
Expand Down

0 comments on commit b9726bb

Please sign in to comment.