Skip to content
Closed
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
21 changes: 20 additions & 1 deletion backend/open_webui/utils/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,15 @@ def extract_attributes(tag_content):
attributes[key] = value
return attributes

def is_inside_fenced_code_block(text: str, index: int) -> bool:
"""Check if index is inside a fenced ``` code block."""
return text[:index].count("```") % 2 == 1

def is_tag_at_line_start(text: str, index: int) -> bool:
"""Return True if tag starts at line start (ignoring whitespace)."""
line_start = text.rfind("\n", 0, index) + 1
return text[line_start:index].strip() == ""

if content_blocks[-1]["type"] == "text":
for start_tag, end_tag in tags:

Expand All @@ -2547,7 +2556,17 @@ def extract_attributes(tag_content):
rf"<{re.escape(start_tag[1:-1])}(\s.*?)?>"
)

match = re.search(start_tag_pattern, content)
match = None
for candidate in re.finditer(start_tag_pattern, content):
if (
not is_inside_fenced_code_block(
content, candidate.start()
)
and is_tag_at_line_start(content, candidate.start())
):
match = candidate
break

if match:
try:
attr_content = (
Expand Down
Loading