Skip to content

Commit 114efb5

Browse files
authored
Update script.js
1 parent f3cbae8 commit 114efb5

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

script.js

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,36 @@ function streamMessage(cssClass, text) {
7676
msg.classList.add("message", cssClass);
7777
chat.appendChild(msg);
7878

79-
// Genummerde lijst detectie: 1. ... 2. ... 3. ...
80-
const numberedPattern = /(?:^|\s)(\d+)\.\s+([^\d\.\n]+)/g;
81-
let matches = [...text.matchAll(numberedPattern)];
82-
83-
if (matches.length >= 2) {
84-
const list = document.createElement("ol");
85-
matches.forEach(match => {
86-
const li = document.createElement("li");
87-
li.textContent = match[2].trim();
88-
list.appendChild(li);
89-
});
90-
msg.appendChild(list);
91-
return;
79+
const lines = text.split("\n").filter(line => line.trim() !== "");
80+
81+
const isNumberedList = lines.length > 1 && lines.every(line => /^\d+\.\s+/.test(line.trim()));
82+
const isBulletedList = lines.length > 1 && lines.every(line => /^[-*]\s+/.test(line.trim()));
83+
84+
if (isNumberedList || isBulletedList) {
85+
const listElement = document.createElement(isNumberedList ? "ol" : "ul");
86+
msg.appendChild(listElement);
87+
let i = 0;
88+
89+
const interval = setInterval(() => {
90+
if (i < lines.length) {
91+
const li = document.createElement("li");
92+
li.textContent = lines[i].replace(/^(\d+\.\s+|[-*]\s+)/, "").trim();
93+
listElement.appendChild(li);
94+
chat.scrollTop = chat.scrollHeight;
95+
i++;
96+
} else {
97+
clearInterval(interval);
98+
}
99+
}, 200);
100+
} else {
101+
let index = 0;
102+
const interval = setInterval(() => {
103+
if (index < text.length) {
104+
msg.textContent += text.charAt(index++);
105+
chat.scrollTop = chat.scrollHeight;
106+
} else {
107+
clearInterval(interval);
108+
}
109+
}, 15);
92110
}
93-
94-
// Bulletlijst detectie: - ... of • ...
95-
const bulletPattern = /(?:^|\s)[\-*]\s+([^\-\n]+)/g;
96-
matches = [...text.matchAll(bulletPattern)];
97-
98-
if (matches.length >= 2) {
99-
const list = document.createElement("ul");
100-
matches.forEach(match => {
101-
const li = document.createElement("li");
102-
li.textContent = match[1].trim();
103-
list.appendChild(li);
104-
});
105-
msg.appendChild(list);
106-
return;
107-
}
108-
109-
// Geen lijst → toon als streaming tekst
110-
let index = 0;
111-
const interval = setInterval(() => {
112-
if (index < text.length) {
113-
msg.textContent += text.charAt(index++);
114-
chat.scrollTop = chat.scrollHeight;
115-
} else {
116-
clearInterval(interval);
117-
}
118-
}, 15);
119111
}

0 commit comments

Comments
 (0)