Conversation
escape_text_block_markdown_prefix() only guarded ATX headings and the +/- bullet markers, so a plain text paragraph that happens to start with ">" or with an ordered-list marker was silently promoted to a different block type in the rendered Markdown. The ordered-list case is common in extracted PDFs: a numbered reference such as "1986. A landmark year" renders as <ol start="1986">. For an ordered list the backslash goes before the delimiter rather than the marker start, because "\1." is not a Markdown escape while "1\." is.
Contributor
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
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.
Problem
escape_text_block_markdown_prefix()exists to stop an assembled plain-text paragraph from being reinterpreted as a different Markdown block. Today its regex only covers ATX headings and+/-bullets:r"^(?P<indent>[ \t]{0,3})(?P<marker>#{1,6}|[+-])(?=[ \t])"Two block markers of the same class are missing, so a
BlockType.TEXTparagraph that happens to start with one is silently promoted to a quote or a list. The three call sites arepipeline_middle_json_mkcontent.py:301,vlm_middle_json_mkcontent.py:357andoffice/mkcontent/inline_renderer.py:964— all guarded on plain text blocks, so real lists are unaffected.Rendering the current output through CommonMark (
markdown-it) on master79d6d8d:> 5 mV was measured at the gate.<blockquote><p>5 mV was measured at the gate.</p></blockquote>1. Smith, J. et al. Nature 2020.<ol><li>Smith, J. et al. Nature 2020.</li></ol>2) Second numbered reference.<ol start="2"><li>…</li></ol>1986. A landmark year for the field.<ol start="1986"><li>…</li></ol>The ordered-list case is the more damaging one in practice: numbered references and enumerations are everywhere in extracted PDFs, and the leading number is swallowed into the list marker, so it disappears from the text entirely.
Fix
Add the two missing markers to the same regex:
>— note CommonMark does not require a space after>, so this branch carries no(?=[ \t])lookahead, unlike the others.\d{1,9}followed by.or)— capped at nine digits, per CommonMark.One subtlety: the backslash cannot simply go at the marker start for ordered lists.
\1.is not a Markdown escape (backslash escapes only apply to ASCII punctuation), so the backslash would render literally. The escape has to be1\.. The fix therefore records which alternative matched and inserts the backslash in front of that group.After the change, all four rows above render as ordinary paragraphs with the leading text intact.
Verification
Added
tests/unittest/test_text_block_markdown_prefix.py(15 parametrized cases).markdown_utils.py: 6 failed, 9 passed — the 6 failures are exactly the new quote/ordered-list cases, and the 9 passes are the regression guards, confirming existing behaviour is untouched.The regression guards cover: existing
#/+/-handling, empty input, plain sentences,1.5 mm(no space after the delimiter, so not a list), a ten-digit run (over the CommonMark cap),>appearing mid-line, and four-space indentation (an indented code block, out of scope here).I kept this to the two missing block markers and deliberately did not touch setext underlines or
---thematic breaks — those are multi-line concerns that this block-prefix helper cannot see.No conflict with #5364: that PR changes
CONSERVATIVE_MARKDOWN_SPECIAL_CHARSand addstests/unittest/test_markdown_utils.py, while this one changesTEXT_BLOCK_MARKDOWN_PREFIX_REand adds a separate test file.🤖 Generated with Claude Code