Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/mobile/src/assistant/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ describe("parseInline", () => {
]);
});

test("a <br> is a line break, also inside an italic span", () => {
expect(parseInline("*one,<br>two<BR />three*")).toEqual([
{ kind: "italic", text: "one,\ntwo\nthree" },
]);
});

test("a <br> in a code span stays literal", () => {
expect(parseInline("`a<br>b`<br>c")).toEqual([
{ kind: "code", text: "a<br>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",
Expand Down
7 changes: 6 additions & 1 deletion apps/mobile/src/assistant/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<br>` for a line break. Replace it after the split, so that
// a `*...*` span over a `<br>` stays one span.
for (const raw of text.split(INLINE)) {
const part = raw.startsWith("`")
? raw
: raw.replace(/<br\s*\/?>\n?/gi, "\n");
if (part === "") continue;
if (part.startsWith("`") && part.endsWith("`") && part.length > 2) {
parts.push({ kind: "code", text: part.slice(1, -1) });
Expand Down
34 changes: 34 additions & 0 deletions apps/web/src/components/assistant/answer-br.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// A model's `<br>` 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(<Answer text={text} />);
});

afterAll(async () => {
await GlobalRegistrator.unregister();
});

describe("Answer <br>", () => {
test("a table cell keeps the break and shows it", () => {
const out = html("| A | B |\n|---|---|\n| one<br>two | x |");
expect(out).toContain("one\ntwo");
expect(out).toContain("whitespace-pre");
expect(out).not.toContain("&lt;br");
});

test("a code span keeps the tag literal", () => {
const out = html("see `a<br>b` here");
expect(out).toContain("a&lt;br&gt;b");
expect(out).not.toContain("a\nb");
});
});
25 changes: 18 additions & 7 deletions apps/web/src/components/assistant/answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ const revealWords = (part: string, at: number) => {
));
};

const BR = /<br\s*\/?>\n?/gi;

/** Inline `code`, **bold**, *italic*, [links](…) and `[[date]]` citations, in
* one pass so none of them can swallow another.
*
Expand All @@ -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 `<br>` for a line break. Replace it after the split, so that
// a `*...*` span over a `<br>` 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;
Expand Down Expand Up @@ -307,7 +313,8 @@ function AnswerTable({
return [
alignClass[column?.align ?? "left"],
column?.numeric === true ? "tabular-nums" : "",
column?.wraps === false ? "whitespace-nowrap" : "",
// Both keep a `<br>` 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
Expand Down Expand Up @@ -560,7 +567,7 @@ export function Answer({
return (
<div
key={key}
className="mt-1 font-medium text-ink first:mt-0 [&:not(:first-child)]:pt-0.5"
className="mt-1 whitespace-pre-line font-medium text-ink first:mt-0 [&:not(:first-child)]:pt-0.5"
>
{renderInline(block.text, reveal)}
</div>
Expand All @@ -578,7 +585,9 @@ export function Answer({
<span className="shrink-0 text-ink-3 tabular-nums">
{itemIndex + 1}.
</span>
<span className="min-w-0">{renderInline(item, reveal)}</span>
<span className="min-w-0 whitespace-pre-line">
{renderInline(item, reveal)}
</span>
</li>
))}
</ol>
Expand All @@ -597,7 +606,9 @@ export function Answer({
aria-hidden
className="mt-[7px] size-1 shrink-0 rounded-full bg-ink-3"
/>
<span className="min-w-0">{renderInline(item, reveal)}</span>
<span className="min-w-0 whitespace-pre-line">
{renderInline(item, reveal)}
</span>
</li>
))}
</ul>
Expand Down
25 changes: 14 additions & 11 deletions apps/web/src/routes/$orgSlug/assistant_/shared/$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,20 @@ function SharedChatPage() {
<PageFull>
<div className="mx-auto flex w-full max-w-[46rem] flex-col gap-6 px-4 py-6">
<div className="flex flex-wrap items-center gap-3 rounded-card bg-inset px-3.5 py-3 text-sm">
<InfoIcon weight="regular" className="size-4 shrink-0 text-ink-2" />
<Avatar size="sm">
{data.sharedBy.image && (
<AvatarImage src={data.sharedBy.image} alt="" />
)}
<AvatarFallback>{data.sharedBy.name.slice(0, 1)}</AvatarFallback>
</Avatar>
<p className="min-w-0 flex-1">
Shared by {data.sharedBy.name} on <When date={data.sharedAt} />. It
can show data that you cannot normally see.
</p>
{/* The min width wraps the buttons to their own row on a phone. */}
<div className="flex min-w-[min(100%,20rem)] flex-1 items-center gap-3">
<InfoIcon weight="regular" className="size-4 shrink-0 text-ink-2" />
<Avatar size="sm">
{data.sharedBy.image && (
<AvatarImage src={data.sharedBy.image} alt="" />
)}
<AvatarFallback>{data.sharedBy.name.slice(0, 1)}</AvatarFallback>
</Avatar>
<p className="min-w-0 flex-1">
Shared by {data.sharedBy.name} on <When date={data.sharedAt} />.
It can show data that you cannot normally see.
</p>
</div>
<OpenInApp orgSlug={orgSlug} id={id} />
{isAdmin && <StopSharing orgSlug={orgSlug} id={id} />}
</div>
Expand Down
Loading