about summary refs log tree commit diff
path: root/src/lib/gif-alt-text.ts
blob: 41738c5067e8c95d3622905a323e207613423792 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 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,
  }
}