Skip to content

fix(ipynb): keep the title when a cell's source is a single string - #2463

Open
Chirag Honnyal (Chirag6722) wants to merge 1 commit into
microsoft:mainfrom
Chirag6722:fix/2115-ipynb-string-source-title
Open

fix(ipynb): keep the title when a cell's source is a single string#2463
Chirag Honnyal (Chirag6722) wants to merge 1 commit into
microsoft:mainfrom
Chirag6722:fix/2115-ipynb-string-source-title

Conversation

@Chirag6722

Copy link
Copy Markdown

Fixes #2115.

The bug

The nbformat spec allows a cell's source to be either a list of lines or a single string. IpynbConverter._convert iterates the value directly when looking for the first # heading:

source_lines = cell.get("source", [])
...
for line in source_lines:
    if line.startswith("# "):

When source is a string, that loop walks it one character at a time, so startswith("# ") never matches and result.title comes back None.

What makes it easy to miss: the body is fine either way, because "".join(source_lines) over a string simply rebuilds the string. Same value, two consumers, and only the title scan cared about the difference. So the converter looked correct on every notebook that happened to use the list form, and lost the title silently on the ones that didn't.

The change

Normalise a string source into lines with splitlines(keepends=True) before the scan. Five lines including the comment. The list form is untouched.

Verification

From packages/markitdown, in a fresh venv with .[all]:

  • New regression test test_ipynb_string_source_keeps_title, placed next to the existing test_ipynb_heading_title_preserves_leading_hash and registered in the same runner list. It converts one notebook both ways and asserts the two titles match and the two bodies match, so it also pins the fact that the body path was never the problem.
  • Reverting only the converter change and keeping the test fails it with AssertionError: assert None == 'My Report'.
  • Full suite: 392 passed, 4 skipped, 3 failed. All three failures reproduce on unmodified main on this machine and are unrelated to notebooks: test_file_uris and test_convert_case_insensitive_uri_schemes are Windows drive-letter path handling, test_speech_transcription needs a network service.
  • black --check clean on both files.

Relationship to #2113

That PR, open since June, contains the same one-line idea for this bug but bundles it with an unrelated PDF numbering fix, and it has had no maintainer review in three months. Credit to Sahilalgo8 for spotting it first. This is the notebook half on its own with a regression test, so it can be reviewed as one thing; if #2113 is preferred instead, happy to close this.


AI help was taken for this change.

nbformat allows a cell's `source` to be one string as well as a list of
lines. The title scan iterated the value directly, so a string was walked
character by character and `line.startswith("# ")` never matched. The body
was unaffected, because `"".join()` over a string rebuilds it, which is why
only the title went missing.

Normalise a string source into lines before the scan. Regression test
converts the same notebook both ways and asserts the titles and bodies match.

Fixes microsoft#2115
@Chirag6722

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

bug: IpynbConverter loses document title when cell source is a string instead of list

1 participant