Skip to content

Commit 92bc9a2

Browse files
committed
fix(web): preserve user list numbers when nested or copied
1 parent 4be9c29 commit 92bc9a2

5 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import "../index.css";
99

1010
import { filesystemEnvironment } from "../state/filesystem";
1111
import { useRightPanelStore } from "../rightPanelStore";
12+
import { remarkEnableOrderedListNumbers } from "@t3tools/shared/markdownPipeline";
1213
import { setMarkdownTaskChecked } from "@t3tools/shared/markdownTaskList";
1314
import { HostImageUrlContext } from "./chat/hostImageUrl";
1415

@@ -161,6 +162,38 @@ function buttonsLabelled(container: HTMLElement, label: string) {
161162
);
162163
}
163164

165+
const SOURCE_NUMBER_PLUGINS = [remarkEnableOrderedListNumbers];
166+
167+
it("keeps decimal markers in nested user lists while documents keep their counter styles", async () => {
168+
setup();
169+
const text = "1. Outer\n 1. Nested\n 7. Skipped\n 1. Deeper\n 4. Fourth";
170+
await using screen = await render(
171+
<div className="grid grid-cols-2 gap-8 p-8">
172+
<section>
173+
<h2>User message</h2>
174+
<ChatMarkdown
175+
cwd={undefined}
176+
text={text}
177+
className="user-message-markdown"
178+
extraRemarkPlugins={SOURCE_NUMBER_PLUGINS}
179+
/>
180+
</section>
181+
<section>
182+
<h2>Markdown preview</h2>
183+
<ChatMarkdown cwd={undefined} text={text} />
184+
</section>
185+
</div>,
186+
);
187+
await expect.element(screen.getByText("User message", { exact: true })).toBeVisible();
188+
const [message, preview] = screen.container.querySelectorAll(".chat-markdown");
189+
expect(
190+
[...message!.querySelectorAll("ol")].map((list) => getComputedStyle(list).listStyleType),
191+
).toEqual(["decimal", "decimal", "decimal"]);
192+
expect(
193+
[...preview!.querySelectorAll("ol")].map((list) => getComputedStyle(list).listStyleType),
194+
).toEqual(["decimal", "lower-alpha", "lower-roman"]);
195+
});
196+
164197
it("shows unavailable audio when a workspace embed has no thread", async () => {
165198
setup();
166199
await using screen = await renderText("![recording](clip.wav)");

‎apps/web/src/components/chat/TimelineUserMessageRow.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ const UserMessageBody = memo(function UserMessageBody(props: {
11931193
cwd={props.markdownCwd}
11941194
threadRef={ctx.threadRef ?? undefined}
11951195
skills={props.skills}
1196-
className="text-foreground"
1196+
className="text-foreground user-message-markdown"
11971197
lineBreaks
11981198
parseRawHtml={false}
11991199
extraRemarkPlugins={USER_MESSAGE_REMARK_PLUGINS}

‎apps/web/src/index.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,10 @@ code {
20122012
list-style-type: lower-roman;
20132013
}
20142014

2015+
.chat-markdown.user-message-markdown ol {
2016+
list-style-type: decimal;
2017+
}
2018+
20152019
.chat-markdown li + li {
20162020
margin-top: 0.25rem;
20172021
}

‎apps/web/src/markdown-clipboard.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ describe("serializeRenderedMarkdownFragment", () => {
197197
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("first line\nsecond line");
198198
});
199199

200+
it("preserves explicit ordered-list values and continues from resets when copying", () => {
201+
const list = new FakeElement("OL", [], { start: "0" }).append(
202+
new FakeElement("LI").append(new FakeText("A")),
203+
new FakeElement("LI", [], { value: "5" }).append(new FakeText("B")),
204+
new FakeElement("LI").append(new FakeText("C")),
205+
new FakeElement("LI", [], { value: "0" }).append(new FakeText("D")),
206+
);
207+
expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe(
208+
"0. A\n5. B\n6. C\n0. D",
209+
);
210+
});
211+
200212
it("keeps fences when a bare list item sits alongside the code block", () => {
201213
const container = new FakeElement("DIV").append(
202214
new FakeElement("UL").append(new FakeElement("LI")),

‎apps/web/src/markdown-clipboard.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,17 @@ function serializeListItem(
144144
}
145145

146146
function serializeList(list: Element, ordered: boolean, inTableCell: boolean): string {
147-
const start = Number.parseInt(list.getAttribute("start") ?? "1", 10) || 1;
147+
const start = Number.parseInt(list.getAttribute("start") ?? "1", 10);
148148
const items = [...list.children].filter((child) => child.tagName === "LI");
149149
if (items.length === 0) return "";
150-
return `${items.map((item, index) => serializeListItem(item, ordered, start + index, inTableCell)).join("\n")}\n\n`;
150+
let next = Number.isNaN(start) ? 1 : start;
151+
return `${items
152+
.map((item) => {
153+
const value = Number.parseInt(item.getAttribute("value") ?? "", 10);
154+
if (ordered && !Number.isNaN(value)) next = value;
155+
return serializeListItem(item, ordered, next++, inTableCell);
156+
})
157+
.join("\n")}\n\n`;
151158
}
152159

153160
function serializeBlockquote(quote: Element, inTableCell: boolean): string {

0 commit comments

Comments
 (0)