Merge branch 'fix/carriage-return' into 'master'
fix carriage return See merge request pleroma/auto_linker!3
This commit is contained in:
commit
ae032126cd
2 changed files with 31 additions and 30 deletions
|
@ -205,23 +205,20 @@ defmodule AutoLinker.Parser do
|
|||
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
|
||||
)
|
||||
|
||||
defp do_parse({"\n" <> text, user_acc}, opts, {buffer, acc, {:open, level}}, handler),
|
||||
do:
|
||||
do_parse(
|
||||
{text, user_acc},
|
||||
opts,
|
||||
{"", acc <> buffer <> "\n", {:attrs, level}},
|
||||
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
|
||||
)
|
||||
end
|
||||
|
||||
# default cases where state is not important
|
||||
defp do_parse(
|
||||
|
@ -232,24 +229,19 @@ defmodule AutoLinker.Parser do
|
|||
),
|
||||
do: do_parse({text, user_acc}, opts, {buffer <> " ", acc, state}, handler)
|
||||
|
||||
defp do_parse({" " <> text, user_acc}, opts, {buffer, acc, state}, handler) do
|
||||
defp do_parse(
|
||||
{<<char::bytes-size(1), text::binary>>, user_acc},
|
||||
opts,
|
||||
{buffer, acc, state},
|
||||
handler
|
||||
)
|
||||
when char in [" ", "\r", "\n"] do
|
||||
{buffer, user_acc} = run_handler(handler, buffer, opts, user_acc)
|
||||
|
||||
do_parse(
|
||||
{text, user_acc},
|
||||
opts,
|
||||
{"", acc <> buffer <> " ", state},
|
||||
handler
|
||||
)
|
||||
end
|
||||
|
||||
defp do_parse({"\n" <> text, user_acc}, opts, {buffer, acc, state}, handler) do
|
||||
{buffer, user_acc} = run_handler(handler, buffer, opts, user_acc)
|
||||
|
||||
do_parse(
|
||||
{text, user_acc},
|
||||
opts,
|
||||
{"", acc <> buffer <> "\n", state},
|
||||
{"", acc <> buffer <> char, state},
|
||||
handler
|
||||
)
|
||||
end
|
||||
|
|
|
@ -51,6 +51,15 @@ defmodule AutoLinker.ParserTest do
|
|||
end
|
||||
|
||||
describe "parse" do
|
||||
test "handle line breakes" do
|
||||
text = "google.com\r\nssss"
|
||||
|
||||
expected =
|
||||
"<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
|
||||
|
||||
assert parse(text) == expected
|
||||
end
|
||||
|
||||
test "does not link attributes" do
|
||||
text = "Check out <a href='google.com'>google</a>"
|
||||
assert parse(text) == text
|
||||
|
|
Loading…
Reference in a new issue