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
Open
fix(ipynb): keep the title when a cell's source is a single string#2463Chirag Honnyal (Chirag6722) wants to merge 1 commit into
Chirag Honnyal (Chirag6722) wants to merge 1 commit into
Conversation
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
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2115.
The bug
The nbformat spec allows a cell's
sourceto be either a list of lines or a single string.IpynbConverter._convertiterates the value directly when looking for the first#heading:When
sourceis a string, that loop walks it one character at a time, sostartswith("# ")never matches andresult.titlecomes backNone.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
sourceinto lines withsplitlines(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]:test_ipynb_string_source_keeps_title, placed next to the existingtest_ipynb_heading_title_preserves_leading_hashand 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.AssertionError: assert None == 'My Report'.mainon this machine and are unrelated to notebooks:test_file_urisandtest_convert_case_insensitive_uri_schemesare Windows drive-letter path handling,test_speech_transcriptionneeds a network service.black --checkclean 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.