markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

Fix ipynb title extraction stripping leading # from heading text

Open LeSingh1 opened this issue 6 months ago • 0 comments

Problem

When converting a Jupyter notebook, the document title is taken from the first Markdown # heading. The extraction uses:

title = line.lstrip("# ").strip()

str.lstrip(chars) treats its argument as a set of characters to strip, not a prefix. So it removes every leading # and space character, not just the single "# " marker. A heading whose text itself begins with # loses those characters.

Example: a notebook whose first heading is # #100DaysOfCode produces the title 100DaysOfCode instead of #100DaysOfCode.

Fix

Strip only the "# " marker by slicing off its known length:

title = line[len("# ") :].strip()

The line already passed line.startswith("# "), so the prefix is guaranteed present. Outer whitespace is still trimmed by .strip().

Tests

Added tests/test_ipynb_converter.py:

  • a heading whose text starts with # keeps it (# #100DaysOfCode -> #100DaysOfCode)
  • only the marker is removed, internal spacing preserved
  • a plain heading is unchanged

All three pass; the first fails on the old code.


Disclosure: I used AI assistance while preparing this change. I reproduced the bug and verified the fix and tests locally.

LeSingh1 avatar Jun 02 '26 01:06 LeSingh1