Skip to content

fix(markdown): retain nested preserved HTML tags - #2351

Open
ktz03 wants to merge 1 commit into
unclecode:developfrom
ktz03:fix/nested-preserved-html-tags
Open

ktz03 wants to merge 1 commit into
unclecode:developfrom
ktz03:fix/nested-preserved-html-tags

Conversation

@ktz03

@ktz03 ktz03 commented Oct 9, 2026

Copy link
Copy Markdown

Summary

DefaultMarkdownGenerator removes the inner element and its attributes when both an outer and nested element match preserve_tags. Record every matching opening and closing tag so preserved HTML retains its nested structure, including nested tables.

List of files changed and why

  • crawl4ai/html2text/__init__.py: retain nested preserved tags while flushing only the completed outer block.
  • tests/unit/test_markdown_preserved_tags.py: cover matching tag names, mixed tag names, nested tables, three levels, ordinary children, and sibling blocks through the public Markdown generator.

How Has This Been Tested?

On Windows with Python 3.12.14:

python -m pytest tests/unit/test_markdown_preserved_tags.py tests/test_table_gfm_compliance.py tests/test_table_rowspan_grid.py tests/test_markdown_generator_validation_1880.py tests/general/test_async_markdown_generator.py tests/general/test_content_source_parameter.py -q
45 passed

The new regression suite fails four cases on the unchanged develop base, with its two controls passing. All six cases pass with the fix. An independent public-API check also passes 16 scenarios, covering nested structures and ordinary controls with citations enabled and disabled. Black checks pass for the new test file and the modified source lines; git diff --check passes. Browser, live crawling, and the full repository test suite were not run.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added/updated unit tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

No documentation change is needed for restoring the existing preserve_tags behavior. The selected local regression suites pass as detailed above; the full suite was not run.

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