Skip to content

fix(web_source): preserve document order and handle comments in <pre> code extraction - #3299

Open
Sinfinal wants to merge 1 commit into
HKUDS:devfrom
Sinfinal:fix/web-source-pre-code-extraction
Open

Sinfinal wants to merge 1 commit into
HKUDS:devfrom
Sinfinal:fix/web-source-pre-code-extraction

Conversation

@Sinfinal

@Sinfinal Sinfinal commented Dec 6, 2026

Copy link
Copy Markdown

Problem

_pre_to_text() and <pre> language detection in deeptutor/services/web_source/html_extractor.py had four defects affecting web-source KB sync, web_fetch, and Immersive Reading web ingestion:

  1. Duplicate <pre> text and smuggled trailing prose: el.iter() yields el itself first, so el.text was appended twice (doubling every plain <pre> block) and el.tail (text after </pre>) was included inside the fenced code block.
  2. Scrambled nested syntax-highlight spans: Walking el.iter() flat appended node.tail before visiting node's children, reordering code inside nested Highlight.js / Prism <span> elements (e.g., turning def foo():\n return 1 into return 1def foo():).
  3. Dropped text after HTML comments: if not tag: continue skipped node.tail on HTML comments inside <pre>.
  4. TypeError crash on HTML comments inside <pre>: c.get("class", "") returns None on lxml HtmlComment nodes, causing " ".join(...) to raise TypeError: sequence item 0: expected str instance, NoneType found.

Fix

  • Replace the flat el.iter() loop in _pre_to_text() with a document-order recursive child walk (_walk_pre) that never reads el.tail, never duplicates el.text, skips _STRIP_TAGS, converts <br> to \n, and appends each child.tail after descending into child.
  • Guard c.get("class", "") or "" when collecting child class names for language detection.

Verification

  • Added 3 regression tests in tests/services/web_source/test_markdown.py covering plain <pre> + tail text, nested Highlight.js <span> ordering, and HTML comments + <br/> inside <pre> (all 3 failed before the fix, all pass after).
  • Ran python -m pytest -q tests/services/web_source/ (24 passed + async test verified via asyncio.run).
  • Ran ruff check and ruff format --check on both modified files: all checks passed.

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

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