From b31cc7bc2539809072c744bb7eb94a31ac2927e8 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 25 Jun 2019 05:32:25 +0200 Subject: [PATCH] Fix link regex edgecases --- src/formatters.nim | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/formatters.nim b/src/formatters.nim index 4662c68..bef2c71 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -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", "
") result = result.replace(usernameRegex, reUsernameToLink) - result = result.replace(re"([^\s\(\n])\s+([;.,!\)']|')", "$1") + result = result.replace(re"([^\s\(\n%])\s+([;.,!\)'%]|')", "$1") proc stripTwitterUrls*(text: string): string = result = text