bsky-app/src/lib/gif-alt-text.ts
Samuel Newman 7d72dfb1cb
[GIFs] Restore default alt text (#3893)
* restore default alt text

* factor out gif alt logic + enable require alt text setting

* rm console.log

* don't prefill input + esc handling

* typo

* Nits

* shorten user alt prefix

* Remove unnecessary condition, rename for clarity

* Add comment

---------

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2024-05-07 20:05:40 +01:00

36 lines
883 B
TypeScript

// Kind of a hack. We needed some way to distinguish these.
const USER_ALT_PREFIX = 'Alt: '
const DEFAULT_ALT_PREFIX = 'ALT: '
export function createGIFDescription(
tenorDescription: string,
preferredAlt: string = '',
) {
preferredAlt = preferredAlt.trim()
if (preferredAlt !== '') {
return USER_ALT_PREFIX + preferredAlt
} else {
return DEFAULT_ALT_PREFIX + tenorDescription
}
}
export function parseAltFromGIFDescription(description: string): {
isPreferred: boolean
alt: string
} {
if (description.startsWith(USER_ALT_PREFIX)) {
return {
isPreferred: true,
alt: description.replace(USER_ALT_PREFIX, ''),
}
} else if (description.startsWith(DEFAULT_ALT_PREFIX)) {
return {
isPreferred: false,
alt: description.replace(DEFAULT_ALT_PREFIX, ''),
}
}
return {
isPreferred: false,
alt: description,
}
}