diff --git a/apps/mobile/src/assistant/markdown.test.ts b/apps/mobile/src/assistant/markdown.test.ts
index 64a1cdde..ceb1b725 100644
--- a/apps/mobile/src/assistant/markdown.test.ts
+++ b/apps/mobile/src/assistant/markdown.test.ts
@@ -145,6 +145,19 @@ describe("parseInline", () => {
]);
});
+ test("a
is a line break, also inside an italic span", () => {
+ expect(parseInline("*one,
two
three*")).toEqual([
+ { kind: "italic", text: "one,\ntwo\nthree" },
+ ]);
+ });
+
+ test("a
in a code span stays literal", () => {
+ expect(parseInline("`a
b`
c")).toEqual([
+ { kind: "code", text: "a
b" },
+ { kind: "text", text: "\nc" },
+ ]);
+ });
+
test("a link that is not a web link does not open", () => {
expect(parseInline("[x](javascript:alert(1))")[0]).toMatchObject({
kind: "link",
diff --git a/apps/mobile/src/assistant/markdown.ts b/apps/mobile/src/assistant/markdown.ts
index d56c3d4a..d255def8 100644
--- a/apps/mobile/src/assistant/markdown.ts
+++ b/apps/mobile/src/assistant/markdown.ts
@@ -332,7 +332,12 @@ const INLINE =
* another. */
export const parseInline = (text: string): Inline[] => {
const parts: Inline[] = [];
- for (const part of text.split(INLINE)) {
+ // Models write `
` for a line break. Replace it after the split, so that
+ // a `*...*` span over a `
` stays one span.
+ for (const raw of text.split(INLINE)) {
+ const part = raw.startsWith("`")
+ ? raw
+ : raw.replace(/
\n?/gi, "\n");
if (part === "") continue;
if (part.startsWith("`") && part.endsWith("`") && part.length > 2) {
parts.push({ kind: "code", text: part.slice(1, -1) });
diff --git a/apps/web/src/components/assistant/answer-br.test.tsx b/apps/web/src/components/assistant/answer-br.test.tsx
new file mode 100644
index 00000000..55c64522
--- /dev/null
+++ b/apps/web/src/components/assistant/answer-br.test.tsx
@@ -0,0 +1,34 @@
+// A model's `
` is a line break in a table cell, and literal in a code span.
+
+import { afterAll, beforeAll, describe, expect, test } from "bun:test";
+
+import { GlobalRegistrator } from "@happy-dom/global-registrator";
+import { renderToStaticMarkup } from "react-dom/server";
+
+let html: (text: string) => string;
+
+beforeAll(async () => {
+ // `answer.tsx` reads `window` when it loads, so import it after the fake DOM.
+ GlobalRegistrator.register();
+ const { Answer } = await import("./answer.tsx");
+ html = (text) => renderToStaticMarkup(
", () => {
+ test("a table cell keeps the break and shows it", () => {
+ const out = html("| A | B |\n|---|---|\n| one
two | x |");
+ expect(out).toContain("one\ntwo");
+ expect(out).toContain("whitespace-pre");
+ expect(out).not.toContain("<br");
+ });
+
+ test("a code span keeps the tag literal", () => {
+ const out = html("see `a
b` here");
+ expect(out).toContain("a<br>b");
+ expect(out).not.toContain("a\nb");
+ });
+});
diff --git a/apps/web/src/components/assistant/answer.tsx b/apps/web/src/components/assistant/answer.tsx
index f745de54..8ef3aec4 100644
--- a/apps/web/src/components/assistant/answer.tsx
+++ b/apps/web/src/components/assistant/answer.tsx
@@ -141,6 +141,8 @@ const revealWords = (part: string, at: number) => {
));
};
+const BR = /
\n?/gi;
+
/** Inline `code`, **bold**, *italic*, [links](…) and `[[date]]` citations, in
* one pass so none of them can swallow another.
*
@@ -152,9 +154,13 @@ const revealWords = (part: string, at: number) => {
* re-splits the line around itself without changing how many words are on it,
* because the marker's characters belong to the word they are attached to. */
const renderInline = (text: string, reveal = false) => {
- const parts = text.split(
- /(`[^`]+`|\*\*[^*]+\*\*|\*[^*\n]+\*|\[\[\d{4}-\d{2}-\d{2}\]\]|\[[^\]\n]+\]\([^)\s]+\))/g,
- );
+ // Models write `
` for a line break. Replace it after the split, so that
+ // a `*...*` span over a `
` stays one span.
+ const parts = text
+ .split(
+ /(`[^`]+`|\*\*[^*]+\*\*|\*[^*\n]+\*|\[\[\d{4}-\d{2}-\d{2}\]\]|\[[^\]\n]+\]\([^)\s]+\))/g,
+ )
+ .map((part) => (part.startsWith("`") ? part : part.replace(BR, "\n")));
let cursor = 0;
return parts.map((part, index) => {
const at = cursor;
@@ -307,7 +313,8 @@ function AnswerTable({
return [
alignClass[column?.align ?? "left"],
column?.numeric === true ? "tabular-nums" : "",
- column?.wraps === false ? "whitespace-nowrap" : "",
+ // Both keep a `
` break. Only `pre` stops a wrap.
+ column?.wraps === false ? "whitespace-pre" : "whitespace-pre-line",
// A floor under the PROSE column only, for the phone: five columns
// cannot all fit there, and prose is the only one allowed to shrink, so
// it collapses into a two-word ribbon forty lines deep while every short
@@ -560,7 +567,7 @@ export function Answer({
return (
- Shared by {data.sharedBy.name} on
+ Shared by {data.sharedBy.name} on