logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker git clone https://hacktivis.me/git/auto_linker.git
commit: 4949c02cb62d36d25893fe80a00efe7eebd15f60
parent 50882b427a641e82957e17f206b41130628a5477
Author: Egor Kislitsyn <egor@kislitsyn.com>
Date:   Tue,  9 Apr 2019 13:08:13 +0700

fix parsing inside HTML tags

Diffstat:

Mlib/auto_linker/parser.ex15++-------------
Mtest/auto_linker_test.exs16++++++++++++++++
Mtest/parser_test.exs13+++++++++++++
3 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex @@ -204,19 +204,8 @@ defmodule AutoLinker.Parser do handler ) - defp do_parse( - {<<char::bytes-size(1), text::binary>>, user_acc}, - opts, - {buffer, acc, {:open, level}}, - handler - ) - when char in [" ", "\r", "\n"] do - do_parse( - {text, user_acc}, - opts, - {"", acc <> buffer <> char, {:attrs, level}}, - handler - ) + defp do_parse({text, user_acc}, opts, {buffer, acc, {:open, level}}, handler) do + do_parse({text, user_acc}, opts, {"", acc <> buffer, {:attrs, level}}, handler) end # default cases where state is not important diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs @@ -144,6 +144,22 @@ defmodule AutoLinkerTest do ) == expected end + test "mentions inside html tags" do + text = + "<p><strong>hello world</strong></p>\n<p><`em>another @user__test and @user__test google.com paragraph</em></p>\n" + + expected = + "<p><strong>hello world</strong></p>\n<p><`em>another <a href=\"u/user__test\">@user__test</a> and <a href=\"u/user__test\">@user__test</a> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n" + + assert AutoLinker.link(text, + mention: true, + mention_prefix: "u/", + class: false, + rel: false, + new_window: false + ) == expected + end + test "metion @user@example.com" do text = "hey @user@example.com" diff --git a/test/parser_test.exs b/test/parser_test.exs @@ -70,6 +70,19 @@ defmodule AutoLinker.ParserTest do end test "links url inside html" do + text = "<div>google.com</div>" + + expected = "<div><a href=\"http://google.com\">google.com</a></div>" + + assert parse(text, class: false, rel: false, new_window: false) == expected + + text = "Check out <div class='section'>google.com</div>" + + expected = + "Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>" + + assert parse(text, class: false, rel: false, new_window: false) == expected + end text = "Check out <div class='section'>google.com</div>" expected =