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:
12
bot.py
12
bot.py
@@ -122,7 +122,8 @@ class DocumentRAG:
|
|||||||
parts.append("") # blank line between docs
|
parts.append("") # blank line between docs
|
||||||
|
|
||||||
parts.append("Use the document content above to answer the user's question. "
|
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)
|
return "\n".join(parts)
|
||||||
|
|
||||||
|
|
||||||
@@ -581,6 +582,15 @@ class Bot:
|
|||||||
safe = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", safe)
|
safe = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", safe)
|
||||||
# Italic
|
# Italic
|
||||||
safe = re.sub(r"\*(.+?)\*", r"<em>\1</em>", safe)
|
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
|
# Line breaks
|
||||||
safe = safe.replace("\n", "<br/>")
|
safe = safe.replace("\n", "<br/>")
|
||||||
return safe
|
return safe
|
||||||
|
|||||||
Reference in New Issue
Block a user