From 956c7bfc3cdcac09b5668d0e86685ef8cbadd6ea Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Wed, 25 Feb 2026 09:51:08 +0000 Subject: [PATCH] Support search-style prefixes in bracketed links --- lib/music_library_web/markdown.ex | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/music_library_web/markdown.ex b/lib/music_library_web/markdown.ex index 1c91b976..4c280662 100644 --- a/lib/music_library_web/markdown.ex +++ b/lib/music_library_web/markdown.ex @@ -5,6 +5,7 @@ defmodule MusicLibraryWeb.Markdown do Text wrapped in double square brackets like `[[Foo]]` will be rendered as search links to the collection page. """ + alias MusicLibrary.Records @doc """ Converts markdown with custom `[[link]]` syntax to HTML. @@ -22,21 +23,37 @@ defmodule MusicLibraryWeb.Markdown do @doc """ Processes text to convert [[text]] patterns into markdown links. + Supports prefixed queries like `[[artist:Blackfield]]` where the link text + shows only the value (e.g. "Blackfield") but the search query uses the full + prefixed content. + ## Examples iex> MusicLibraryWeb.Markdown.process_double_bracket_links("Check out [[Porcupine Tree]]") "Check out [Porcupine Tree](/collection?query=Porcupine+Tree)" - + iex> MusicLibraryWeb.Markdown.process_double_bracket_links("Albums like [[Steven Wilson - The Raven That Refused to Sing (and Other Stories) (2013)]] are great") "Albums like [Steven Wilson - The Raven That Refused to Sing (and Other Stories) (2013)](/collection?query=Steven+Wilson+-+The+Raven+That+Refused+to+Sing+%28and+Other+Stories%29+%282013%29) are great" + + iex> MusicLibraryWeb.Markdown.process_double_bracket_links("Listen to [[artist:Blackfield]]") + "Listen to [Blackfield](/collection?query=artist%3ABlackfield)" + + iex> MusicLibraryWeb.Markdown.process_double_bracket_links(~s|[[genre:"psychedelic rock"]]|) + ~s|[psychedelic rock](/collection?query=genre%3A%22psychedelic+rock%22)| """ def process_double_bracket_links(text) do - # Regex to match [[text]] patterns ~r/\[\[([^\]]+)\]\]/ |> Regex.replace(text, fn _match, content -> - # URL encode the content for the search query encoded_content = URI.encode_www_form(content) - "[#{content}](/collection?query=#{encoded_content})" + "[#{display_text(content)}](/collection?query=#{encoded_content})" end) end + + defp display_text(content) do + case Records.SearchParser.parse(content) do + {:ok, %{query: _}} -> content + {:ok, parsed} when map_size(parsed) == 1 -> parsed |> Map.values() |> hd() |> to_string() + _ -> content + end + end end