From 33c6754592063c94b4e8598675f1f2d46fc9a49e Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Mon, 18 Feb 2019 18:45:29 +0700 Subject: [PATCH] fix parser --- lib/auto_linker/builder.ex | 3 +-- lib/auto_linker/parser.ex | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/auto_linker/builder.ex b/lib/auto_linker/builder.ex index be0ed26..a46ccf4 100644 --- a/lib/auto_linker/builder.ex +++ b/lib/auto_linker/builder.ex @@ -117,11 +117,10 @@ defmodule AutoLinker.Builder do url = mention_prefix <> name - [] + [href: url] |> build_attrs(url, opts, :rel) |> build_attrs(url, opts, :target) |> build_attrs(url, opts, :class) - |> build_attrs(url, opts, :scheme) |> format_mention(name, opts) end diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index 7374df0..14cb785 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -378,23 +378,43 @@ defmodule AutoLinker.Parser do def link_hashtag(nil, buffer, _, _user_acc), do: buffer def link_hashtag(hashtag, buffer, %{hashtag_handler: hashtag_handler} = opts, user_acc) do - hashtag_handler.(hashtag, buffer, opts, user_acc) + hashtag + |> hashtag_handler.(buffer, opts, user_acc) + |> maybe_update_buffer(hashtag, buffer) end def link_hashtag(hashtag, buffer, opts, _user_acc) do - Builder.create_hashtag_link(hashtag, buffer, opts) + hashtag + |> Builder.create_hashtag_link(buffer, opts) + |> maybe_update_buffer(hashtag, buffer) end def link_mention(nil, buffer, _, user_acc), do: {buffer, user_acc} def link_mention(mention, buffer, %{mention_handler: mention_handler} = opts, user_acc) do - mention_handler.(mention, buffer, opts, user_acc) + mention + |> mention_handler.(buffer, opts, user_acc) + |> maybe_update_buffer(mention, buffer) end def link_mention(mention, buffer, opts, _user_acc) do - Builder.create_mention_link(mention, buffer, opts) + mention + |> Builder.create_mention_link(buffer, opts) + |> maybe_update_buffer(mention, buffer) end + defp maybe_update_buffer(out, match, buffer) when is_binary(out) do + maybe_update_buffer({out, nil}, match, buffer) + end + + defp maybe_update_buffer({out, user_acc}, match, buffer) + when match != buffer and out != buffer do + out = String.replace(buffer, match, out) + {out, user_acc} + end + + defp maybe_update_buffer(out, _match, _buffer), do: out + def link_phone(nil, buffer, _), do: buffer def link_phone(list, buffer, opts) do