1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-24 03:02:23 +00:00

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

View File

@ -157,7 +157,6 @@ function fetchPreviousMessages() {
}
function receiveFetchedMessages(data) {
console.log(data)
let channel_id = data['channel_id']
let new_messages = data['messages']
@ -237,7 +236,7 @@ function redrawMessages() {
messageContentDiv.setAttribute('data-message-id', message['id'])
lastContentDiv.appendChild(messageContentDiv)
let messageContentSpan = document.createElement('span')
messageContentSpan.innerText = message['content']
messageContentSpan.innerHTML = markdownToHTML(message['content'])
messageContentDiv.appendChild(messageContentSpan)
registerMessageContextMenu(message, messageContentDiv, messageContentSpan)
@ -272,7 +271,7 @@ function redrawMessages() {
messageContentDiv.setAttribute('data-message-id', message['id'])
contentDiv.appendChild(messageContentDiv)
let messageContentSpan = document.createElement('span')
messageContentSpan.innerText = message['content']
messageContentSpan.innerHTML = markdownToHTML(message['content'])
messageContentDiv.appendChild(messageContentSpan)
registerMessageContextMenu(message, messageContentDiv, messageContentSpan)
@ -290,6 +289,26 @@ function redrawMessages() {
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() {
for (let popover of document.querySelectorAll('*[aria-describedby*="popover"]')) {
let instance = bootstrap.Popover.getInstance(popover)