Skip to content

Commit 3d0b173

Browse files
committed
refactor: enhance bot reply handling and add Markdown to HTML conversion
1 parent 3870854 commit 3d0b173

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

js/widgets/reflection.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ class ReflectionMatrix {
6565
* Initializes the reflection widget.
6666
*/
6767

68-
// download current summary
69-
// download analysis as text file
70-
// .md to html conversion
7168
init(activity) {
7269
this.activity = activity;
7370
this.isOpen = true;
@@ -261,7 +258,7 @@ class ReflectionMatrix {
261258

262259
if (data && !data.error) {
263260
this.inputContainer.style.display = "flex";
264-
this.botReplyDiv(data, false);
261+
this.botReplyDiv(data, false, false);
265262
} else {
266263
this.activity.errorMsg(_(data.error), 3000);
267264
}
@@ -328,10 +325,9 @@ class ReflectionMatrix {
328325
if (this.chatHistory.length < 10) return;
329326
this.showTypingIndicator();
330327
const data = await this.generateAnalysis();
331-
console.log(data.analysis);
332328
this.hideTypingIndicator();
333329
if (data) {
334-
this.botReplyDiv(data, false);
330+
this.botReplyDiv(data, false, true);
335331
}
336332
await this.saveReport(data);
337333
}
@@ -365,7 +361,7 @@ class ReflectionMatrix {
365361
* @param {boolean} user_query - Flag indicating if the message is from the user.
366362
* @returns {Promise<void>}
367363
*/
368-
async botReplyDiv(message, user_query = true) {
364+
async botReplyDiv(message, user_query = true, md = false, download = false) {
369365
let reply;
370366
// check if message is from user or bot
371367
if (user_query === true) {
@@ -402,8 +398,13 @@ class ReflectionMatrix {
402398
senderName.innerText = this.mentorsMap[this.AImentor];
403399

404400
const botReply = document.createElement("div");
405-
botReply.innerText = reply.response;
406401

402+
if (md) {
403+
botReply.innerHTML = this.mdToHTML(reply.response);
404+
} else {
405+
botReply.innerText = reply.response;
406+
}
407+
407408
messageContainer.appendChild(senderName);
408409
messageContainer.appendChild(botReply);
409410

@@ -531,4 +532,28 @@ class ReflectionMatrix {
531532
document.body.removeChild(a);
532533
URL.revokeObjectURL(url);
533534
}
535+
536+
mdToHTML(md) {
537+
let html = md;
538+
539+
// Headings
540+
html = html.replace(/^###### (.*$)/gim, "<h6>$1</h6>");
541+
html = html.replace(/^##### (.*$)/gim, "<h5>$1</h5>");
542+
html = html.replace(/^#### (.*$)/gim, "<h4>$1</h4>");
543+
html = html.replace(/^### (.*$)/gim, "<h3>$1</h3>");
544+
html = html.replace(/^## (.*$)/gim, "<h2>$1</h2>");
545+
html = html.replace(/^# (.*$)/gim, "<h1>$1</h1>");
546+
547+
// Bold & Italic
548+
html = html.replace(/\*\*(.*?)\*\*/gim, "<b>$1</b>");
549+
html = html.replace(/\*(.*?)\*/gim, "<i>$1</i>");
550+
551+
// Links
552+
html = html.replace(/\[(.*?)\]\((.*?)\)/gim, "<a href='$2' target='_blank'>$1</a>");
553+
554+
// Line breaks
555+
html = html.replace(/\n/gim, "<br>");
556+
557+
return html.trim();
558+
}
534559
}

0 commit comments

Comments
 (0)