Konfigurerar parser. Städat upp från bortkommenterad kod.
This commit is contained in:
@@ -3,7 +3,13 @@
|
|||||||
const chatbox = document.getElementById('chatbox');
|
const chatbox = document.getElementById('chatbox');
|
||||||
const userInput = document.getElementById('userInput');
|
const userInput = document.getElementById('userInput');
|
||||||
|
|
||||||
const parser = window.markdownit();
|
const parser = window.markdownit({
|
||||||
|
linkify: true,
|
||||||
|
strikethrough: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.enable(['table']);
|
||||||
|
|
||||||
|
|
||||||
// const html = markdownIt.use({
|
// const html = markdownIt.use({
|
||||||
// // You can customize the parser options here, e.g., enable/disable certain features
|
// // You can customize the parser options here, e.g., enable/disable certain features
|
||||||
@@ -42,18 +48,6 @@ function sendMessage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function customRenderer(markdownText) {
|
|
||||||
// Use a regex pattern to match LaTeX code (e.g. $$...$$ or $...$)
|
|
||||||
const latexPattern = /(?:\$\$|\\\[)(.*?)?(?:\$\$|\\\])/g;
|
|
||||||
|
|
||||||
// Replace each occurrence of LaTeX code with an HTML span element
|
|
||||||
const html = markdownText.replace(latexPattern, (match, p1) => {
|
|
||||||
return `<span class="latex">${p1}</span>`;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return the rendered HTML
|
|
||||||
return html;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Define a function to render a message in the chatbox with a specific class name
|
// Define a function to render a message in the chatbox with a specific class name
|
||||||
@@ -67,20 +61,18 @@ function renderMessage(text, className) {
|
|||||||
// // Set the text content of the element to the message text
|
// // Set the text content of the element to the message text
|
||||||
// messageElement.textContent = text;
|
// messageElement.textContent = text;
|
||||||
|
|
||||||
// Parse Markdown text into HTML and set it as the innerHTML of the element
|
|
||||||
// messageElement.innerHTML = marked.parse(text);
|
|
||||||
|
|
||||||
// Use the markdown-it parser
|
// Use the markdown-it parser
|
||||||
const html = parser.render(text);
|
const html = parser.render(text);
|
||||||
messageElement.innerHTML = html;
|
messageElement.innerHTML = html;
|
||||||
|
|
||||||
// Typeset math
|
|
||||||
// Append the message element to the chatbox immediately
|
// Append the message element to the chatbox immediately
|
||||||
chatbox.appendChild(messageElement);
|
// chatbox.appendChild(messageElement);
|
||||||
|
|
||||||
// Typeset math in the message element
|
// Typeset math in the message element
|
||||||
MathJax.typesetPromise([messageElement]).then(() => {
|
MathJax.typesetPromise([messageElement]).then(() => {
|
||||||
// No need to append anything here, it's already appended above
|
// No need to append anything here, it's already appended above
|
||||||
|
chatbox.appendChild(messageElement);
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user