From 74afaca73b6b688d87e48a88ee8e8e3ee8421294 Mon Sep 17 00:00:00 2001 From: Sergey Suprunenko Date: Sat, 29 Aug 2020 23:30:43 +0200 Subject: [PATCH] Handle punctuation marks and angle bracket in the end of a link --- CHANGELOG.md | 2 ++ lib/linkify/parser.ex | 1 + test/parser_test.exs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d86357..adfc4a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Fixed - Hashtags followed by HTML tags "a", "code" and "pre" were not detected +- Incorrect parsing of HTML links inside HTML tags +- Punctuation marks in the end of urls were included in the html links ## 0.2.0 - 2020-07-21 diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex index 38eca8c..dbb27fb 100644 --- a/lib/linkify/parser.ex +++ b/lib/linkify/parser.ex @@ -193,6 +193,7 @@ defmodule Linkify.Parser do buffer |> String.split("<") |> List.first() + |> String.replace(~r/[,.;:)>]$/, "") |> strip_parens() if url?(str, opts) do diff --git a/test/parser_test.exs b/test/parser_test.exs index 718be90..352f237 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -114,6 +114,20 @@ defmodule Linkify.ParserTest do assert parse(text) == expected end + test "handle angle bracket in the end" do + text = "google.com
" + assert parse(text) == "google.com
" + + text = "google.com
" + assert parse(text) == "google.com
" + + text = "google.com<" + assert parse(text) == "google.com<" + + text = "google.com>" + assert parse(text) == "google.com>" + end + test "does not link attributes" do text = "Check out google" assert parse(text) == text @@ -185,6 +199,23 @@ defmodule Linkify.ParserTest do assert parse(text, class: false, rel: false) == expected end + test "do not link punctuation marks in the end" do + text = "google.com." + assert parse(text) == "google.com." + + text = "google.com;" + assert parse(text) == "google.com;" + + text = "google.com:" + assert parse(text) == "google.com:" + + text = "hack google.com, please" + assert parse(text) == "hack google.com, please" + + text = "(check out google.com)" + assert parse(text) == "(check out google.com)" + end + test "do not link urls" do text = "google.com" assert parse(text, url: false) == text