Skip to content

fix: skip pruning inside <pre>/<code> in PruningContentFilter - #2111

Open
kinorai wants to merge 1 commit into
unclecode:developfrom
kinorai:fix/pruning-preserve-code-whitespace
Open

fix: skip pruning inside <pre>/<code> in PruningContentFilter#2111
kinorai wants to merge 1 commit into
unclecode:developfrom
kinorai:fix/pruning-preserve-code-whitespace

Conversation

@kinorai

@kinorai kinorai commented Jul 30, 2026

Copy link
Copy Markdown

Fixes #2110

What

PruningContentFilter._prune_tree now skips <pre>/<code> subtrees entirely — never scored, never pruned. This mirrors the scraper-side guard that #1181 added to WebScrapingStrategy.remove_empty_elements_fast in v0.7.8, which fixed raw_markdown but left fit_markdown unprotected.

Why

Syntax highlighters wrap every token in <span>, including whitespace-only spans (<span class="w"> </span>). Those score ~0.26–0.36 against the default 0.48 threshold (text_density 0, tag_weight span=0.3, log(1)=0) and get decomposed — import torch becomes importtorch, FROM golang:1.21 AS builder becomes FROMgolang:1.21ASbuilder in fit_markdown. Heavily-highlighted <pre> blocks (hundreds of spans → huge tag_len, near-zero text density) can be dropped wholesale, which removes all code from e.g. MDN pages. Details and real-URL repros in #2110.

Tests

tests/test_pruning_code_whitespace.py (4 tests, modeled on test_pruning_preserve_whitelist_1900.py):

  • whitespace-only spans inside code survive filtering
  • a heavily-highlighted <pre> block is not dropped
  • normal pruning outside code blocks still works (nav is still pruned)
  • rendered text keeps token spacing (import torch, FROM golang:1.21 AS builder)

Without the fix, the two whitespace tests fail with the exact corruption signature; with it, all 24 pruning-filter tests pass (including the full #1900 preserve-whitelist suite — no regressions).

preserve_tags=["pre","code"] (#1904) remains a valid explicit workaround for older 0.9.x versions; this PR makes the safe behavior the default.

🤖 Generated with Claude Code

Syntax highlighters wrap every token in <span>, including whitespace-only
spans (<span class="w"> </span>). PruningContentFilter scored those at
~0.26-0.36 against the default 0.48 threshold and decomposed them,
collapsing 'import torch' to 'importtorch' in fit_markdown — the same
failure unclecode#1181 fixed on the scraper path in v0.7.8. Heavily-highlighted
<pre> blocks (huge tag_len, near-zero text density) could also be dropped
wholesale.

Mirror the scraper-side guard: never score or prune <pre>/<code> subtrees.

Fixes unclecode#2110

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant