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

Aperta Adatta ai principianti
#6,789 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
80/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
frontend

Direzione di ricerca

Inizia da MarkdownTableOfContents.rebuild_table_of_contents, dove il ciclo usa NUMERALS[level] per i nodi che crea scendendo e chiama expand() su ogni nodo esistente che attraversa. Esegui lo script di chi ha segnalato, che usa MarkdownViewer e Tree, e confronta le etichette e i messaggi Tree.NodeExpanded con l'output atteso nell'issue. Il lavoro è completo quando il nodo del livello saltato mostra il numerale corretto e ogni nodo viene espanso una sola volta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.