fix(ipynb): use a dynamic code fence so cells containing backticks don't leak#2170
Open
YellowFoxH4XOR wants to merge 1 commit into
Open
fix(ipynb): use a dynamic code fence so cells containing backticks don't leak#2170YellowFoxH4XOR wants to merge 1 commit into
YellowFoxH4XOR wants to merge 1 commit into
Conversation
Author
|
@microsoft-github-policy-service agree |
…t leak Code and raw notebook cells were always wrapped in a 3-backtick fence without inspecting the cell content. A cell whose source itself contains a ``` line (common in notebooks that demo Markdown or print fenced strings) closed the fence early, leaking the rest of the cell out as prose and corrupting the document structure. Wrap cells with a fence longer than the longest backtick run in the content, per CommonMark. Adds a regression test.
0cd5bc7 to
6971e65
Compare
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.
Summary
IpynbConverterwraps every code/raw cell in a hard-coded 3-backtick fence(
```python … ```) without inspecting the cell's contents. When a cell'ssource itself contains a
```line — common in notebooks that demo Markdown,print fenced strings, or embed heredocs — the inner backticks close the fence
early. The rest of the cell then leaks out of the code block and renders as
prose, corrupting the document.
This PR sizes the fence to the content: it uses a run of backticks one longer
than the longest backtick run inside the cell, per the
CommonMark fenced-code-block rules.
Cells with no backticks are unaffected (still 3 backticks), so output is
unchanged for the common case.
Repro
Before — the inner
```closes the block; the code leaks out as prose:After — a 4-backtick fence keeps the cell intact:
Context
This is the same defect jupytext fixed in #712
and quarto in #3179 — the
"longer fence than content" approach is the standard, ecosystem-wide fix.
Note: the fence length counts all backtick runs, including
inline ones that cannot actually close a fence. This is a safe
over-approximation — it occasionally yields a fence one backtick longer than
strictly minimal, which is always valid CommonMark, and in exchange it also
covers indented closing fences (which a line-prefix check would miss).
Testing
test_ipynb_code_cell_with_backtick_fence— fails onmain, passes with this change.missing optional dependencies: docx, llm, speech, exiftool — identical on baseline).