Skip to content
Merged
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
23 changes: 21 additions & 2 deletions app/_utils/client-parser-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const parseChecklistContent = (
(line) => line.trim().startsWith("- [") || /^\s*- \[/.test(line),
);

let globalItemCounter = 0;
const generateItemId = (level: number): string =>
`${id}-${level}-${globalItemCounter++}`;

const buildNestedItems = (
lines: string[],
startIndex: number = 0,
Expand Down Expand Up @@ -164,7 +168,7 @@ export const parseChecklistContent = (
});

item = {
id: (itemMetadata.id as string) || `${id}-${currentItemIndex}`,
id: (itemMetadata.id as string) || generateItemId(parentLevel),
text: itemText,
completed,
order: currentItemIndex,
Expand Down Expand Up @@ -206,7 +210,7 @@ export const parseChecklistContent = (
}

item = {
id: itemMetadata.id || `${id}-${currentItemIndex}`,
id: itemMetadata.id || generateItemId(parentLevel),
text: itemText,
completed,
order: currentItemIndex,
Expand Down Expand Up @@ -246,6 +250,21 @@ export const parseChecklistContent = (

const { items } = buildNestedItems(itemLines, 0, 0, 0);

const dedupeItemIds = (itemList: Item[], seen: Set<string>): void => {
itemList.forEach((item) => {
if (item.id && seen.has(item.id)) {
item.id = generateItemId(0);
}
if (item.id) {
seen.add(item.id);
}
if (item.children && item.children.length > 0) {
dedupeItemIds(item.children, seen);
}
});
};
dedupeItemIds(items, new Set<string>());

let statuses = undefined;
if (metadata.statuses && Array.isArray(metadata.statuses)) {
statuses = metadata.statuses;
Expand Down
62 changes: 62 additions & 0 deletions tests/utils/client-parser-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect } from "vitest";
import { parseChecklistContent } from "@/app/_utils/client-parser-utils";
import { Item } from "@/app/_types";

const collectIds = (items: Item[], acc: string[] = []): string[] => {
items.forEach((item) => {
acc.push(item.id);
if (item.children && item.children.length > 0) {
collectIds(item.children, acc);
}
});
return acc;
};

describe("parseChecklistContent — unique item IDs", () => {
it("assigns unique IDs to every item across nested groups (regression for #501)", () => {
const lines: string[] = [];
for (let g = 1; g <= 4; g++) {
lines.push(`- [ ] Group ${g}`);
for (let i = 1; i <= 5; i++) {
lines.push(` - [ ] Item ${g}.${i}`);
}
}
const content = lines.join("\n");

const { items } = parseChecklistContent(content, "my-list");
const ids = collectIds(items);

expect(ids).toHaveLength(4 + 4 * 5);
expect(new Set(ids).size).toBe(ids.length);
});

it("preserves IDs stored in inline item metadata", () => {
const content = [
"- [ ] Item with stored id | metadata:{\"id\":\"stored-id-1\"}",
"- [ ] Item without stored id",
].join("\n");

const { items } = parseChecklistContent(content, "my-list");

expect(items[0].id).toBe("stored-id-1");
expect(items[1].id).not.toBe("stored-id-1");
expect(items[1].id).toBeTruthy();
});

it("dedupes IDs from already-broken imported files", () => {
const content = [
"- [ ] Group A | metadata:{\"id\":\"file-0\"}",
" - [ ] Item A1 | metadata:{\"id\":\"file-0\"}",
" - [ ] Item A2 | metadata:{\"id\":\"file-1\"}",
"- [ ] Group B | metadata:{\"id\":\"file-1\"}",
" - [ ] Item B1 | metadata:{\"id\":\"file-2\"}",
].join("\n");

const { items } = parseChecklistContent(content, "file");
const ids = collectIds(items);

expect(new Set(ids).size).toBe(ids.length);
expect(items[0].id).toBe("file-0");
expect(items[0].children?.[0].id).not.toBe("file-0");
});
});
Loading