Skip to content

Fix ipynb title extraction stripping leading # from heading text#2052

Open
LeSingh1 wants to merge 1 commit into
microsoft:mainfrom
LeSingh1:fix-ipynb-title-lstrip
Open

Fix ipynb title extraction stripping leading # from heading text#2052
LeSingh1 wants to merge 1 commit into
microsoft:mainfrom
LeSingh1:fix-ipynb-title-lstrip

Conversation

@LeSingh1
Copy link
Copy Markdown

@LeSingh1 LeSingh1 commented Jun 2, 2026

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.

The title was extracted with line.lstrip("# "), which strips any
combination of leading "#" and space characters rather than the single
"# " heading marker. A heading whose text begins with "#" (for example
"# #100DaysOfCode") lost those characters and produced "100DaysOfCode".

Remove only the "# " marker prefix via slicing so the heading text is
preserved. Add tests covering a leading "#" in the title and extra
internal spacing after the marker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant