Fix link regex edgecases

This commit is contained in:
Zed 2019-06-25 05:32:25 +02:00
parent aae0e51154
commit b31cc7bc25

View file

@ -6,9 +6,9 @@ import ./types, ./utils
from unicode import Rune, `$`
const
urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+(/[^\s]*)?)"
urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+(/[^\s\)]*)?)"
emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
usernameRegex = re"(^|[^\S\n]|\.|>)@([A-z0-9_]+)"
usernameRegex = re"(^|[^\S]|\.|>)@([A-z0-9_]+)"
picRegex = re"pic.twitter.com/[^ ]+"
cardRegex = re"(https?://)?cards.twitter.com/[^ ]+"
ellipsisRegex = re" ?…"
@ -52,10 +52,9 @@ proc linkifyText*(text: string): string =
result = result.replace(ellipsisRegex, "")
result = result.replace(emailRegex, reEmailToLink)
result = result.replace(urlRegex, reUrlToLink)
result = result.replace("\n", "<br>")
result = result.replace(usernameRegex, reUsernameToLink)
result = result.replace(re"([^\s\(\n])<a", "$1 <a")
result = result.replace(re"</a>\s+([;.,!\)']|&apos;)", "</a>$1")
result = result.replace(re"([^\s\(\n%])<a", "$1 <a")
result = result.replace(re"</a>\s+([;.,!\)'%]|&apos;)", "</a>$1")
proc stripTwitterUrls*(text: string): string =
result = text