Markdown table of contents: the node added for a skipped heading level shows the wrong numeral

Open Beginner friendly
#6,789 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.