diff --git a/src/view/com/composer/TagInput/index.tsx b/src/view/com/composer/TagInput/index.tsx index 98cb37e4b9..23040224dd 100644 --- a/src/view/com/composer/TagInput/index.tsx +++ b/src/view/com/composer/TagInput/index.tsx @@ -13,6 +13,7 @@ import { FontAwesomeIconStyle, } from '@fortawesome/react-native-fontawesome' import BottomSheet, {BottomSheetBackdrop} from '@gorhom/bottom-sheet' +import {sanitizeHashtag} from '@atproto/api' import {Portal} from 'view/com/util/Portal' import {TagsAutocompleteModel} from 'state/models/ui/tags-autocomplete' @@ -23,7 +24,7 @@ import {useStores} from 'state/index' import {ActivityIndicator} from 'react-native' import {TagInputEntryButton} from './TagInputEntryButton' import {uniq} from 'lib/strings/helpers' -import {sanitizeHashtag, sanitizeHashtagOnChange} from './util' +import {sanitizeHashtagOnChange} from './util' export function TagInput({ max = 8, diff --git a/src/view/com/composer/TagInput/index.web.tsx b/src/view/com/composer/TagInput/index.web.tsx index 2a9a95f1f7..ce0ee12926 100644 --- a/src/view/com/composer/TagInput/index.web.tsx +++ b/src/view/com/composer/TagInput/index.web.tsx @@ -13,6 +13,7 @@ import { FontAwesomeIconStyle, } from '@fortawesome/react-native-fontawesome' import {Pin} from 'pind' +import {sanitizeHashtag} from '@atproto/api' import {isWeb} from 'platform/detection' import {TagsAutocompleteModel} from 'state/models/ui/tags-autocomplete' @@ -23,7 +24,7 @@ import {useStores} from 'state/index' import {TagInputEntryButton} from './TagInputEntryButton' import {TextInputFocusEventData} from 'react-native' import {uniq} from 'lib/strings/helpers' -import {sanitizeHashtag, sanitizeHashtagOnChange} from './util' +import {sanitizeHashtagOnChange} from './util' export function TagInput({ max = 8, diff --git a/src/view/com/composer/TagInput/util.ts b/src/view/com/composer/TagInput/util.ts index 7abab04f0e..f178a2f3d2 100644 --- a/src/view/com/composer/TagInput/util.ts +++ b/src/view/com/composer/TagInput/util.ts @@ -1,28 +1,19 @@ import { HASHTAG_INVALID_CHARACTER_REGEX, - TRAILING_PUNCTUATION_REGEX, LEADING_PUNCTUATION_REGEX, LEADING_NUMBER_REGEX, + LEADING_HASH_REGEX, } from '@atproto/api' /** - * Trims leading numbers, all invalid characters, and any trailing punctuation. - */ -export function sanitizeHashtag(hashtag: string) { - return hashtag - .replace(LEADING_PUNCTUATION_REGEX, '') - .replace(LEADING_NUMBER_REGEX, '') - .replace(HASHTAG_INVALID_CHARACTER_REGEX, '') - .replace(TRAILING_PUNCTUATION_REGEX, '') -} - -/** - * Trims leading numbers and all invalid charactes, but ignores trailing + * Basically `sanitizeHashtag` from `@atproto/api`, but ignores trailing * punctuation in case the user intends to use `_` or `-`. */ export function sanitizeHashtagOnChange(hashtag: string) { return hashtag - .replace(LEADING_PUNCTUATION_REGEX, '') + .replace(LEADING_HASH_REGEX, '') .replace(LEADING_NUMBER_REGEX, '') + .replace(LEADING_PUNCTUATION_REGEX, '') .replace(HASHTAG_INVALID_CHARACTER_REGEX, '') + .slice(0, 64) } diff --git a/src/view/com/composer/text-input/web/Tags/utils.ts b/src/view/com/composer/text-input/web/Tags/utils.ts index 654552e07c..6894d2ba04 100644 --- a/src/view/com/composer/text-input/web/Tags/utils.ts +++ b/src/view/com/composer/text-input/web/Tags/utils.ts @@ -1,5 +1,5 @@ import { - HASHTAG_REGEX_WITH_TRAILING_PUNCTUATION, + HASHTAG_WITH_TRAILING_PUNCTUATION_REGEX, TRAILING_PUNCTUATION_REGEX, LEADING_HASH_REGEX, } from '@atproto/api' @@ -31,7 +31,7 @@ export function findSuggestionMatch({ cursorPosition: number }) { const match = Array.from( - text.matchAll(HASHTAG_REGEX_WITH_TRAILING_PUNCTUATION), + text.matchAll(HASHTAG_WITH_TRAILING_PUNCTUATION_REGEX), ).pop() if (!match || match.input === undefined || match.index === undefined) {