Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

fix(web): support nested WhatsApp formatting and format thread previews

Acho Arnold Ewin00d61b02bd25
diff --git a/web/app/components/MessageThread.vue b/web/app/components/MessageThread.vue
index 93c77324..53bd401b 100644
--- a/web/app/components/MessageThread.vue
+++ b/web/app/components/MessageThread.vue
@@ -8,6 +8,10 @@ import {
mdiAccount,
} from '@mdi/js'
import type { EntitiesMessageThread } from '~~/shared/types/api'
+import {
+ isUrlNotificationToken,
+ formatWhatsappText,
+} from '~/utils/whatsapp-format'
const threadsStore = useThreadsStore()
const phonesStore = usePhonesStore()
@@ -45,6 +49,19 @@ function onInstallApp() {
})
}
+function isWhatsappFormatted(thread: EntitiesMessageThread): boolean {
+ const phone = phonesStore.phones.find((x) => x.phone_number === thread.owner)
+ return isUrlNotificationToken(phone?.fcm_token)
+}
+
+function threadPreviewHtml(thread: EntitiesMessageThread): string {
+ // Links are unwrapped because the preview sits inside a router link.
+ return formatWhatsappText(thread.last_message_content ?? '').replace(
+ /<a [^>]*>(.*?)<\/a>/g,
+ '$1',
+ )
+}
+
function threadContactName(thread: EntitiesMessageThread): string {
return thread.contact_details?.name?.trim() ?? ''
}
@@ -160,7 +177,11 @@ function threadAvatarInitial(thread: EntitiesMessageThread): string {
}"
style="max-width: 250px"
>
- {{ thread.last_message_content }}
+ <span
+ v-if="isWhatsappFormatted(thread)"
+ v-html="threadPreviewHtml(thread)"
+ />
+ <template v-else>{{ thread.last_message_content }}</template>
</v-list-item-subtitle>
<template #append>
<div class="d-flex flex-column align-end">
diff --git a/web/app/utils/whatsapp-format.ts b/web/app/utils/whatsapp-format.ts
index c1e4e9e4..202a1235 100644
--- a/web/app/utils/whatsapp-format.ts
+++ b/web/app/utils/whatsapp-format.ts
@@ -23,7 +23,9 @@ function escapeHtml(text: string): string {
// Character class (as a regex source string) that is treated as a valid
// boundary around a formatting marker: whitespace, common punctuation, or
// the start/end of the string.
-const BOUNDARY_CHARS = String.raw`\s.,!?;:'"()\[\]{}-`
+// Other formatting markers and generated tags (<>) also count so that nested
+// formatting such as `_*text*_` works.
+const BOUNDARY_CHARS = String.raw`\s.,!?;:'"()\[\]{}<>*_~-`
function withBoundaries(pattern: string): RegExp {
return new RegExp(