commit: a6e87558631ffdc6955cf7dbc47b319fdfd8c976
parent 02d7cb1eba656627a183345dc1b77d78e7dce103
Author: Egor Kislitsyn <egor@kislitsyn.com>
Date: Tue, 9 Apr 2019 13:45:55 +0700
do not link inside `<pre>` and `<code>`
Diffstat:
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex
@@ -162,9 +162,21 @@ defmodule AutoLinker.Parser do
defp do_parse({"<a" <> text, user_acc}, opts, {buffer, acc, :parsing}, handler),
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<a", :skip}, handler)
+ defp do_parse({"<pre" <> text, user_acc}, opts, {buffer, acc, :parsing}, handler),
+ do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<pre", :skip}, handler)
+
+ defp do_parse({"<code" <> text, user_acc}, opts, {buffer, acc, :parsing}, handler),
+ do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<code", :skip}, handler)
+
defp do_parse({"</a>" <> text, user_acc}, opts, {buffer, acc, :skip}, handler),
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "</a>", :parsing}, handler)
+ defp do_parse({"</pre>" <> text, user_acc}, opts, {buffer, acc, :skip}, handler),
+ do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "</pre>", :parsing}, handler)
+
+ defp do_parse({"</code>" <> text, user_acc}, opts, {buffer, acc, :skip}, handler),
+ do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "</code>", :parsing}, handler)
+
defp do_parse({"<" <> text, user_acc}, opts, {"", acc, :parsing}, handler),
do: do_parse({text, user_acc}, opts, {"<", acc, {:open, 1}}, handler)
diff --git a/test/parser_test.exs b/test/parser_test.exs
@@ -69,6 +69,17 @@ defmodule AutoLinker.ParserTest do
assert parse(text) == text
end
+ test "does not link inside `<pre>` and `<code>`" do
+ text = "<pre>google.com</pre>"
+ assert parse(text) == text
+
+ text = "<code>google.com</code>"
+ assert parse(text) == text
+
+ text = "<pre><code>google.com</code></pre>"
+ assert parse(text) == text
+ end
+
test "links url inside html" do
text = "<div>google.com</div>"