Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
Linkify
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pleroma
Elixir libraries
Linkify
Commits
e862ee9d
Commit
e862ee9d
authored
5 years ago
by
minibikini
Browse files
Options
Downloads
Patches
Plain Diff
improve parsing (
#2
)
parent
6dd627bf
No related branches found
No related tags found
2 merge requests
!10
Improve Parser
,
!9
Improve detection of URLs and Asian scripts
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/auto_linker/parser.ex
+15
-6
15 additions, 6 deletions
lib/auto_linker/parser.ex
test/auto_linker_test.exs
+8
-0
8 additions, 0 deletions
test/auto_linker_test.exs
with
23 additions
and
6 deletions
lib/auto_linker/parser.ex
+
15
−
6
View file @
e862ee9d
...
@@ -29,22 +29,20 @@ defmodule AutoLinker.Parser do
...
@@ -29,22 +29,20 @@ defmodule AutoLinker.Parser do
@match_url
~r{^[\w\.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$}
@match_url
~r{^[\w\.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$}
@match_scheme
~r{^(?:http
(s)
?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$
}
@match_scheme
~r{^(?:
\W*)?(?<url>(?:\W*
http
s
?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:
\
/?#[\]@!\$&'\(\)\*\+,;=.]+$
)}u
@match_phone
~r"((?:x\d{2,7})|(?:(?:\+?1\s?(?:[.-]\s?)?)?(?:\(\s?(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s?\)|(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s?(?:[.-]\s?)?)(?:[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s?(?:[.-]\s?)?(?:[0-9]{4}))"
@match_phone
~r"((?:x\d{2,7})|(?:(?:\+?1\s?(?:[.-]\s?)?)?(?:\(\s?(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s?\)|(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s?(?:[.-]\s?)?)(?:[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s?(?:[.-]\s?)?(?:[0-9]{4}))"
@match_hostname
~r{^(?:https?:\/\/)?(?:[^@\n]+\\w@)?(?<host>[^:#~\/\n?]+)}
@match_hostname
~r{^(?:
\W*
https?:\/\/)?(?:[^@\n]+\\w@)?(?<host>[^:#~\/\n?]+)}
u
@match_ip
~r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
@match_ip
~r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
# @user
# @user
# @user@example.com
# @user@example.com
# credo:disable-for-next-line
@match_mention
~r"^@[a-zA-Z\d_-]+@[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|@[a-zA-Z\d_-]+"u
@match_mention
~r/^@[a-zA-Z\d_-]+@[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|@[a-zA-Z\d_-]+/u
# https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
# https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
# credo:disable-for-next-line
@match_email
~r"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"u
@match_email
~r/^[a-zA-Z0-9.!#$%&'*+\/
=
?^
_
`
{
|
}
~
-
]
+
@
[
a
-
zA
-
Z0
-
9
](
?:
[
a
-
zA
-
Z0
-
9
-
]{
0
,
61
}[
a
-
zA
-
Z0
-
9
])
?(?:
\
.
[
a
-
zA
-
Z0
-
9
](
?:
[
a
-
zA
-
Z0
-
9
-
]{
0
,
61
}[
a
-
zA
-
Z0
-
9
])
?)
*
$
/
u
@match_hashtag
~r/^(?<tag>\#[[:word:]_]*[[:alpha:]_·][[:word:]_·]*)/u
@match_hashtag
~r/^(?<tag>\#[[:word:]_]*[[:alpha:]_·][[:word:]_·]*)/u
...
@@ -261,6 +259,17 @@ defmodule AutoLinker.Parser do
...
@@ -261,6 +259,17 @@ defmodule AutoLinker.Parser do
defp
do_parse
({
<<
ch
::
8
>>
<>
text
,
user_acc
},
opts
,
{
buffer
,
acc
,
state
},
handler
),
defp
do_parse
({
<<
ch
::
8
>>
<>
text
,
user_acc
},
opts
,
{
buffer
,
acc
,
state
},
handler
),
do
:
do_parse
({
text
,
user_acc
},
opts
,
{
buffer
<>
<<
ch
::
8
>>
,
acc
,
state
},
handler
)
do
:
do_parse
({
text
,
user_acc
},
opts
,
{
buffer
<>
<<
ch
::
8
>>
,
acc
,
state
},
handler
)
def
check_and_link
(
buffer
,
%{
scheme:
true
}
=
opts
,
_user_acc
)
do
if
is_url?
(
buffer
,
opts
[
:scheme
])
do
case
Regex
.
run
(
@match_scheme
,
buffer
,
capture:
[
:url
])
do
[
^
buffer
]
->
link_url
(
true
,
buffer
,
opts
)
[
url
]
->
String
.
replace
(
buffer
,
url
,
link_url
(
true
,
url
,
opts
))
end
else
buffer
end
end
def
check_and_link
(
buffer
,
opts
,
_user_acc
)
do
def
check_and_link
(
buffer
,
opts
,
_user_acc
)
do
buffer
buffer
|>
is_url?
(
opts
[
:scheme
])
|>
is_url?
(
opts
[
:scheme
])
...
...
This diff is collapsed.
Click to expand it.
test/auto_linker_test.exs
+
8
−
0
View file @
e862ee9d
...
@@ -242,6 +242,14 @@ defmodule AutoLinkerTest do
...
@@ -242,6 +242,14 @@ defmodule AutoLinkerTest do
assert
AutoLinker
.
link
(
text
,
scheme:
true
)
==
expected
assert
AutoLinker
.
link
(
text
,
scheme:
true
)
==
expected
end
end
test
"turn urls with schema into urls"
do
text
=
"📌https://google.com"
expected
=
"📌<a href=
\"
https://google.com
\"
>google.com</a>"
assert
AutoLinker
.
link
(
text
,
scheme:
true
,
class:
false
,
new_window:
false
,
rel:
false
)
==
expected
end
test
"hostname/@user"
do
test
"hostname/@user"
do
text
=
"https://example.com/@user"
text
=
"https://example.com/@user"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment