feat(webui): refactor chat module to Preact component and add unit tests

This commit is contained in:
sam committed 2026-09-03 00:21:01 +10:00
1 parent 4e189b605e
commit b6112642db
12 files changed
+2193 -424

No files matched your search

+1
View File
@@ -59,6 +59,7 @@
.chat-bubble-text pre { margin: .6rem 0; padding: .75rem; overflow: auto; border: 1px solid var(--border); border-radius: .5rem; background: color-mix(in srgb, var(--surface) 75%, #000); }
.chat-bubble-text pre code { padding: 0; background: transparent; }
.chat-bubble-text a { color: var(--accent); }
.chat-media-img { max-width: 100%; max-height: 24rem; border-radius: .5rem; margin: .4rem 0; display: block; border: 1px solid var(--border); object-fit: contain; background: var(--surface); cursor: pointer; }
.chat-bubble-text table { width: 100%; margin: .7rem 0; border-collapse: collapse; font-size: .92em; }
.chat-bubble-text th, .chat-bubble-text td { padding: .45rem .6rem; text-align: left; border: 1px solid var(--border); }
.chat-bubble-text th { color: var(--fg); background: color-mix(in srgb, var(--surface) 70%, transparent); font-weight: 700; }
+950 -320
View File
File diff suppressed because it is too large. Load diff
+12 -3
View File
@@ -2,16 +2,25 @@ import { el } from "../base/dom.jsx";
function inlineMarkdown(text) {
const fragment = document.createDocumentFragment();
const pattern = /(~~[^~]+~~|\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\[[^\]]+\]\(https?:\/\/[^\s)]+\)|\*[^*]+\*|_[^_]+_)/g;
const pattern = /(!\[[^\]]*\]\((?:https?:\/\/[^\s)]+|data:image\/[^\s)]+|\/[^\s)]+)\)|~~[^~]+~~|\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\[[^\]]+\]\((?:https?:\/\/[^\s)]+|\/[^\s)]+)\)|\*[^*]+\*|_[^_]+_)/g;
let cursor = 0;
for (const match of text.matchAll(pattern)) {
if (match.index > cursor) fragment.append(document.createTextNode(text.slice(cursor, match.index)));
const value = match[0];
if (value.startsWith("~~")) fragment.append(el("del", value.slice(2, -2)));
if (value.startsWith("![")) {
const img = value.match(/^!\[([^\]]*)\]\((.+)\)$/);
if (img) {
fragment.append(el("img.chat-media-img", {
src: img[2],
alt: img[1] || "Image",
loading: "lazy",
}));
}
} else if (value.startsWith("~~")) fragment.append(el("del", value.slice(2, -2)));
else if (value.startsWith("**") || value.startsWith("__")) fragment.append(el("strong", value.slice(2, -2)));
else if (value.startsWith("`")) fragment.append(el("code", value.slice(1, -1)));
else if (value.startsWith("[")) {
const link = value.match(/^\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)$/);
const link = value.match(/^\[([^\]]+)\]\((https?:\/\/[^\s)]+|\/[^\s)]+)\)$/);
fragment.append(el("a", { href: link[2], target: "_blank", rel: "noreferrer" }, link[1]));
} else fragment.append(el("em", value.slice(1, -1)));
cursor = match.index + value.length;