I wrote this function to capture and convert hash tags into links:
def link_caption_hashtags(caption) when is_binary(caption) do
Regex.replace(~r/#([[:alnum:]]+)/, caption, fn tag, word ->
link(tag, to: "#{@tag_explorer_uri}#{word}")
|> safe_to_string()
end)
|> raw()
end
If you pass it I am a caption with a #hashtag
, you get back I am a caption with a <a href="http://foo.com/hashtag">#hashtag</a>
.
However, it breaks down when passed a hashtag with diacritics (accents): #é
. It seems that safe_to_string()
converts accents into unknown chars, which are displayed as a diamond with a question mark. So it returns #�
.
Is this a bug? Is there a better way to do what I want?