From 6a6410bcaad7afab1cf2efdd6b340b7b1b9b0fd4 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 13 Apr 2026 14:24:36 +0100 Subject: [PATCH] Have the LLM use square bracketed links for internal references --- lib/music_library/chats/collection_chat.ex | 4 ++++ lib/music_library_web/markdown.ex | 24 ++++++++++++++-------- test/music_library_web/markdown_test.exs | 5 +++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/music_library/chats/collection_chat.ex b/lib/music_library/chats/collection_chat.ex index efc3de77..81a3e6df 100644 --- a/lib/music_library/chats/collection_chat.ex +++ b/lib/music_library/chats/collection_chat.ex @@ -24,6 +24,10 @@ defmodule MusicLibrary.Chats.CollectionChat do Collection catalog: #{collection_summary} + + # Mentioning artists/albums + + **IF YOU MENTION AN ARTIST NAME OR ALBUM NAME, wrap it in "[[name]]", for example "[[Steven Wilson]]" """) end end diff --git a/lib/music_library_web/markdown.ex b/lib/music_library_web/markdown.ex index f62e0447..4e9e45a8 100644 --- a/lib/music_library_web/markdown.ex +++ b/lib/music_library_web/markdown.ex @@ -120,19 +120,27 @@ defmodule MusicLibraryWeb.Markdown do end 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} -> - text = extract_text(nodes) + MDEx.Document.update_nodes(doc, MDEx.Link, fn %MDEx.Link{url: url} = link -> + if external_link?(url) do + text = extract_text(link.nodes) - title_attr = - if title != "" and title != nil, do: ~s( title="#{html_escape(title)}"), else: "" + title_attr = + if link.title != "" and link.title != nil, + do: ~s( title="#{html_escape(link.title)}"), + else: "" - %MDEx.HtmlInline{ - literal: - ~s(#{html_escape(text)}) - } + %MDEx.HtmlInline{ + literal: + ~s(#{html_escape(text)}) + } + else + link + 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(%{literal: literal}), do: literal defp extract_text(%{nodes: nodes}), do: extract_text(nodes) diff --git a/test/music_library_web/markdown_test.exs b/test/music_library_web/markdown_test.exs index ef699dbc..8ab450a3 100644 --- a/test/music_library_web/markdown_test.exs +++ b/test/music_library_web/markdown_test.exs @@ -83,10 +83,11 @@ defmodule MusicLibraryWeb.MarkdownTest do assert result =~ ~s(href="http://example.com") 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") - assert result =~ ~s(target="_blank") + refute result =~ ~s(target="_blank") + assert result =~ ~s(href="/collection?query=Porcupine+Tree") assert result =~ "Porcupine Tree" end