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 13:16:40 -0600
committerPaul Frazee <pfrazee@gmail.com>2022-11-22 13:16:40 -0600
commitce56d4e34e75258104b26e2cdc14c423d937a8af (patch)
treeb0d29452db7bcdf53757a66c3e372ae7ad7fe04a /src/lib/strings.ts
parent27db820a9d9731a16161d08f20e224f7e77a6af5 (diff)
downloadvoidsky-ce56d4e34e75258104b26e2cdc14c423d937a8af.tar.zst
Shorten rendered URLs in posts
Diffstat (limited to 'src/lib/strings.ts')
-rw-r--r--src/lib/strings.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib/strings.ts b/src/lib/strings.ts
index 8f2c3a3ea..c8a9171a3 100644
--- a/src/lib/strings.ts
+++ b/src/lib/strings.ts
@@ -141,6 +141,23 @@ export function toNiceDomain(url: string): string {
   }
 }
 
+export function toShortUrl(url: string): string {
+  try {
+    const urlp = new URL(url)
+    const shortened =
+      urlp.host +
+      (urlp.pathname === '/' ? '' : urlp.pathname) +
+      urlp.search +
+      urlp.hash
+    if (shortened.length > 20) {
+      return shortened.slice(0, 17) + '...'
+    }
+    return shortened
+  } catch (e) {
+    return url
+  }
+}
+
 export function toShareUrl(url: string) {
   if (!url.startsWith('https')) {
     const urlp = new URL('https://bsky.app')