about summary refs log tree commit diff
path: root/src/lib/strings
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/strings')
-rw-r--r--src/lib/strings/bidi.ts10
-rw-r--r--src/lib/strings/handles.ts6
2 files changed, 15 insertions, 1 deletions
diff --git a/src/lib/strings/bidi.ts b/src/lib/strings/bidi.ts
new file mode 100644
index 000000000..790c2b6ac
--- /dev/null
+++ b/src/lib/strings/bidi.ts
@@ -0,0 +1,10 @@
+const LEFT_TO_RIGHT_EMBEDDING = '\u202A'
+const POP_DIRECTIONAL_FORMATTING = '\u202C'
+
+/*
+ * Force LTR directionality in a string.
+ * https://www.unicode.org/reports/tr9/#Directional_Formatting_Characters
+ */
+export function forceLTR(str: string) {
+  return LEFT_TO_RIGHT_EMBEDDING + str + POP_DIRECTIONAL_FORMATTING
+}
diff --git a/src/lib/strings/handles.ts b/src/lib/strings/handles.ts
index bc07b32ec..7955e1b2e 100644
--- a/src/lib/strings/handles.ts
+++ b/src/lib/strings/handles.ts
@@ -1,5 +1,7 @@
 // Regex from the go implementation
 // https://github.com/bluesky-social/indigo/blob/main/atproto/syntax/handle.go#L10
+import {forceLTR} from 'lib/strings/bidi'
+
 const VALIDATE_REGEX =
   /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/
 
@@ -22,7 +24,9 @@ export function isInvalidHandle(handle: string): boolean {
 }
 
 export function sanitizeHandle(handle: string, prefix = ''): string {
-  return isInvalidHandle(handle) ? '⚠Invalid Handle' : `${prefix}${handle}`
+  return isInvalidHandle(handle)
+    ? '⚠Invalid Handle'
+    : forceLTR(`${prefix}${handle}`)
 }
 
 export interface IsValidHandle {