Skip to content

Commit a5d198f

Browse files
authored
Merge pull request #774 from Adamant-im/fix/markdown-headings-2
fix: formatting for markdown headings in chat list and notifications
2 parents 8f8dda5 + 1925d9b commit a5d198f

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

src/components/AChat/AChatReplyPreview.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { useStore } from 'vuex'
2929
import ChatAvatar from '@/components/Chat/ChatAvatar.vue'
3030
import { Cryptos } from '@/lib/constants'
3131
import currencyFormatter from '@/filters/currencyAmountWithSymbol'
32-
import { formatMessage } from '@/lib/markdown'
32+
import { formatChatPreviewMessage } from '@/lib/markdown'
3333
import { mdiClose } from '@mdi/js'
3434
import type { NormalizedChatMessageTransaction } from '@/lib/chat/helpers'
3535
@@ -73,7 +73,7 @@ const cryptoTransferLabel = computed(() => {
7373
7474
const messageLabel = computed(() => {
7575
return store.state.options.formatMessages
76-
? formatMessage(props.message.message)
76+
? formatChatPreviewMessage(props.message.message)
7777
: props.message.message
7878
})
7979
</script>

src/components/AChat/QuotedMessage.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { getTransaction, decodeChat } from '@/lib/adamant-api'
4343
import { NormalizedChatMessageTransaction, normalizeMessage } from '@/lib/chat/helpers'
4444
import { Cryptos } from '@/lib/constants'
4545
import currencyFormatter from '@/filters/currencyAmountWithSymbol'
46-
import { formatMessage } from '@/lib/markdown'
46+
import { formatChatPreviewMessage } from '@/lib/markdown'
4747
import { ChatMessageTransaction } from '@/lib/schema/client/api'
4848
4949
const className = 'quoted-message'
@@ -141,7 +141,7 @@ export default defineComponent({
141141
}
142142
143143
return store.state.options.formatMessages
144-
? formatMessage(transaction.value.message)
144+
? formatChatPreviewMessage(transaction.value.message)
145145
: transaction.value.message
146146
})
147147

src/components/ChatPreview.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ import ChatAvatar from '@/components/Chat/ChatAvatar.vue'
109109
import Icon from '@/components/icons/BaseIcon.vue'
110110
import currency from '@/filters/currencyAmountWithSymbol'
111111
import formatDate from '@/filters/dateBrief'
112-
import { formatMessage } from '@/lib/markdown'
112+
import { formatChatPreviewMessage } from '@/lib/markdown'
113113
import { isAdamantChat, isWelcomeChat } from '@/lib/chat/meta/utils'
114114
import { NormalizedChatMessageTransaction } from '@/lib/chat/helpers'
115115
import { isStringEqualCI } from '@/lib/textHelpers'
@@ -196,7 +196,7 @@ const lastMessageTextLocalized = computed(() =>
196196
)
197197
const lastMessageTextNoFormats = computed(() => {
198198
if (isAdamantChat(contactId.value) || store.state.options.formatMessages) {
199-
return formatMessage(lastMessageTextLocalized.value)
199+
return formatChatPreviewMessage(lastMessageTextLocalized.value)
200200
}
201201
202202
return lastMessageTextLocalized.value

src/lib/markdown.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,30 @@ export function renderMarkdown(text = '') {
6767
* @param {string} text text to process
6868
* @returns {string} resulting clear text of the first line
6969
*/
70-
export function removeFormats(text = '') {
71-
const node = document.createElement('div')
72-
const textWithSymbol = text.replace(/\n/g, '↵ ')
73-
node.innerHTML = marked.parse(sanitizeHTML(textWithSymbol), { async: false })
74-
75-
return node.textContent || node.innerText || ''
76-
}
77-
78-
export function formatMessage(text = '') {
70+
export function formatMessageBasic(text = '') {
7971
const node = document.createElement('div')
8072

8173
const textWithSymbol = text.replace(/\n/g, LINE_BREAK_VISUAL)
8274
node.innerHTML = marked.parse(sanitizeHTML(textWithSymbol), { async: false })
8375

84-
const textWithoutHtml = node.textContent || node.innerText || ''
76+
let textWithoutHtml = node.textContent || node.innerText || ''
8577

86-
return textWithoutHtml
78+
textWithoutHtml = textWithoutHtml
8779
.replace(LINE_SEPARATOR, LINE_BREAK_VISUAL)
80+
.split(LINE_BREAK_VISUAL)
81+
// strip all markdown heading signs (#) but only if they are at the beginning of the line
82+
.map((line) => line.replace(/^[#]+ /, ''))
83+
.join(LINE_BREAK_VISUAL)
84+
85+
return textWithoutHtml
86+
}
87+
88+
/**
89+
* Formats a message for display in a chat preview
90+
* @param {string} text text to process
91+
* @returns {string} resulting clear text of the first line
92+
*/
93+
export function formatChatPreviewMessage(text = '') {
94+
return formatMessageBasic(text)
8895
.replace(//g, '<span class="arrow-return">↵</span>')
8996
}

src/lib/notifications.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Notify from 'notifyjs'
44
import Visibility from 'visibilityjs'
55
import currency from '@/filters/currencyAmountWithSymbol'
6-
import { removeFormats } from '@/lib/markdown'
6+
import { formatMessageBasic } from '@/lib/markdown'
77
import { isAdamantChat } from '@/lib/chat/meta/utils'
88

99
let _this
@@ -80,7 +80,7 @@ class PushNotification extends Notification {
8080
message = this.lastUnread.message
8181
}
8282
const processedMessage = this.store.state.options.formatMessages
83-
? removeFormats(message)
83+
? formatMessageBasic(message)
8484
: message
8585
return `${this.partnerIdentity}: ${processedMessage}`
8686
}

0 commit comments

Comments
 (0)