Add basic Markdown rules for the chat

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-04-30 20:08:56 +02:00
parent 531eecf4b8
commit 64a2ea007e
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 22 additions and 3 deletions

View File

@ -157,7 +157,6 @@ function fetchPreviousMessages() {
} }
function receiveFetchedMessages(data) { function receiveFetchedMessages(data) {
console.log(data)
let channel_id = data['channel_id'] let channel_id = data['channel_id']
let new_messages = data['messages'] let new_messages = data['messages']
@ -237,7 +236,7 @@ function redrawMessages() {
messageContentDiv.setAttribute('data-message-id', message['id']) messageContentDiv.setAttribute('data-message-id', message['id'])
lastContentDiv.appendChild(messageContentDiv) lastContentDiv.appendChild(messageContentDiv)
let messageContentSpan = document.createElement('span') let messageContentSpan = document.createElement('span')
messageContentSpan.innerText = message['content'] messageContentSpan.innerHTML = markdownToHTML(message['content'])
messageContentDiv.appendChild(messageContentSpan) messageContentDiv.appendChild(messageContentSpan)
registerMessageContextMenu(message, messageContentDiv, messageContentSpan) registerMessageContextMenu(message, messageContentDiv, messageContentSpan)
@ -272,7 +271,7 @@ function redrawMessages() {
messageContentDiv.setAttribute('data-message-id', message['id']) messageContentDiv.setAttribute('data-message-id', message['id'])
contentDiv.appendChild(messageContentDiv) contentDiv.appendChild(messageContentDiv)
let messageContentSpan = document.createElement('span') let messageContentSpan = document.createElement('span')
messageContentSpan.innerText = message['content'] messageContentSpan.innerHTML = markdownToHTML(message['content'])
messageContentDiv.appendChild(messageContentSpan) messageContentDiv.appendChild(messageContentSpan)
registerMessageContextMenu(message, messageContentDiv, messageContentSpan) registerMessageContextMenu(message, messageContentDiv, messageContentSpan)
@ -290,6 +289,26 @@ function redrawMessages() {
messageList.dispatchEvent(new CustomEvent('updatemessages')) messageList.dispatchEvent(new CustomEvent('updatemessages'))
} }
function markdownToHTML(text) {
let safeText = text.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;")
let lines = safeText.split('\n')
let htmlLines = []
for (let line of lines) {
let htmlLine = line
.replaceAll(/_(.*)_/gim, '<span class="text-decoration-underline">$1</span>') // Underline
.replaceAll(/\*\*(.*)\*\*/gim, '<span class="fw-bold">$1</span>') // Bold
.replaceAll(/\*(.*)\*/gim, '<span class="fst-italic">$1</span>') // Italic
.replaceAll(/`(.*)`/gim, '<pre>$1</pre>') // Code
.replaceAll(/(https?:\/\/\S+)/g, '<a href="$1" target="_blank">$1</a>') // Links
htmlLines.push(htmlLine)
}
return htmlLines.join('<br>')
}
function removeAllPopovers() { function removeAllPopovers() {
for (let popover of document.querySelectorAll('*[aria-describedby*="popover"]')) { for (let popover of document.querySelectorAll('*[aria-describedby*="popover"]')) {
let instance = bootstrap.Popover.getInstance(popover) let instance = bootstrap.Popover.getInstance(popover)