Skip to content

fix(filter): skip pruning nodes inside pre/code blocks (#2110) - #2191

Open
Battleplus wants to merge 1 commit into
unclecode:mainfrom
Battleplus:fix/2110-pruning-whitespace-pre-code
Open

fix(filter): skip pruning nodes inside pre/code blocks (#2110)#2191
Battleplus wants to merge 1 commit into
unclecode:mainfrom
Battleplus:fix/2110-pruning-whitespace-pre-code

Conversation

@Battleplus

Copy link
Copy Markdown

Summary

Fixes #2110 — PruningContentFilter was decomposing whitespace-only spans inside pre/code elements, corrupting code that raw_markdown preserves.

Root cause

_prune_tree() in PruningContentFilter has no guard for code blocks. It scores every node and decomposes those below threshold, including whitespace-significant spans inside pre/code. The equivalent function in content_scraping_strategy.remove_empty_elements_fast already has this guard since v0.7.8 (#1181).

Fix

Add _is_in_code_block() helper that walks .parents checking for pre or code ancestors. _prune_tree() returns early for these nodes, matching the existing guard in remove_empty_elements_fast.

Files changed

  • crawl4ai/content_filter_strategy.py (+5 lines)

PruningContentFilter was decomposing whitespace-only spans inside
<pre>/<code> elements (e.g., <span class=\"w\"> </span> in syntax
highlighting), corrupting code that raw_markdown preserves.

Add _is_in_code_block() guard mirroring the existing fix in
content_scraping_strategy.remove_empty_elements_fast (v0.7.8, unclecode#1181).

Closes unclecode#2110
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.

PruningContentFilter drops whitespace-only spans in <pre>/<code> — #1181's bug, still present on the fit_markdown path

1 participant