Skip to content

fix(markdown): escape leading block-quote and ordered-list markers in text blocks - #5376

Open
Anai-Guo wants to merge 1 commit into
opendatalab:masterfrom
Anai-Guo:fix/text-block-quote-and-ordered-list-prefix
Open

Anai-Guo wants to merge 1 commit into
opendatalab:masterfrom
Anai-Guo:fix/text-block-quote-and-ordered-list-prefix

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Aug 9, 2026

Copy link
Copy Markdown

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.TEXT paragraph that happens to start with one is silently promoted to a quote or a list. The three call sites are pipeline_middle_json_mkcontent.py:301, vlm_middle_json_mkcontent.py:357 and office/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 master 79d6d8d:

paragraph text renders today as
> 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:

  • Block quote > — note CommonMark does not require a space after >, so this branch carries no (?=[ \t]) lookahead, unlike the others.
  • Ordered list \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 be 1\.. 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).

  • Against this branch: 15 passed.
  • Against master's 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_CHARS and adds tests/unittest/test_markdown_utils.py, while this one changes TEXT_BLOCK_MARKDOWN_PREFIX_RE and adds a separate test file.

🤖 Generated with Claude Code

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.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant