aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-10-16 14:11:11 +0200
committerDanilo M. <danix@danix.xyz>2026-10-16 14:11:11 +0200
commit72d3ff1f0c8c2cadf315214f31312895154cd20e (patch)
tree907e3692c1659264347272f02e4586b5ae7c04f6
parent043bddefebe49a5446c6ff6a0e28eace45d32681 (diff)
downloadquickshell-72d3ff1f0c8c2cadf315214f31312895154cd20e.tar.gz
quickshell-72d3ff1f0c8c2cadf315214f31312895154cd20e.zip
feat(assistant): split messages into prose and fenced code
Qt's markdown renders a fence but gives no per-block hook, so a copy button per code block needs the fences found first. An unclosed fence is open code, which is every fence while a reply streams; a fence inside a list item keeps its indent out of the copied text. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
-rw-r--r--desktop/assistant/Md.js53
-rw-r--r--desktop/assistant/test_md.js51
2 files changed, 104 insertions, 0 deletions
diff --git a/desktop/assistant/Md.js b/desktop/assistant/Md.js
new file mode 100644
index 0000000..fe61522
--- /dev/null
+++ b/desktop/assistant/Md.js
@@ -0,0 +1,53 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+.pragma library
+
+// Splits a message into prose and fenced code, so each code block can carry
+// its own copy button: Qt's markdown renders fences but gives no per-block
+// hook. A fence opens with ``` or ~~~ (three or more) after any indent, as
+// inside a list item, and closes with a run of the same character at least
+// as long. The fence's indent is stripped from its lines. A fence still open
+// at the end, which is normal while a reply streams, is a code segment with
+// open set.
+function split(text) {
+ const out = [];
+ const prose = [];
+ let code = null;
+ const flush = () => {
+ const t = prose.join("\n").trim();
+ if (t) out.push({ kind: "prose", text: t });
+ prose.length = 0;
+ };
+ for (const line of text.split("\n")) {
+ if (code) {
+ const close = /^\s*(`{3,}|~{3,})\s*$/.exec(line);
+ if (close && close[1][0] === code.fence[0] && close[1].length >= code.fence.length) {
+ out.push({ kind: "code", lang: code.lang, text: code.lines.join("\n"), open: false });
+ code = null;
+ } else {
+ code.lines.push(line.startsWith(code.indent) ? line.slice(code.indent.length)
+ : line.trimStart());
+ }
+ continue;
+ }
+ const open = /^(\s*)(`{3,}|~{3,})\s*([^`\s]*)/.exec(line);
+ if (open) {
+ flush();
+ code = { indent: open[1], fence: open[2], lang: open[3], lines: [] };
+ } else {
+ prose.push(line);
+ }
+ }
+ if (code) out.push({ kind: "code", lang: code.lang, text: code.lines.join("\n"), open: true });
+ else flush();
+ return out;
+}
diff --git a/desktop/assistant/test_md.js b/desktop/assistant/test_md.js
new file mode 100644
index 0000000..030cdc9
--- /dev/null
+++ b/desktop/assistant/test_md.js
@@ -0,0 +1,51 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// Run: node desktop/assistant/test_md.js
+// Md.js is a QML library, so its .pragma line is stripped before node
+// evaluates it.
+
+const fs = require("fs");
+const assert = require("assert");
+
+const src = fs.readFileSync(__dirname + "/Md.js", "utf8")
+ .replace(/^\.pragma library$/m, "");
+const { split } = new Function(src + "\nreturn { split };")();
+
+const prose = text => ({ kind: "prose", text });
+const code = (lang, text, open = false) => ({ kind: "code", lang, text, open });
+
+assert.deepStrictEqual(split(""), []);
+assert.deepStrictEqual(split("Just **prose**.\n\nTwo paragraphs."),
+ [prose("Just **prose**.\n\nTwo paragraphs.")]);
+// One fence, with a language, between prose.
+assert.deepStrictEqual(split("Run:\n```bash\nls -l\n```\nDone."),
+ [prose("Run:"), code("bash", "ls -l"), prose("Done.")]);
+// Two fences, no language, blank lines kept inside code.
+assert.deepStrictEqual(split("```\na\n\nb\n```\n```\nc\n```"),
+ [code("", "a\n\nb"), code("", "c")]);
+// Streaming: an unclosed fence is open code.
+assert.deepStrictEqual(split("Here:\n```py\nprint(1)\npri"),
+ [prose("Here:"), code("py", "print(1)\npri", true)]);
+assert.deepStrictEqual(split("```"), [code("", "", true)]);
+// Inline backticks are not a fence.
+assert.deepStrictEqual(split("Use `ls` or ``a`b``."), [prose("Use `ls` or ``a`b``.")]);
+// A fence in a list item: its indent is stripped from the code.
+assert.deepStrictEqual(split("1. Install:\n ```sh\n make\n make install\n ```\n2. Go"),
+ [prose("1. Install:"), code("sh", "make\n make install"), prose("2. Go")]);
+// A shorter or different closing run does not close the fence.
+assert.deepStrictEqual(split("````\n```\n~~~\n````"), [code("", "```\n~~~")]);
+// Tilde fences.
+assert.deepStrictEqual(split("~~~js\nx\n~~~"), [code("js", "x")]);
+// A closing fence with text after it is content, not a close.
+assert.deepStrictEqual(split("```\n``` not\n```"), [code("", "``` not")]);
+
+console.log("ok");