-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shorten links in composer to reduce char usage (#1188)
* Modify toShortUrl() to always include the full domain * Shorten links in the composer to save on characters * Apply some limits to the link card suggester
- Loading branch information
Showing
6 changed files
with
123 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {RichText, UnicodeString} from '@atproto/api' | ||
import {toShortUrl} from './url-helpers' | ||
|
||
export function shortenLinks(rt: RichText): RichText { | ||
if (!rt.facets?.length) { | ||
return rt | ||
} | ||
rt = rt.clone() | ||
// enumerate the link facets | ||
if (rt.facets) { | ||
for (const facet of rt.facets) { | ||
const isLink = !!facet.features.find( | ||
f => f.$type === 'app.bsky.richtext.facet#link', | ||
) | ||
if (!isLink) { | ||
continue | ||
} | ||
|
||
// extract and shorten the URL | ||
const {byteStart, byteEnd} = facet.index | ||
const url = rt.unicodeText.slice(byteStart, byteEnd) | ||
const shortened = new UnicodeString(toShortUrl(url)) | ||
|
||
// insert the shorten URL | ||
rt.insert(byteStart, shortened.utf16) | ||
// update the facet to cover the new shortened URL | ||
facet.index.byteStart = byteStart | ||
facet.index.byteEnd = byteStart + shortened.length | ||
// remove the old URL | ||
rt.delete(byteStart + shortened.length, byteEnd + shortened.length) | ||
} | ||
} | ||
return rt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters