Files
bsky-social-app/src/lib/strings/helpers.ts
T
Samuel Newman 2beb63fb2d migrate RichText to the SDK and resolve facets via the appview
The two RichText classes are not mutually assignable - `UnicodeString` has a
private field and the SDK brands `did`/`uri` as template literal types - so
every producer and consumer of a RichText instance has to move in one step.

`detectFacets` now takes a lex client instead of the legacy agent, which is
what removes the last hard agent dependency from these files. Handle
resolution is an appview job, so the appview client is threaded in: through
`useAppviewClient` in the hooks and dialogs, and through a new
`appviewClient` option on `apilib.post` (Composer already had the client to
hand). The rest of the post pipeline still writes through the agent.

Facet feature checks move from the `AppBskyRichtextFacet` validators to the
generated `#/lexicons` schemas, matching how the rest of the app narrows
lexicon types.

Display sinks still read facets off `@atproto/api` view types, which are the
same lexicon but typed with plain strings. `asSdkFacets` widens them at those
call sites and goes away once the view types come from the SDK too.
2026-08-13 21:50:54 +03:00

59 lines
1.4 KiB
TypeScript

import {type RichText} from '@bsky.app/sdk/richtext'
import {countGraphemes} from 'unicode-segmenter/grapheme'
import {shortenLinks} from './rich-text-manip'
export function enforceLen(
str: string,
len: number,
ellipsis = false,
mode: 'end' | 'middle' = 'end',
): string {
str = str || ''
if (str.length > len) {
if (ellipsis) {
if (mode === 'end') {
return str.slice(0, len) + '…'
} else if (mode === 'middle') {
const half = Math.floor(len / 2)
return str.slice(0, half) + '…' + str.slice(-half)
} else {
// fallback
return str.slice(0, len)
}
} else {
return str.slice(0, len)
}
}
return str
}
export function isOverMaxGraphemeCount({
text,
maxCount,
}: {
text: string | RichText
maxCount: number
}) {
if (typeof text === 'string') {
return countGraphemes(text) > maxCount
} else {
return shortenLinks(text).graphemeLength > maxCount
}
}
export function countLines(str: string | undefined): number {
if (!str) return 0
return str.match(/\n/g)?.length ?? 0
}
/**
* Normalizes a raw search query for the backend. The iOS keyboard inserts smart
* quotes, but only straight quotes work for exact-phrase matching. Operators
* like `from:me` are passed through untouched - the backend resolves `me` to
* the viewer.
*/
export function augmentSearchQuery(query: string) {
return query.replaceAll(/[“”]/g, '"')
}