fix(frontend): 调整审核页布局与报告渲染
This commit is contained in:
102
static/js/app.js
102
static/js/app.js
@@ -153,11 +153,105 @@
|
||||
return escapeHtml(text).replace(/\n/g, "<br>");
|
||||
}
|
||||
|
||||
function renderInlineMarkdown(text) {
|
||||
return escapeHtml(text || "").replace(/\[([^\]]+)\]\(([^)]+)\)/g, function (_match, label, href) {
|
||||
var safeHref = escapeHtml(href);
|
||||
var safeLabel = escapeHtml(label);
|
||||
if (!/^\/[^/\\]/.test(href) && !/^https?:\/\//.test(href)) {
|
||||
return safeLabel;
|
||||
}
|
||||
return '<a href="' + safeHref + '" target="_blank" rel="noopener">' + safeLabel + "</a>";
|
||||
});
|
||||
}
|
||||
|
||||
function renderMarkdownTable(lines, startIndex) {
|
||||
var header = lines[startIndex].trim();
|
||||
var separator = lines[startIndex + 1] ? lines[startIndex + 1].trim() : "";
|
||||
if (header.charAt(0) !== "|" || separator.indexOf("---") === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function cells(line) {
|
||||
return line
|
||||
.trim()
|
||||
.replace(/^\|/, "")
|
||||
.replace(/\|$/, "")
|
||||
.split("|")
|
||||
.map(function (cell) {
|
||||
return cell.trim();
|
||||
});
|
||||
}
|
||||
|
||||
var html = "<table><thead><tr>";
|
||||
cells(header).forEach(function (cell) {
|
||||
html += "<th>" + renderInlineMarkdown(cell) + "</th>";
|
||||
});
|
||||
html += "</tr></thead><tbody>";
|
||||
|
||||
var index = startIndex + 2;
|
||||
while (index < lines.length && lines[index].trim().charAt(0) === "|") {
|
||||
html += "<tr>";
|
||||
cells(lines[index]).forEach(function (cell) {
|
||||
html += "<td>" + renderInlineMarkdown(cell || "-") + "</td>";
|
||||
});
|
||||
html += "</tr>";
|
||||
index += 1;
|
||||
}
|
||||
html += "</tbody></table>";
|
||||
return { html: html, nextIndex: index };
|
||||
}
|
||||
|
||||
function renderBasicMarkdown(text) {
|
||||
var lines = (text || "").split(/\r?\n/);
|
||||
var html = "";
|
||||
var paragraph = [];
|
||||
var index = 0;
|
||||
|
||||
function flushParagraph() {
|
||||
if (!paragraph.length) {
|
||||
return;
|
||||
}
|
||||
html += "<p>" + renderInlineMarkdown(paragraph.join("\n")).replace(/\n/g, "<br>") + "</p>";
|
||||
paragraph = [];
|
||||
}
|
||||
|
||||
while (index < lines.length) {
|
||||
var line = lines[index];
|
||||
var table = renderMarkdownTable(lines, index);
|
||||
if (table) {
|
||||
flushParagraph();
|
||||
html += table.html;
|
||||
index = table.nextIndex;
|
||||
continue;
|
||||
}
|
||||
if (!line.trim()) {
|
||||
flushParagraph();
|
||||
} else {
|
||||
paragraph.push(line);
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
flushParagraph();
|
||||
return html;
|
||||
}
|
||||
|
||||
function renderAssistantContent(text) {
|
||||
if (window.marked && window.DOMPurify) {
|
||||
return window.DOMPurify.sanitize(window.marked.parse(text || ""));
|
||||
}
|
||||
return nl2br(text || "");
|
||||
return renderBasicMarkdown(text || "");
|
||||
}
|
||||
|
||||
function renderExistingAssistantMessages() {
|
||||
document.querySelectorAll(".message.assistant .message-bubble").forEach(function (bubble) {
|
||||
var target = bubble.querySelector(".markdown-content");
|
||||
var raw = bubble.querySelector(".message-raw");
|
||||
if (!target || !raw || target.dataset.rendered === "true") {
|
||||
return;
|
||||
}
|
||||
target.innerHTML = renderAssistantContent(raw.content ? raw.content.textContent : raw.textContent);
|
||||
target.dataset.rendered = "true";
|
||||
});
|
||||
}
|
||||
|
||||
function scrollChatToBottom() {
|
||||
@@ -181,7 +275,10 @@
|
||||
var bubble = document.createElement("div");
|
||||
bubble.className = "message-bubble";
|
||||
|
||||
var text = document.createElement("p");
|
||||
var text = document.createElement(role === "assistant" ? "div" : "p");
|
||||
if (role === "assistant") {
|
||||
text.className = "message-content markdown-content";
|
||||
}
|
||||
text.innerHTML = role === "assistant" ? renderAssistantContent(content) : nl2br(content);
|
||||
bubble.appendChild(text);
|
||||
|
||||
@@ -549,6 +646,7 @@
|
||||
|
||||
syncNodeRailVisibility();
|
||||
bindNodeAnchorClicks();
|
||||
renderExistingAssistantMessages();
|
||||
|
||||
if (chatScroll) {
|
||||
chatScroll.addEventListener("scroll", setActiveNode, { passive: true });
|
||||
|
||||
Reference in New Issue
Block a user