Skip to content

fix(md): preserve whitespace around inline emphasis in table cells - #4318

Merged
ceberam merged 2 commits into
docling-project:mainfrom
Anurag-M1:fix/md-table-cell-inline-whitespace
Sep 28, 2026
Merged

ceberam merged 2 commits into
docling-project:mainfrom
Anurag-M1:fix/md-table-cell-inline-whitespace

Conversation

@Anurag-M1

Copy link
Copy Markdown
Contributor

Summary

Resolves #4314.

When parsing Markdown tables in MarkdownDocumentBackend, table row text is accumulated across Markdown tokens in self.md_table_buffer. Previously, each token snippet was stripped with original_text.strip(), which inadvertently stripped leading and trailing whitespace from fragments adjacent to inline emphasis tokens (e.g. **C** Cadre -> cell text became CCadre).

This PR:

  1. Keeps whitespace intact during row buffer accumulation by using _unescape_except_pipe(original_text) instead of stripping each token snippet.
  2. Accumulates pure-whitespace tokens between inline formatting tags (e.g., **A** **B**).
  3. Relies on existing cell-level stripping performed by _split_table_row() so cell padding whitespace around | boundaries continues to be cleanly trimmed.
  4. Adds regression tests covering spaces before, after, and between inline emphasis in Markdown table cells.

Verification

  • Added test_convert_table_cell_whitespace_around_inline_emphasis in tests/test_backend_markdown.py.
  • Verified all 20 tests in tests/test_backend_markdown.py pass:
    uv run pytest tests/test_backend_markdown.py
  • Validated changeset with repository checks:
    make validate

@github-actions

github-actions Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @Anurag-M1, all your commits are properly signed off. 🎉

@Anurag-M1
Anurag-M1 force-pushed the fix/md-table-cell-inline-whitespace branch from 8a9bea0 to 9b29683 Compare September 19, 2026 12:21
@mergify

mergify Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

🟢 Require two reviewer for test updates

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

When parsing Markdown tables, table row text is accumulated in
md_table_buffer across Markdown AST tokens. Previously, each snippet
was stripped with .strip(), which caused spaces following or preceding
inline emphasis tokens (like '**C** Cadre') to be dropped, resulting in
'CCadre' and similar squashed text.

Avoid stripping snippet text while accumulating table buffer lines,
relying on cell trimming during _split_table_row instead.

Resolves docling-project#4314

Signed-off-by: Anurag Singh <[email protected]>
@Anurag-M1
Anurag-M1 force-pushed the fix/md-table-cell-inline-whitespace branch from 9b29683 to 77f04c4 Compare September 23, 2026 15:49
@ceberam ceberam added bug Something isn't working markdown issue related to markdown backend labels Sep 24, 2026

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Anurag-M1 , the fix is minimal and it targets the right issue.

I added some minor comments. In addition, could you please add the example in a real .md file? We collect these cases in source data files that we used for testing the conversion and serialization in some formats.
The test file tests/data/md/sources/inline_and_formatting.md already has a table at the end with some formatted cells, but not the use case you fixed. Appending your example at the end of that file would be the right choice. Keep in mind that you will need to regenerate the ground-truth files.

Comment thread docling/backend/md_backend.py Resolved
Comment thread tests/test_backend_markdown.py Resolved
- Check original_text directly in MarkdownDocumentBackend table element handling.
- Use Google-style docstring in test_convert_table_cell_whitespace_around_inline_emphasis and remove issue reference.
- Add inline emphasis table cell test cases to tests/data/md/sources/inline_and_formatting.md.
- Regenerate ground-truth test files (inline_and_formatting.md.md and inline_and_formatting.md.yaml).

Signed-off-by: Anurag Singh <[email protected]>

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good! 🚀

@Anurag-M1

Copy link
Copy Markdown
Contributor Author

Thanks for the review @ceberam! I've addressed all the feedback in the latest commit:

  1. Simplified table condition: Updated docling/backend/md_backend.py to check if self.in_table and original_text: directly.
  2. Google-style docstring: Updated the test function docstring in tests/test_backend_markdown.py to Google style and removed the issue reference.
  3. Source test data & ground-truth: Appended the inline emphasis table test cases to the table at the end of tests/data/md/sources/inline_and_formatting.md and regenerated the ground-truth files (inline_and_formatting.md.md and inline_and_formatting.md.yaml).

All validation checks (make validate) and unit tests pass.

@ceberam
ceberam merged commit d453bb0 into docling-project:main Sep 28, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working markdown issue related to markdown backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown backend drops the space after inline emphasis in table cells (**C** Cadre → CCadre)

3 participants