diff --git a/bot.py b/bot.py
index 0d45735..aacc8b6 100644
--- a/bot.py
+++ b/bot.py
@@ -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"\1", safe)
# Italic
safe = re.sub(r"\*(.+?)\*", r"\1", 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'{label}'
+ safe = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", _link_repl, safe)
+ # Bare URLs (not already in an tag)
+ safe = re.sub(r'(?)(https?://[^\s<]+)', r'\1', safe)
# Line breaks
safe = safe.replace("\n", "
")
return safe