Markdown table of contents: the node added for a skipped heading level shows the wrong numeral
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 80/100
Research direction
Start with MarkdownTableOfContents.rebuild_table_of_contents, where the loop uses NUMERALS[level] for the nodes it creates on the way down and calls expand() on every existing node it passes. Run the reporter's script, which uses MarkdownViewer and Tree, and compare the labels and the Tree.NodeExpanded messages with the expected output in the issue. Done means the skipped-level node shows the correct numeral and each node is expanded only once.
Written by the indexing model from the issue text.
Description
Hi! I hit this while writing generated tests against Textual's widgets.
When a document skips a heading level (# then ###), MarkdownTableOfContents.rebuild_table_of_contents adds a node for the missing level. That node is labelled with the numeral of the heading being added, not of its own level, so a level-2 node reads "Ⅲ" next to real level-2 headings that read "Ⅱ". Also, every heading under a node calls expand() on it again, so a Tree with several sub-headings posts Tree.NodeExpanded for the same node several times.
import asyncio
import textual
from textual.app import App, ComposeResult
from textual.widgets import MarkdownViewer, Tree
DOCUMENT = """\
# One
### One point one point one
#### Deeper
## Two
"""
class TocApp(App):
def compose(self) -> ComposeResult:
yield MarkdownViewer(DOCUMENT)
def on_tree_node_expanded(self, event: Tree.NodeExpanded) -> None:
self.expanded.append(str(event.node.label))
expanded: list[str] = []
def dump(node, depth=0):
for child in node.children:
print(" " * depth + str(child.label), "" if child.allow_expand else "(leaf)")
dump(child, depth + 1)
async def main() -> None:
app = TocApp()
async with app.run_test() as pilot:
await pilot.pause()
tree = app.query_one(Tree)
dump(tree.root)
print("NodeExpanded messages:", app.expanded)
print("Textual", textual.__version__)
asyncio.run(main())
Output on Textual 8.2.7 (main at 5e5b7ef is the same):
Textual 8.2.7
Ⅰ One
Ⅲ
Ⅲ One point one point one
Ⅳ Deeper (leaf)
Ⅱ Two (leaf)
NodeExpanded messages: ['Ⅰ One', 'Ⅰ One', 'Ⅲ', 'Ⅲ One point one point one', 'Ⅰ One']
Expected: the node for the missing level reads "Ⅱ", like "Ⅱ Two" beside it, and each node is expanded once: NodeExpanded messages: ['Ⅰ One', 'Ⅲ One point one point one'].
The loop in rebuild_table_of_contents uses NUMERALS[level] (the new heading's level) for the nodes it creates on the way down, and calls node.expand() and sets allow_expand on every existing node it passes. I'm happy to send a PR.
- Dominant language
- Python
- Stars
- 37.7k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/textual
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6790 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6787 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6782 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Textualize/textual#6778 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/textual#6777 · 1 comment ·
All issues in Textualize/textual
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day