Skip to content

Preserve words split by partial highlights in Markdown - #506

Open
Christiansada wants to merge 1 commit into
pymupdf:mainfrom
Christiansada:fix/505-highlight-span-spacing
Open

Christiansada wants to merge 1 commit into
pymupdf:mainfrom
Christiansada:fix/505-highlight-span-spacing

Conversation

@Christiansada

Copy link
Copy Markdown

Fixes #505.

A partial highlight makes MuPDF split a word into touching spans. Both Markdown paths appended a space between those spans, turning <mark>Highlig</mark>hted into <mark>Highlig</mark> hted.

This uses shared separator logic for highlight transitions: retain real source whitespace, geometric gaps and original line/block boundaries; suppress the separator only for touching fragments (within 0.001 point). The layout serializer keeps its existing style-merging and hyphenation behaviour. The legacy serializer uses the same rule, including its plain header/code-line text. Other style transitions retain their existing spacing.

Tests generate the report’s PDF in memory, covering layout and legacy output, a following word, header output, whitespace, geometric gaps, source line/block boundaries, floating-point roundoff and unchanged same-style behaviour. No uploaded PDF or client material is added.

Validation:

  • All four generated-PDF regression cases failed on unchanged main helpers at 1200f2a; they pass with the patch.
  • The surrounding pre-existing document/API selection passed 25 tests on the base. After narrowing the change to highlight transitions, its reference Markdown remains unchanged.
  • python -m pytest --noconftest tests/test_505.py tests/test_chunking_*.py tests/test_table_html.py tests/test_370.py tests/test_137.py -q: 108 passed, 3 skipped, with SWIG deprecation warnings.
  • New-test Ruff checks, source compilation and git diff --check passed.

Local environment: Python 3.12.14, macOS arm64, released PyMuPDF/PyMuPDF4LLM 2.1 runtime. The repository’s tracked src was overlaid into an isolated virtual environment; no system package was changed. --noconftest avoids the test bootstrap installing unrelated optional packages. I did not run the private Aptest build/CI, a full MuPDF source build, broad OCR testing or other platforms. The fix targets the reported horizontal highlight transition, not a general reconstruction of word boundaries from arbitrary PDF geometry.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_markdown inserts a space inside a word when a highlight covers only part of it

1 participant