Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep code indentation when an HTML file is attached to a chat (#12312)

Nilay6e8a82f30e43
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.ts
index f0279a00b66..47ea5444a1b 100644
--- a/studio/frontend/src/features/chat/attachment-content.ts
+++ b/studio/frontend/src/features/chat/attachment-content.ts
@@ -1178,12 +1178,19 @@ export function extractHtmlAttachmentText(html: string): string {
for (const el of doc.querySelectorAll("script, style, noscript, template")) {
el.remove();
}
- return normalizeExtractedText(collectHtmlBlockText(doc.body));
+ const preformatted: string[] = [];
+ return normalizeExtractedText(collectHtmlBlockText(doc.body, preformatted))
+ .split("\u0000")
+ .map((text, index) => text + (preformatted[index] ?? ""))
+ .join("");
}
/** Text with the line structure the source had. `textContent` runs a whole page together, so
* every block-level element and every `<br>` contributes a break of its own. */
-function collectHtmlBlockText(node: Node | null): string {
+function collectHtmlBlockText(
+ node: Node | null,
+ preformatted?: string[],
+): string {
if (!node) {
return "";
}
@@ -1200,8 +1207,21 @@ function collectHtmlBlockText(node: Node | null): string {
return "\n";
}
+ if (tag === "pre" && preformatted) {
+ const code = Array.from(element.childNodes)
+ .map((child) => collectHtmlBlockText(child))
+ .join("")
+ .replace(/^(?:[^\S\n]*\n)+/, "")
+ .trimEnd();
+ if (!code.trim()) {
+ return "\n";
+ }
+ preformatted.push(code);
+ return "\n\u0000\n";
+ }
+
const text = Array.from(element.childNodes)
- .map(collectHtmlBlockText)
+ .map((child) => collectHtmlBlockText(child, preformatted))
.join("");
return HTML_BLOCK_TAGS.has(tag) ? `\n${text}\n` : text;
}
diff --git a/studio/frontend/tests/attachment-preview-text.test.ts b/studio/frontend/tests/attachment-preview-text.test.ts
index 55030bf2494..cb1f982a2a8 100644
--- a/studio/frontend/tests/attachment-preview-text.test.ts
+++ b/studio/frontend/tests/attachment-preview-text.test.ts
@@ -307,6 +307,68 @@ test("extractHtmlAttachmentText keeps the line structure of the page", async ()
);
});
+test("extractHtmlAttachmentText keeps the indentation of preformatted code", async () => {
+ const extracted = await withStubDom(
+ () =>
+ element(
+ "body",
+ element("h1", textNode("Totals")),
+ element(
+ "pre",
+ textNode("def total(items):\n s = 0\n"),
+ element("span", textNode(" for x in items:\n s += x\n")),
+ textNode(" return s\n"),
+ ),
+ element("p", textNode("Done here")),
+ ),
+ () => extractHtmlAttachmentText("<html/>"),
+ );
+
+ assert.equal(
+ extracted,
+ "Totals\n\ndef total(items):\n s = 0\n for x in items:\n s += x\n return s\n\nDone here",
+ );
+});
+
+test("extractHtmlAttachmentText adds no blank lines for an empty preformatted block", async () => {
+ const between = await withStubDom(
+ () =>
+ element(
+ "body",
+ element("p", textNode("X")),
+ element("pre", textNode(" \n ")),
+ element("p", textNode("Y")),
+ ),
+ () => extractHtmlAttachmentText("<html/>"),
+ );
+ const last = await withStubDom(
+ () =>
+ element(
+ "body",
+ element("p", textNode("X")),
+ element("pre", textNode("\n")),
+ ),
+ () => extractHtmlAttachmentText("<html/>"),
+ );
+
+ assert.equal(between, "X\n\nY");
+ assert.equal(last, "X");
+});
+
+test("extractHtmlAttachmentText drops blank lines leading a preformatted block", async () => {
+ const extracted = await withStubDom(
+ () =>
+ element(
+ "body",
+ element("pre", textNode("\n \n first\n second\n")),
+ element("p", textNode("Z")),
+ ),
+ () => extractHtmlAttachmentText("<html/>"),
+ );
+
+ assert.equal(extracted, " first\n second\n\nZ");
+});
+
test("isAudioAttachment matches by MIME and by extension", () => {
assert.equal(isAudioAttachment("clip.m4a", ""), true);
assert.equal(isAudioAttachment("clip", "audio/webm"), true);