diff --git a/lib/linkify/builder.ex b/lib/linkify/builder.ex index 4776d26..25ac8c6 100644 --- a/lib/linkify/builder.ex +++ b/lib/linkify/builder.ex @@ -123,14 +123,14 @@ defmodule Linkify.Builder do |> format_extra(uri, opts) end - def create_youtube(text, opts) do + def create_youtube(text, _opts) do id = get_video_id(text) "" end - Enum.each(["http", "https"], fn protocol -> + Enum.each(["", "http://", "https://"], fn protocol -> Enum.each(["youtu.be/", "www.youtube.com/watch?v=", "www.youtube.com/?v="], fn prefix -> - def get_video_id(unquote(protocol <> "://" <> prefix) <> id), do: id + def get_video_id(unquote(protocol <> prefix) <> id), do: id end) end) diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex index 9f175bf..81a4985 100644 --- a/lib/linkify/parser.ex +++ b/lib/linkify/parser.ex @@ -7,7 +7,7 @@ defmodule Linkify.Parser do @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/ - @match_youtube ~r{http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?}u + @match_youtube ~r{(http(?:s?):\/\/)?(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?}u @match_url ~r{^(?:\W*)?(?(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u @@ -56,7 +56,7 @@ defmodule Linkify.Parser do ~s{Check out google.com} """ - @types [:url, :email, :hashtag, :mention, :extra, :youtube] + @types [:url, :email, :hashtag, :mention, :extra] def parse(input, opts \\ %{}) def parse(input, opts) when is_binary(input), do: {input, %{}} |> parse(opts) |> elem(0) @@ -68,7 +68,7 @@ defmodule Linkify.Parser do Enum.reduce(opts, input, fn {type, true}, input when type in @types -> do_parse(input, opts, {"", "", :parsing}, type) - a, input -> + _, input -> input end) end @@ -172,19 +172,16 @@ defmodule Linkify.Parser do defp do_parse({<> <> text, user_acc}, opts, {buffer, acc, state}, type), do: do_parse({text, user_acc}, opts, {buffer <> <>, acc, state}, type) - def check_and_link(:youtube, buffer, opts, _user_acc) do - if youtube?(buffer, opts), do: link_youtube(buffer, opts), else: buffer - end - def check_and_link(:url, buffer, opts, _user_acc) do str = strip_parens(buffer) - if url?(str, opts) do - case @match_url |> Regex.run(str, capture: [:url]) |> hd() do - ^buffer -> link_url(buffer, opts) - url -> String.replace(buffer, url, link_url(url, opts)) - end - else - buffer + cond do + youtube?(str, opts) == true -> link_youtube(buffer, opts) + url?(str, opts) == true -> + case @match_url |> Regex.run(str, capture: [:url]) |> hd() do + ^buffer -> link_url(buffer, opts) + url -> String.replace(buffer, url, link_url(url, opts)) + end + true -> buffer end end