IpynbConverter: title is never extracted when a markdown cell's source is a single string (valid nbformat form)

Aperta
#2,762 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@guptaishaan ci sta già lavorando.

Dal 24/10/2026.

  • #2763 di @guptaishaan — aperta
  • #2768 di @vsmutok — aperta
  • #2777 di @tandede — aperta
  • #2778 di @houyunsun — aperta
  • #2894 di @poojithparchuri-iiitk — aperta
  • #2965 di @Primaverah — aperta

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
jupyter-notebook, python
Ambito
backend

Direzione di ricerca

Inspect packages/markitdown/src/markitdown/converters/_ipynb_converter.py lines 79-81 where the title loop iterates source_lines. Apply the normalization snippet from the issue so single-string source splits into lines, then verify with the provided Python repro that both notebook forms return the title. A linked PR #2763 already exists for this change.

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

Descrizione

Problem

IpynbConverter never extracts a title from a notebook whose markdown cells store source as a single string, even though that's a fully valid representation under the nbformat schema (the Jupyter format spec allows source to be "a single string" or a list of strings — nbformat.validator accepts both). Notebooks written by scripts and many third-party tools use the single-string form.

Repro

from markitdown import MarkItDown
import json

md = MarkItDown()

# source as a list (what Jupyter itself usually writes)
nb_list = {
    "nbformat": 4, "nbformat_minor": 5,
    "cells": [{"cell_type": "markdown", "source": ["# My Title\n", "hello"]}],
}

# source as a single string (equally valid)
nb_str = {
    "nbformat": 4, "nbformat_minor": 5,
    "cells": [{"cell_type": "markdown", "source": "# My Title\nhello"}],
}

import tempfile, pathlib
for name, nb in (("list", nb_list), ("string", nb_str)):
    p = pathlib.Path(tempfile.mkdtemp()) / "t.ipynb"
    p.write_text(json.dumps(nb))
    result = md.convert(str(p))
    print(name, "->", repr(result.title))

Output:

list -> 'My Title'
string -> None

The body content converts identically in both cases; only the extracted title differs.

Root cause

In packages/markitdown/src/markitdown/converters/_ipynb_converter.py, the title-extraction loop (lines 79-81 of the current main) iterates source_lines assuming it is a list of whole lines:

for line in source_lines:
    if line.startswith("# "):
        title = line.removeprefix("# ").strip()
        break

When source is a single string, for line in source_lines iterates characters, so line.startswith("# ") can never be true and the title is silently lost. The body conversion only keeps working by luck: "".join(source_lines) happens to reconstruct a string source correctly (and it would also misbehave if the string contained multi-line content in the code/raw branches' fence formatting, since no trailing-newline handling exists).

Suggested fix: normalize once, e.g.

source = cell.get("source", [])
source_lines = source if isinstance(source, list) else source.splitlines(keepends=True)

before the loop, so both valid nbformat forms take the same path.

I verified this by running the current main version of IpynbConverter._convert directly against both notebooks above (Python 3.13.5, commit 7482c092d9c0074a89803c9fdb63a969e7fe17fc).

Lingua principale
Python
Stelle
198k
Fork
15k
Merge medio
2h 16m
PR unite (30g)
9

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

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 microsoft/markitdown

Tutte le issue di microsoft/markitdown

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.