about summary refs log tree commit diff
path: root/src/lib/strings.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2022-11-22 14:30:35 -0600
committerPaul Frazee <pfrazee@gmail.com>2022-11-22 14:30:35 -0600
commite488cf8f44a0d6b2b05e50a2cc2e5467ea6f7e37 (patch)
tree71e0c440e7363169fc1dee0d80ecc07482536942 /src/lib/strings.ts
parent1df48d4dad0d6b13047185e37db94997ab36bb4b (diff)
downloadvoidsky-e488cf8f44a0d6b2b05e50a2cc2e5467ea6f7e37.tar.zst
Add support for links with no scheme in composer
Diffstat (limited to 'src/lib/strings.ts')
-rw-r--r--src/lib/strings.ts46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/lib/strings.ts b/src/lib/strings.ts
index c8a9171a3..ea2a4dd9f 100644
--- a/src/lib/strings.ts
+++ b/src/lib/strings.ts
@@ -82,13 +82,18 @@ export function extractEntities(
   }
   {
     // links
-    const re = /(^|\s)(https?:\/\/[\S]+)(\b)/dg
+    const re =
+      /(^|\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')) {
+        value = `https://${value}`
+      }
       ents.push({
         type: 'link',
-        value: match[2],
+        value,
         index: {
-          start: match.indices[1][0], // skip the (^|\s) but include the '@'
+          start: match.indices[2][0], // skip the (^|\s)
           end: match.indices[2][1],
         },
       })
@@ -97,6 +102,41 @@ export function extractEntities(
   return ents.length > 0 ? ents : undefined
 }
 
+interface DetectedLink {
+  link: string
+}
+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
+  const segments = []
+  let match
+  let start = 0
+  while ((match = re.exec(text))) {
+    let matchIndex = match.index
+    let matchValue = match[0]
+
+    if (/\s/.test(matchValue)) {
+      // HACK
+      // skip the starting space
+      // we have to do this because RN doesnt support negative lookaheads
+      // -prf
+      matchIndex++
+      matchValue = matchValue.slice(1)
+    }
+
+    if (start !== matchIndex) {
+      segments.push(text.slice(start, matchIndex))
+    }
+    segments.push({link: matchValue})
+    start = matchIndex + matchValue.length
+  }
+  if (start < text.length) {
+    segments.push(text.slice(start))
+  }
+  return segments
+}
+
 export function makeValidHandle(str: string): string {
   if (str.length > 20) {
     str = str.slice(0, 20)