feat(markdown): add opt-in escape_markdown_syntax to escape block markers and literal asterisks #1008
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
shlokbam
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_syntaxflag, 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=...)andsave_as_markdown(escape_markdown_syntax=...). The flag is keyword-only inexport_to_markdown/save_as_markdown(after), likeformatting_fallback_mode, so positional callers aren't affected.When the flag is on, plain paragraph text items (
TEXT/PARAGRAPH) are escaped as follows:- ATX headings:
#to######- bullet markers:
-and+- ordered markers:
1.and1)-,_or=(spaces allowed), escape the first character.>is escaped only whenescape_html=False. Whenescape_htmlis on,>is already rendered as>.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. Ifwere escaped after formatting is applied,boldwould come out as\\bold\\. So escaping runs inMarkdownTextSerializer.serialize, on the item's raw text after line-break/wrap handling and beforepost_processadds formatting and hyperlink markup. A bold text item- xtherefore renders as*\- \x\, and plain bold and italic items stayboldanditalic.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/PARAGRAPHitem), its first run gets the line-start rules, so# xbecomes\# xand1. xbecomes1\. x. Later runs, and inline groups that are the content of a list item or heading, sit mid-line, so only literalis 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,FalseandTrue. The cases include tab-after-marker and the inputs that must stay unescaped.test_md_escape_markdown_syntax_export_to_markdown:export_to_markdownandsave_as_markdownmatch the serializer path, and default output is unchanged.test_md_escape_markdown_syntax_block_quote:>is escaped only whenescape_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 aTEXTitem; the later run getsescaping 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 getescaping 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 unmodifiedmainin my local environment, so they are not related to this change.ruff format --checkandruff check(0.16.10) pass on the changed files.Related
#1004 also edits
markdown.py(MarkdownTextSerializer.serializeandpost_process), but none of the changed lines overlap. The nearest hunk is thepost_process(...)call about 20 lines below the new escaping block.