@@ -510,6 +510,7 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
510
510
511
511
if tag == "a" and not self .ignore_links :
512
512
if start :
513
+ self .inside_link = True
513
514
if (
514
515
"href" in attrs
515
516
and attrs ["href" ] is not None
@@ -526,6 +527,7 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
526
527
else :
527
528
self .astack .append (None )
528
529
else :
530
+ self .inside_link = False
529
531
if self .astack :
530
532
a = self .astack .pop ()
531
533
if self .maybe_automatic_link and not self .empty_link :
@@ -1035,6 +1037,7 @@ def __init__(self, *args, handle_code_in_pre=False, **kwargs):
1035
1037
super ().__init__ (* args , ** kwargs )
1036
1038
self .inside_pre = False
1037
1039
self .inside_code = False
1040
+ self .inside_link = False
1038
1041
self .preserve_tags = set () # Set of tags to preserve
1039
1042
self .current_preserved_tag = None
1040
1043
self .preserved_content = []
@@ -1114,11 +1117,17 @@ def handle_tag(self, tag, attrs, start):
1114
1117
# Ignore code tags inside pre blocks if handle_code_in_pre is False
1115
1118
return
1116
1119
if start :
1117
- self .o ("`" ) # Markdown inline code start
1120
+ if not self .inside_link :
1121
+ self .o ("`" ) # Only output backtick if not inside a link
1118
1122
self .inside_code = True
1119
1123
else :
1120
- self .o ("`" ) # Markdown inline code end
1124
+ if not self .inside_link :
1125
+ self .o ("`" ) # Only output backtick if not inside a link
1121
1126
self .inside_code = False
1127
+
1128
+ # If inside a link, let the parent class handle the content
1129
+ if self .inside_link :
1130
+ super ().handle_tag (tag , attrs , start )
1122
1131
else :
1123
1132
super ().handle_tag (tag , attrs , start )
1124
1133
0 commit comments