Have the LLM use square bracketed links for internal references

This commit is contained in:
Claudio Ortolina
2026-04-13 14:24:36 +01:00
parent 96bb0fc403
commit 6a6410bcaa
3 changed files with 23 additions and 10 deletions
@@ -24,6 +24,10 @@ defmodule MusicLibrary.Chats.CollectionChat do
Collection catalog: Collection catalog:
#{collection_summary} #{collection_summary}
# Mentioning artists/albums
**IF YOU MENTION AN ARTIST NAME OR ALBUM NAME, wrap it in "[[name]]", for example "[[Steven Wilson]]"
""") """)
end end
end end
+11 -3
View File
@@ -120,19 +120,27 @@ defmodule MusicLibraryWeb.Markdown do
end end
defp open_links_in_new_tab(doc, target) do defp open_links_in_new_tab(doc, target) do
MDEx.Document.update_nodes(doc, MDEx.Link, fn %MDEx.Link{url: url, title: title, nodes: nodes} -> MDEx.Document.update_nodes(doc, MDEx.Link, fn %MDEx.Link{url: url} = link ->
text = extract_text(nodes) if external_link?(url) do
text = extract_text(link.nodes)
title_attr = title_attr =
if title != "" and title != nil, do: ~s( title="#{html_escape(title)}"), else: "" if link.title != "" and link.title != nil,
do: ~s( title="#{html_escape(link.title)}"),
else: ""
%MDEx.HtmlInline{ %MDEx.HtmlInline{
literal: literal:
~s(<a href="#{html_escape(url)}" target="#{target}"#{title_attr}>#{html_escape(text)}</a>) ~s(<a href="#{html_escape(url)}" target="#{target}"#{title_attr}>#{html_escape(text)}</a>)
} }
else
link
end
end) end)
end end
defp external_link?(url), do: url =~ ~r"^https?://"
defp extract_text(nodes) when is_list(nodes), do: Enum.map_join(nodes, "", &extract_text/1) defp extract_text(nodes) when is_list(nodes), do: Enum.map_join(nodes, "", &extract_text/1)
defp extract_text(%{literal: literal}), do: literal defp extract_text(%{literal: literal}), do: literal
defp extract_text(%{nodes: nodes}), do: extract_text(nodes) defp extract_text(%{nodes: nodes}), do: extract_text(nodes)
+3 -2
View File
@@ -83,10 +83,11 @@ defmodule MusicLibraryWeb.MarkdownTest do
assert result =~ ~s(href="http://example.com") assert result =~ ~s(href="http://example.com")
end end
test "adds target to double bracket links" do test "does not add target to internal links" do
result = Markdown.to_html("Check [[Porcupine Tree]]", link_target: "_blank") result = Markdown.to_html("Check [[Porcupine Tree]]", link_target: "_blank")
assert result =~ ~s(target="_blank") refute result =~ ~s(target="_blank")
assert result =~ ~s(href="/collection?query=Porcupine+Tree")
assert result =~ "Porcupine Tree</a>" assert result =~ "Porcupine Tree</a>"
end end