Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crawl4ai/html2text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ def handle_data(self, data: str, entity_char: bool = False) -> None:
data = escape_md_section(
data,
snob=self.escape_snob,
escape_backslash=self.escape_backslash,
escape_dot=self.escape_dot,
escape_plus=self.escape_plus,
escape_dash=self.escape_dash,
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_html2text_escape_backslash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Unit tests for CustomHTML2Text's escape_backslash config flag.

escape_backslash is threaded from config.py -> HTML2Text.__init__ -> instance
attribute, and sibling flags (escape_dot/escape_plus/escape_dash) are all
forwarded into escape_md_section() at its call site in handle_data(). This
tests that escape_backslash is forwarded the same way, so that setting it to
False actually disables backslash escaping in the generated markdown.
"""
from crawl4ai.html2text import CustomHTML2Text


def _convert(html: str, escape_backslash: bool) -> str:
h = CustomHTML2Text()
h.body_width = 0
h.escape_backslash = escape_backslash
return h.handle(html)


# RE_MD_BACKSLASH_MATCHER (html2text/config.py) only fires on a backslash
# followed by a markdown-special char (`*_{}[]()#+-.!), so the repro text
# must contain a literal backslash immediately before one of those chars.
HTML_WITH_BACKSLASH = r"<p>Use \* for multiplication</p>"


class TestEscapeBackslashConfig:
def test_escape_backslash_false_leaves_backslashes_alone(self):
"""escape_backslash=False must not double the literal backslash."""
out = _convert(HTML_WITH_BACKSLASH, escape_backslash=False)
assert r"\*" in out
assert r"\\*" not in out

def test_escape_backslash_true_still_escapes(self):
"""escape_backslash=True (the escape_md_section default) must still escape."""
out = _convert(HTML_WITH_BACKSLASH, escape_backslash=True)
assert r"\\*" in out