fix: render markdown links as clickable HTML in Matrix messages

_md_to_html now converts [text](url) to <a> tags and auto-links bare URLs.
Also instructs LLM to use markdown links instead of raw URLs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Christian Gick
2026-02-16 13:51:22 +02:00
parent 84a7150c87
commit 61d3e524e9

12
bot.py
View File

@@ -122,7 +122,8 @@ class DocumentRAG:
parts.append("") # blank line between docs
parts.append("Use the document content above to answer the user's question. "
"Always include document links when referencing documents.")
"When referencing documents, use markdown links: [Document Title](url). "
"Never show raw URLs.")
return "\n".join(parts)
@@ -581,6 +582,15 @@ class Bot:
safe = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", safe)
# Italic
safe = re.sub(r"\*(.+?)\*", r"<em>\1</em>", safe)
# Markdown links [text](url) — must unescape the URL parts first
def _link_repl(m):
import html as _h
label = m.group(1)
url = _h.unescape(m.group(2))
return f'<a href="{url}">{label}</a>'
safe = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", _link_repl, safe)
# Bare URLs (not already in an <a> tag)
safe = re.sub(r'(?<!href=")(?<!">)(https?://[^\s<]+)', r'<a href="\1">\1</a>', safe)
# Line breaks
safe = safe.replace("\n", "<br/>")
return safe