about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2022-11-23 13:15:38 -0600
committerPaul Frazee <pfrazee@gmail.com>2022-11-23 13:15:38 -0600
commit0840c3f8f7ea491ec9c7f4ff11a7becf21435dd0 (patch)
tree227ca1f269b0819d29694bdcfec8111e4a959caf
parent67906db720b4b1e4c6ed9010c1f573c95c585e36 (diff)
downloadvoidsky-0840c3f8f7ea491ec9c7f4ff11a7becf21435dd0.tar.zst
Fix: dont detect double dots as urls
-rw-r--r--__tests__/string-utils.ts4
-rw-r--r--src/lib/strings.ts4
2 files changed, 6 insertions, 2 deletions
diff --git a/__tests__/string-utils.ts b/__tests__/string-utils.ts
index 37e0012a8..2ebeb0a2c 100644
--- a/__tests__/string-utils.ts
+++ b/__tests__/string-utils.ts
@@ -26,6 +26,7 @@ describe('extractEntities', () => {
     'start.com/foo/bar?baz=bux#hash middle end',
     'start middle end.com/foo/bar?baz=bux#hash',
     'newline1.com\nnewline2.com',
+    'not.. a..url ..here',
   ]
   interface Output {
     type: string
@@ -74,6 +75,7 @@ describe('extractEntities', () => {
       {type: 'link', value: 'newline1.com', noScheme: true},
       {type: 'link', value: 'newline2.com', noScheme: true},
     ],
+    [],
   ]
   it('correctly handles a set of text inputs', () => {
     for (let i = 0; i < inputs.length; i++) {
@@ -138,6 +140,7 @@ describe('detectLinkables', () => {
     'start.com/foo/bar?baz=bux#hash middle end',
     'start middle end.com/foo/bar?baz=bux#hash',
     'newline1.com\nnewline2.com',
+    'not.. a..url ..here',
   ]
   const outputs = [
     ['no linkable'],
@@ -163,6 +166,7 @@ describe('detectLinkables', () => {
     [{link: 'start.com/foo/bar?baz=bux#hash'}, ' middle end'],
     ['start middle ', {link: 'end.com/foo/bar?baz=bux#hash'}],
     [{link: 'newline1.com'}, '\n', {link: 'newline2.com'}],
+    ['not.. a..url ..here'],
   ]
   it('correctly handles a set of text inputs', () => {
     for (let i = 0; i < inputs.length; i++) {
diff --git a/src/lib/strings.ts b/src/lib/strings.ts
index 6457e4b90..f884cc86c 100644
--- a/src/lib/strings.ts
+++ b/src/lib/strings.ts
@@ -83,7 +83,7 @@ export function extractEntities(
   {
     // links
     const re =
-      /(^|\s)((https?:\/\/[\S]+)|([a-z][a-z0-9]*\.[a-z0-9\.]+[\S]*))(\b)/dg
+      /(^|\s)((https?:\/\/[\S]+)|([a-z][a-z0-9]*(\.[a-z0-9]+)+[\S]*))(\b)/dg
     while ((match = re.exec(text))) {
       let value = match[2]
       if (!value.startsWith('http')) {
@@ -108,7 +108,7 @@ interface DetectedLink {
 type DetectedLinkable = string | DetectedLink
 export function detectLinkables(text: string): DetectedLinkable[] {
   const re =
-    /((^|\s)@[a-z0-9\.-]*)|((^|\s)https?:\/\/[\S]+)|((^|\s)[a-z][a-z0-9]*\.[a-z0-9\.]+[\S]*)/gi
+    /((^|\s)@[a-z0-9\.-]*)|((^|\s)https?:\/\/[\S]+)|((^|\s)[a-z][a-z0-9]*(\.[a-z0-9]+)+[\S]*)/gi
   const segments = []
   let match
   let start = 0