From ce56d4e34e75258104b26e2cdc14c423d937a8af Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 22 Nov 2022 13:16:40 -0600 Subject: Shorten rendered URLs in posts --- src/lib/strings.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/lib/strings.ts') 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') -- cgit 1.4.1