- def add_user_links(text, mentions) do
- mentions = mentions
- |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
- |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
+ def add_attachments(text, attachments) do
+ attachment_text =
+ Enum.map(attachments, fn
+ %{"url" => [%{"href" => href} | _]} = attachment ->
+ name = attachment["name"] || URI.decode(Path.basename(href))
+ href = MediaProxy.url(href)
+ "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
+
+ _ ->
+ ""
+ end)
+
+ Enum.join([text | attachment_text], "<br>")
+ end
+
+ def format_input(text, mentions, tags, "text/plain") do
+ text
+ |> Formatter.html_escape("text/plain")
+ |> String.replace(~r/\r?\n/, "<br>")
+ |> (&{[], &1}).()
+ |> Formatter.add_links()
+ |> Formatter.add_user_links(mentions)
+ |> Formatter.add_hashtag_links(tags)
+ |> Formatter.finalize()
+ end
+
+ def format_input(text, mentions, tags, "text/html") do
+ text
+ |> Formatter.html_escape("text/html")
+ |> String.replace(~r/\r?\n/, "<br>")
+ |> (&{[], &1}).()
+ |> Formatter.add_user_links(mentions)
+ |> Formatter.finalize()
+ end
+
+ def format_input(text, mentions, tags, "text/markdown") do
+ text
+ |> Earmark.as_html!()
+ |> Formatter.html_escape("text/html")
+ |> String.replace(~r/\r?\n/, "")
+ |> (&{[], &1}).()
+ |> Formatter.add_user_links(mentions)
+ |> Formatter.add_hashtag_links(tags)
+ |> Formatter.finalize()
+ end