dipth
Autolinking urls in string (replacing urls with link tags)
I’m having a bit of trouble wrapping my head around how to implement an autolinking feature in my Phoenix application. Something like rails_autolink from rails (GitHub - tenderlove/rails_autolink: The auto_link function from Rails · GitHub)
The idea is that I want to scan some user-content and replace all urls with link-tags.
Now, I could technically just make a regular expression and just use it with String.replace to replace the urls, but since I will be using this in combination with text_to_html to also split the text into paragraphs it becomes a little more complex, since text_to_html returns a {:safe, string} tuple. Furthermore when I start replacing the urls by using the Phoenix.HTML.Link.link it becomes a bit of a mess, since I end up with something like:
["<p>This is a test:\r<br>\n", {:safe, [60, "a", [[32, "class", 61, 34, "embed", 34], [32, "href", 61, 34, "https://www.youtube.com/watch?v=CmAI_MwdASw", 34]], 62, "https://www.youtube.com/watch?v=CmAI_MwdASw", 60, 47, "a", 62]} | "</p>\n"]
Which Phoenix seems to choke on:
ArgumentError at GET /forums/4/forum_topics/16
argument error
nofile
No code available.
:erlang.iolist_to_binary/1
Called with 1 arguments
["<p>This is a test:\r<br>\n", {:safe, [60, "a", [[32, "class", 61, 34, "embed", 34], [32, "href", 61, 34, "https://www.youtube.com/watch?v=CmAI_MwdASw", 34]], 62, "https://www.youtube.com/watch?v=CmAI_MwdASw", 60, 47, "a", 62]} | "</p>\n"]
I think I’m missing something here. What would be the “correct” way to accomplish this?
Most Liked
alexparker
I’m working on this as well, if I figure it out and remember to come back, I’ll add my solution here. Or if you figured it out, do update please.
voughtdq
Have you tried using Phoenix.HTML.safe_to_string/1 on the result of text_to_html?
MKoussa
For anyone who arrives here, this is what I found to work. Not perfect, but a starting point.
# only accepts https
@url_regex ~r/(https:\/\/[^\s<]+)/i
def linkify(text) when is_binary(text) do
text
# Escape ALL user content
|> html_escape()
# Get the escaped HTML string
|> safe_to_string()
# Inject safe <a> tags
|> replace_urls()
# Mark final output as safe
|> raw()
end
defp replace_urls(escaped_html) do
Regex.replace(@url_regex, escaped_html, fn url ->
~s(<a href="#{url}" target="_blank" rel="noopener noreferrer">#{url}</a>)
end)
end
And then in your HEEX:
<%= linkify(@content_to_linkify) %>
If anyone knows of a better way, I am open to improvements and suggestions!
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









