logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker git clone https://hacktivis.me/git/auto_linker.git
commit: 74afaca73b6b688d87e48a88ee8e8e3ee8421294
parent e5a8cf2b08edfe22f776d9c5f500029747daf1c8
Author: Sergey Suprunenko <suprunenko.s@gmail.com>
Date:   Sat, 29 Aug 2020 23:30:43 +0200

Handle punctuation marks and angle bracket in the end of a link

Diffstat:

MCHANGELOG.md2++
Mlib/linkify/parser.ex1+
Mtest/parser_test.exs31+++++++++++++++++++++++++++++++
3 files changed, 34 insertions(+), 0 deletions(-)

diff --git 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 @@ -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 @@ -114,6 +114,20 @@ defmodule Linkify.ParserTest do assert parse(text) == expected end + test "handle angle bracket in the end" do + text = "google.com <br>" + assert parse(text) == "<a href=\"http://google.com\">google.com</a> <br>" + + text = "google.com<br>" + assert parse(text) == "<a href=\"http://google.com\">google.com</a><br>" + + text = "google.com<" + assert parse(text) == "<a href=\"http://google.com\">google.com</a><" + + text = "google.com>" + assert parse(text) == "<a href=\"http://google.com\">google.com</a>>" + end + test "does not link attributes" do text = "Check out <a href='google.com'>google</a>" 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) == "<a href=\"http://google.com\">google.com</a>." + + text = "google.com;" + assert parse(text) == "<a href=\"http://google.com\">google.com</a>;" + + text = "google.com:" + assert parse(text) == "<a href=\"http://google.com\">google.com</a>:" + + text = "hack google.com, please" + assert parse(text) == "hack <a href=\"http://google.com\">google.com</a>, please" + + text = "(check out google.com)" + assert parse(text) == "(check out <a href=\"http://google.com\">google.com</a>)" + end + test "do not link urls" do text = "google.com" assert parse(text, url: false) == text