Skip to content

fix(co_writer): keep run-edge whitespace outside emphasis markers in DOCX import - #3273

Open
XAE-A-Xii wants to merge 1 commit into
HKUDS:devfrom
XAE-A-Xii:fix/docx-emphasis-whitespace
Open

XAE-A-Xii wants to merge 1 commit into
HKUDS:devfrom
XAE-A-Xii:fix/docx-emphasis-whitespace

Conversation

@XAE-A-Xii

Copy link
Copy Markdown

Description

Importing a Word document produced emphasis that Markdown doesn't render. _run_to_markdown (deeptutor/co_writer/docx_converter.py) wrapped each formatted run as is, and Word routinely includes the trailing space in a bold or italic run. A document built with python-docx (bold "Hello " + "world", and so on) imported as:

**Hello **world
*Note: *x
a*** both ***b
gap**  **here
old ~~price ~~new

CommonMark doesn't close **, * or ~~ after a space. Checked with markdown-it-py (commonmark + strikethrough):

'**Hello **world'   -> **Hello **world                   (literal asterisks)
'**Hello** world'   -> <strong>Hello</strong> world
'old ~~price ~~new' -> old ~~price ~~new
'old ~~price~~ new' -> old <s>price</s> new

The markers now wrap only the run's non-space core, and the edge whitespace goes back outside: **Hello** world, *Note:* x, a ***both*** b, old ~~price~~ new. A formatted run that is only spaces is emitted as plain spaces (gap here). Unformatted runs come out byte-identical, and code-font runs are untouched (inside backticks the spaces are significant).

Related Issues

  • None filed; this is a small, self-contained fix.

Module(s) Affected

  • agents
  • api
  • config
  • core
  • knowledge
  • logging
  • services
  • tools
  • utils
  • web (Frontend)
  • docs (Documentation)
  • scripts
  • tests
  • Other: co_writer

Checklist

  • This PR targets dev (not main — see the base-branch selector above).
  • I have read and followed the contribution guidelines.
  • My code follows the project's coding standards.
  • I have run pre-commit run --all-files and fixed any issues.
  • I have added relevant tests for my changes.
  • I have updated the documentation (if necessary). (Not needed.)
  • My changes do not introduce any new security vulnerabilities.

Additional Notes

tests/co_writer/test_docx_import_emphasis_whitespace.py builds real .docx files with python-docx and runs docx_to_markdown. The existing DOCX emphasis tests only cover export. The cases are trailing space (bold, italic, bold-italic, strikethrough), leading space, a whitespace-only bold run, and a plain bold word as a regression guard.

Before the fix, 6 of the 7 cases fail; only the plain bold word passes. Mutations are caught too: dropping the whitespace-only guard fails 1 case, and putting the leading space after the core fails 2.

$ python -m pytest tests/co_writer tests/api/test_co_writer.py tests/api/routers/test_co_writer_contract.py -q
118 passed in 3.25s

pre-commit run --files on both changed files passes every hook (ruff, mypy, bandit, detect-secrets). Note that pre-commit run --all-files currently fails mypy on dev itself in deeptutor/services/embedding/adapters/volcengine.py, from #1015, which this PR doesn't touch.

🤖 Generated with Claude Code

@github-actions github-actions Bot added backend Python runtime, capabilities, tools, API & CLI bug Something isn't working labels Dec 5, 2026
…DOCX import

`_run_to_markdown` wrapped a formatted Word run as is, so a bold run
"Hello " followed by "world" imported as `**Hello **world`. CommonMark
does not close `**` after a space, so the asterisks showed as literal
text. Word bolds a word together with its trailing space all the time,
and the same broke italic, bold-italic and strikethrough runs; a
formatted run of only spaces became `**  **`.

The markers now wrap only the run's non-space core and the edge
whitespace goes back outside (`**Hello** world`); a whitespace-only run
is emitted as plain spaces. Unformatted and code-font runs are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@XAE-A-Xii
XAE-A-Xii force-pushed the fix/docx-emphasis-whitespace branch from a892891 to efd9072 Compare December 5, 2026 12:15

This branch has not been deployed

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

Labels

backend Python runtime, capabilities, tools, API & CLI bug Something isn't working

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant