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

Abierto Apto para principiantes
#6,789 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
80/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
frontend

Línea de trabajo

Empieza con MarkdownTableOfContents.rebuild_table_of_contents, donde el bucle usa NUMERALS[level] para los nodos que crea en el descenso y llama a expand() en cada nodo existente que recorre. Ejecuta el script del reportero, que usa MarkdownViewer y Tree, y compara las etiquetas y los mensajes Tree.NodeExpanded con la salida esperada en el issue. Está terminado cuando el nodo de nivel omitido muestra el numeral correcto y cada nodo se expande solo una vez.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
37.7k
Forks
1.5k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Textualize/textual

Todos los issues de Textualize/textual

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.