add markdown

This commit is contained in:
Christian Rute 2024-10-31 16:33:05 +01:00
parent 4d2e5332d8
commit a7dfbe2ebd
2 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,6 @@
let chatCount = 0; // Zähler für Chat-Sessions
let currentChatId = null; // Aktuell angezeigte Chat-ID
let useMarkdown = true;
document.getElementById('newChatButton').addEventListener('click', () => {
createNewChat();
@ -13,7 +14,14 @@ document.getElementById('sendButton').addEventListener('click', () => {
if (messageText !== '' && currentChatId !== null) { // Stelle sicher, dass eine Chat-ID vorhanden ist
const messageElement = document.createElement('div');
messageElement.classList.add('chat-message', 'user');
messageElement.innerText = messageText;
//messageElement.innerText = messageText;
if (useMarkdown) {
messageElement.innerHTML = renderMarkdown(messageText);
} else {
messageElement.innerText = messageText;
}
chatBox.prepend(messageElement);
// Nachricht speichern
@ -238,6 +246,10 @@ function getLastUserMessage() {
return ''; // Rückgabe eines leeren Strings, falls keine Nachricht gefunden wird
}
function renderMarkdown(content) {
return marked.parse(content);
}
async function stream_api_open_ai(userMessage) {
// Neues div-Element für die Antwort des Chatbots erstellen
const chatBox = document.getElementById('chatBox');
@ -287,8 +299,11 @@ async function stream_api_open_ai(userMessage) {
const token = json.choices[0].delta.content;
result += token;
// Live-Update des Chatbot-Textes
botMessageDiv.textContent = result;
if (useMarkdown) {
botMessageDiv.innerHTML = renderMarkdown(result);
} else {
botMessageDiv.textContent = result;
}
}
} catch (error) {
console.error('Error parsing JSON:', error, 'Received:', jsonStr);

View File

@ -43,7 +43,7 @@
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="js/chat.js"></script>
<script src="js/main.js"></script>
</body>