← Back to Pull requests

feat(markdown): add opt-in escape_markdown_syntax to escape block markers and literal asterisks #1008

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 11, 2026 05:11 UTC
Author

Part of #61

This covers line-start block markers and literal . Other characters from the issue's list can follow in a separate PR.

What this adds

A new MarkdownParams.escape_markdown_syntax flag, off by default. With the flag off, output is byte-for-byte identical to today, and no existing output or golden files change.

The flag is also available as export_to_markdown(escape_markdown_syntax=...) and save_as_markdown(escape_markdown_syntax=...). The flag is keyword-only in export_to_markdown/save_as_markdown (after ), like formatting_fallback_mode, so positional callers aren't affected.

When the flag is on, plain paragraph text items (TEXT / PARAGRAPH) are escaped as follows:

  • Start of each line: escape these if followed by a space, a tab or the end of the line:

  • - ATX headings: # to ######
    - bullet markers: - and +
    - ordered markers: 1. and 1)
  • Thematic breaks and setext underlines: in a line made only of -, _ or = (spaces allowed), escape the first character.

  • Block quotes: > is escaped only when escape_html=False. When escape_html is on, > is already rendered as >.

  • Indented code: 4+ leading spaces are removed, because CommonMark can't backslash-escape spaces.

  • Inline: every literal becomes \.
  • The flag does not affect section headers, titles, list items (including docling's own list markers), checkboxes, code, formulas, tables or captions.

    Before / after

    | Input text | Default (unchanged) | escape_markdown_syntax=True |
    |---|---|---|
    | # not a heading | # not a heading | \# not a heading |
    | - not a list | - not a list | \- not a list |
    | + plus | + plus | \+ plus |
    | -<TAB>x | -<TAB>x | \-<TAB>x |
    | 1. not a list | 1. not a list | 1\. not a list |
    | 1) item | 1) item | 1\) item |
    | 1.<TAB>x | 1.<TAB>x | 1\.<TAB>x |
    | > quote (escape_html=False) | > quote | \> quote |
    | --- | --- | \--- |
    | | | \\\ |
    | indented four | indented four | indented four |
    | Use
    stars and _under_ literally | Use stars and \_under\_ literally | Use \stars\ and \_under\_ literally |
    | Title\n=== | Title \n=== | Title \n\=== |
    | 5
    3 = 15 | 5 3 = 15 | 5 \ 3 = 15 |
    | plain sentence | unchanged | unchanged |
    | #hashtag, -5 degrees, 2026-11-11, a - b | unchanged | unchanged |

    Why the escaping runs on raw text

    Docling uses for its own bold and italic. If were escaped after formatting is applied, bold would come out as \\bold\\. So escaping runs in MarkdownTextSerializer.serialize, on the item's raw text after line-break/wrap handling and before post_process adds formatting and hyperlink markup. A bold text item - x therefore renders as *\- \x\, and plain bold and italic items stay bold and italic.

    Known limitation

    Inline groups are handled for their first run: when an inline group starts a line (it is its own block, or it is the content of a TEXT/PARAGRAPH item), its first run gets the line-start rules, so # x becomes \# x and 1. x becomes 1\. x. Later runs, and inline groups that are the content of a list item or heading, sit mid-line, so only literal is escaped there.

    The only remaining gap is a later run that contains a line break followed by a block marker (e.g. a run "\n# x"): the line after the break is not escaped. This can be handled in a follow-up.

    Tests

    New tests in tests/test_serialization.py:

  • test_md_escape_markdown_syntax (18 cases): each case is checked with the flag unset, False and True. The cases include tab-after-marker and the inputs that must stay unescaped.

  • test_md_escape_markdown_syntax_export_to_markdown: export_to_markdown and save_as_markdown match the serializer path, and default output is unchanged.

  • test_md_escape_markdown_syntax_block_quote: > is escaped only when escape_html=False.

  • test_md_escape_markdown_syntax_keeps_own_markup: bold and italic formatting, section headers, list items, code blocks and inline groups keep docling's own markup.

  • test_md_escape_markdown_syntax_inline_group_first_run (6 cases): the first run of an inline group that starts a line gets the line-start rules (#, 1., -), both for a group that is its own block and for a group that is the content of a TEXT item; the later run gets escaping only, and default output is unchanged.

  • test_md_escape_markdown_syntax_inline_group_mid_line_runs: inline runs after a list marker, a heading marker or another run get escaping only; a nested group that opens its parent group still starts the line.
  • Results:

  • tests/test_serialization.py: 255 passed (including the 7 new inline-group cases), 1 xfailed. No golden files changed.

  • tests/test_docling_doc.py: 123 passed, 3 failed. The 3 failures (test_save_to_disk, test_document_manipulation, test_concatenate) also fail on unmodified main in my local environment, so they are not related to this change.

  • ruff format --check and ruff check (0.16.10) pass on the changed files.

  • I also ran a one-off local check with markdown-it-py (CommonMark preset, not committed). Every escaped output above parses as a single paragraph.
  • Related

    #1004 also edits markdown.py (MarkdownTextSerializer.serialize and post_process), but none of the changed lines overlap. The nearest hunk is the post_process(...) call about 20 lines below the new escaping block.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: shlokbam Review duration: 28206m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.52) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1008"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.7% · Safe to merge
    29.7%