fix(ipynb): read the notebook's own language and a string source - #2518
Open
Lukas (L4XB) wants to merge 1 commit into
Open
Lukas (L4XB) wants to merge 1 commit into
Lukas (L4XB) wants to merge 1 commit into
Conversation
Two shapes nbformat does not guarantee were assumed. Code cells were always fenced as ```python, whatever the notebook is written in, so an R or Julia notebook came out labelled as Python. The language is in the notebook: `metadata.language_info.name`, or `metadata.kernelspec.language` on older ones. A notebook that records neither keeps the previous default. `source` is an nbformat `multiline_string` -- a list of lines or a single string. Joining is the same either way, which is why this went unnoticed, but iterating is not: the title scan saw one character per step and never matched a `# ` heading, so a notebook whose source is a string got no title at all.
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.
What
IpynbConverterassumes two shapes that nbformat does not guarantee.1. Every code cell is fenced as Python.
An R notebook comes out as:
Over a string, that iterates one character at a time, and
"#".startswith("# ")is never true. Same notebook, two spellings of the same cell:How
_fence_language(notebook_content)—metadata.language_info.name, thenmetadata.kernelspec.language, then"python"as before. A value that is not a usable fence info string (empty, containing a space, containing a backtick) falls through to the default rather than producing a broken fence._source_lines(cell)— splits a stringsourceinto lines withsplitlines(keepends=True), passes a list through unchanged, and yields nothing for any other type. Joining is unaffected; the title scan now sees lines in both cases.Raw cells keep their untagged fence — only code cells carry a language.
Test
packages/markitdown/tests/test_ipynb_converter.py— 13 tests.maintests/test_ipynb_converter.pyThe nine that pass on both sides are the guards: a notebook with no language metadata still fences as
python, a listsourcerenders byte-identically,metadata.titlestill wins over a heading, raw cells stay untagged, and the three malformed-metadata cases fall back rather than emitting a broken fence.Full suite: 905 passed, 14 skipped.
black(the pinned 23.7.0 from.pre-commit-config.yaml) reports both files unchanged.