Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(comments): address markdown review feedback

Tin18335419dfe5
diff --git a/apps/api/src/activity/controllers/get-activities.ts b/apps/api/src/activity/controllers/get-activities.ts
index 6e5b3c020..7cb51983f 100644
--- a/apps/api/src/activity/controllers/get-activities.ts
+++ b/apps/api/src/activity/controllers/get-activities.ts
@@ -9,7 +9,7 @@ async function getActivitiesFromTaskId(taskId: string) {
});
activities.forEach((x) => {
- if (x.content) {
+ if (x.content && x.type !== "comment") {
x.content = x.content.replace(/\n+/g, "\n");
}
});
diff --git a/apps/api/src/activity/index.ts b/apps/api/src/activity/index.ts
index 38fe79d2d..73cc55a32 100644
--- a/apps/api/src/activity/index.ts
+++ b/apps/api/src/activity/index.ts
@@ -11,6 +11,8 @@ import deleteComment from "./controllers/delete-comment";
import getActivities from "./controllers/get-activities";
import updateComment from "./controllers/update-comment";
+const activityCommentContentSchema = v.pipe(v.string(), v.maxLength(10_000));
+
const activity = new Hono<{
Variables: {
userId: string;
@@ -97,7 +99,7 @@ const activity = new Hono<{
"json",
v.object({
taskId: v.string(),
- comment: v.string(),
+ comment: activityCommentContentSchema,
}),
),
workspaceAccess.fromTaskId(),
@@ -129,7 +131,7 @@ const activity = new Hono<{
"json",
v.object({
activityId: v.string(),
- comment: v.string(),
+ comment: activityCommentContentSchema,
}),
),
workspaceAccess.fromActivity("activityId"),
diff --git a/apps/web/src/lib/normalize-comment-markdown.test.ts b/apps/web/src/lib/normalize-comment-markdown.test.ts
index f33153afd..4f1845156 100644
--- a/apps/web/src/lib/normalize-comment-markdown.test.ts
+++ b/apps/web/src/lib/normalize-comment-markdown.test.ts
@@ -3,22 +3,45 @@ import { normalizeCommentMarkdown } from "./normalize-comment-markdown";
describe("normalizeCommentMarkdown", () => {
it("preserves explicit blank paragraphs", () => {
- expect(
- normalizeCommentMarkdown(
- "This is a test comment\n\n\n\nThis is another paragraph",
- ),
- ).toBe("This is a test comment\n\n\n\nThis is another paragraph");
+ const markdown = "This is a test comment\n\n\n\nThis is another paragraph";
+
+ const result = normalizeCommentMarkdown(markdown);
+
+ expect(result).toBe(markdown);
});
it("preserves the blank paragraph that separates a list from later text", () => {
- expect(normalizeCommentMarkdown("- Item\n\n\n\nNot a list item")).toBe(
- "- Item\n\n\n\nNot a list item",
- );
+ const markdown = "- Item\n\n\n\nNot a list item";
+
+ const result = normalizeCommentMarkdown(markdown);
+
+ expect(result).toBe(markdown);
});
it("normalizes platform newlines and non-breaking spaces", () => {
- expect(normalizeCommentMarkdown("First\r\nSecond&nbsp;\u00A0line")).toBe(
- "First\nSecond line",
+ const markdown = "First\r\nSecond&nbsp;\u00A0line";
+
+ const result = normalizeCommentMarkdown(markdown);
+
+ expect(result).toBe("First\nSecond line");
+ });
+
+ it("preserves non-breaking spaces inside inline code", () => {
+ const markdown = "Text&nbsp; `code&nbsp;\u00A0value` more\u00A0text";
+
+ const result = normalizeCommentMarkdown(markdown);
+
+ expect(result).toBe("Text `code&nbsp;\u00A0value` more text");
+ });
+
+ it("preserves non-breaking spaces inside fenced code blocks", () => {
+ const markdown =
+ "Before&nbsp;\n```html\n<div>&nbsp;\u00A0</div>\n```\nAfter\u00A0";
+
+ const result = normalizeCommentMarkdown(markdown);
+
+ expect(result).toBe(
+ "Before \n```html\n<div>&nbsp;\u00A0</div>\n```\nAfter ",
);
});
});
diff --git a/apps/web/src/lib/normalize-comment-markdown.ts b/apps/web/src/lib/normalize-comment-markdown.ts
index 57d9531a8..baf29c456 100644
--- a/apps/web/src/lib/normalize-comment-markdown.ts
+++ b/apps/web/src/lib/normalize-comment-markdown.ts
@@ -1,6 +1,103 @@
+function normalizeSpaces(markdown: string) {
+ return markdown.replace(/&nbsp;/g, " ").replace(/\u00A0/g, " ");
+}
+
+function findClosingBacktickRun(
+ markdown: string,
+ start: number,
+ delimiterLength: number,
+) {
+ let index = start;
+
+ while (index < markdown.length) {
+ if (markdown[index] !== "`") {
+ index += 1;
+ continue;
+ }
+
+ let runLength = 1;
+ while (markdown[index + runLength] === "`") {
+ runLength += 1;
+ }
+
+ if (runLength === delimiterLength) return index;
+ index += runLength;
+ }
+
+ return -1;
+}
+
+function normalizeInlineMarkdown(markdown: string) {
+ let output = "";
+ let textStart = 0;
+ let index = 0;
+
+ while (index < markdown.length) {
+ if (markdown[index] !== "`") {
+ index += 1;
+ continue;
+ }
+
+ let delimiterLength = 1;
+ while (markdown[index + delimiterLength] === "`") {
+ delimiterLength += 1;
+ }
+
+ const closingIndex = findClosingBacktickRun(
+ markdown,
+ index + delimiterLength,
+ delimiterLength,
+ );
+ if (closingIndex === -1) {
+ index += delimiterLength;
+ continue;
+ }
+
+ output += normalizeSpaces(markdown.slice(textStart, index));
+ output += markdown.slice(index, closingIndex + delimiterLength);
+ index = closingIndex + delimiterLength;
+ textStart = index;
+ }
+
+ return output + normalizeSpaces(markdown.slice(textStart));
+}
+
export function normalizeCommentMarkdown(markdown: string) {
- return markdown
- .replace(/\r\n/g, "\n")
- .replace(/&nbsp;/g, " ")
- .replace(/\u00A0/g, " ");
+ const normalizedNewlines = markdown.replace(/\r\n/g, "\n");
+ const lines = normalizedNewlines.match(/[^\n]*(?:\n|$)/g) ?? [];
+ let fence: { marker: string; length: number } | null = null;
+
+ return lines
+ .map((lineWithEnding) => {
+ const line = lineWithEnding.endsWith("\n")
+ ? lineWithEnding.slice(0, -1)
+ : lineWithEnding;
+
+ if (fence) {
+ const closingFence = /^ {0,3}(`{3,}|~{3,})[\t ]*$/.exec(line);
+ if (
+ closingFence &&
+ closingFence[1][0] === fence.marker &&
+ closingFence[1].length >= fence.length
+ ) {
+ fence = null;
+ }
+ return lineWithEnding;
+ }
+
+ const openingFence = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
+ if (
+ openingFence &&
+ (openingFence[1][0] === "~" || !openingFence[2].includes("`"))
+ ) {
+ fence = {
+ marker: openingFence[1][0],
+ length: openingFence[1].length,
+ };
+ return lineWithEnding;
+ }
+
+ return normalizeInlineMarkdown(lineWithEnding);
+ })
+ .join("");
}
diff --git a/tests/api-integration/comment.test.ts b/tests/api-integration/comment.test.ts
index 981456a59..93f2d235f 100644
--- a/tests/api-integration/comment.test.ts
+++ b/tests/api-integration/comment.test.ts
@@ -35,10 +35,12 @@ describe("API integration: task comments", () => {
mockAuthenticatedSession(member.user);
const { app } = createApp();
+ const formattedComment =
+ "First paragraph\n\n\n\n- List item\n\n\n\nAfter list";
const uiResponse = await app.request("/api/activity/comment", {
method: "POST",
headers: { "content-type": "application/json" },
- body: JSON.stringify({ taskId: task.id, comment: "Created in the UI" }),
+ body: JSON.stringify({ taskId: task.id, comment: formattedComment }),
});
expect(uiResponse.status).toBe(200);
@@ -46,7 +48,7 @@ describe("API integration: task comments", () => {
expect(commentApiResponse.status).toBe(200);
await expect(commentApiResponse.json()).resolves.toEqual([
expect.objectContaining({
- content: "Created in the UI",
+ content: formattedComment,
taskId: task.id,
}),
]);
@@ -67,7 +69,7 @@ describe("API integration: task comments", () => {
expect(activities).toEqual(
expect.arrayContaining([
expect.objectContaining({
- content: "Created in the UI",
+ content: formattedComment,
type: "comment",
}),
expect.objectContaining({
@@ -94,4 +96,52 @@ describe("API integration: task comments", () => {
.where(eq(schema.commentTable.taskId, task.id));
expect(legacyComments).toHaveLength(0);
});
+
+ it("rejects comments over the activity API length limit", async () => {
+ const member = await createWorkspaceMember();
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({
+ projectId: project.id,
+ title: "Comment length limit",
+ status: "to-do",
+ columnId: columns.todo.id,
+ priority: "medium",
+ number: 1,
+ position: 1,
+ })
+ .returning();
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const createResponse = await app.request("/api/activity/comment", {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ taskId: task.id, comment: "Short comment" }),
+ });
+ expect(createResponse.status).toBe(200);
+ const createdComment = (await createResponse.json()) as { id: string };
+
+ const overlongComment = "x".repeat(10_001);
+ const createOverLimitResponse = await app.request("/api/activity/comment", {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ taskId: task.id, comment: overlongComment }),
+ });
+ expect(createOverLimitResponse.status).toBe(400);
+
+ const updateOverLimitResponse = await app.request("/api/activity/comment", {
+ method: "PUT",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ activityId: createdComment.id,
+ comment: overlongComment,
+ }),
+ });
+ expect(updateOverLimitResponse.status).toBe(400);
+ });
});