From bd013dbfa5ecf6c4c476ef5e8a656e4916511571 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 21 Jun 2026 18:36:00 -0400 Subject: [PATCH] fix: Remove quote flattening on markdown serialization (#12774) * fix: Remove quote flattening on markdown serialization closes #12773 * PR feedback --- server/commands/documentUpdater.test.ts | 34 ++++++++++++ server/models/helpers/DocumentHelper.test.ts | 55 ++++++++++++++++++++ server/models/helpers/DocumentHelper.tsx | 4 -- shared/editor/lib/markdown/serializer.ts | 5 +- 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/server/commands/documentUpdater.test.ts b/server/commands/documentUpdater.test.ts index 156a961304..0e1cf5b565 100644 --- a/server/commands/documentUpdater.test.ts +++ b/server/commands/documentUpdater.test.ts @@ -419,6 +419,40 @@ describe("documentUpdater", () => { }); }); + it("should patch text retrieved via toMarkdown containing smart quotes and brackets", async () => { + const user = await buildUser(); + const document = await buildDocument({ + teamId: user.teamId, + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "the cat’s “meow” [loud]" }], + }, + ], + }, + }); + + // findText is the exact text a client receives from retrieval. + const findText = await DocumentHelper.toMarkdown(document, { + includeTitle: false, + }); + expect(findText).toBe("the cat’s “meow” [loud]"); + + const result = DocumentHelper.applyMarkdownToDocument( + document, + "quiet", + TextEditMode.Patch, + findText + ); + + expect(result.content!.content![0]).toMatchObject({ + type: "paragraph", + content: [{ type: "text", text: "quiet" }], + }); + }); + it("should throw when findText is not found in document", async () => { const user = await buildUser(); const document = await buildDocument({ diff --git a/server/models/helpers/DocumentHelper.test.ts b/server/models/helpers/DocumentHelper.test.ts index bd5c9dd074..3107d57d79 100644 --- a/server/models/helpers/DocumentHelper.test.ts +++ b/server/models/helpers/DocumentHelper.test.ts @@ -342,6 +342,61 @@ This is a [test paragraph](https://example.net)`, }); describe("toMarkdown", () => { + it("should preserve smart quotes rather than flattening them", async () => { + const document = await buildDocument({ + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "the cat’s “meow”" }], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(document, { + includeTitle: false, + }); + expect(result).toBe("the cat’s “meow”"); + }); + + it("should not escape standalone square brackets", async () => { + const document = await buildDocument({ + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "one [two] three" }], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(document, { + includeTitle: false, + }); + expect(result).toBe("one [two] three"); + }); + + it("should escape literal inline-link syntax with nested brackets", async () => { + const document = await buildDocument({ + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "nested [a[b]c](url) link" }], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(document, { + includeTitle: false, + }); + // Escaped so it is not re-parsed into a link on round-trip. + expect(result).toBe("nested \\[a\\[b]c](url) link"); + }); + it("should export bullet lists inside table cells with br tags", async () => { // Create a document with a table containing a bullet list in a cell // This tests the renderList inTable handling diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 767dd7c108..f3bdde8cf6 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -211,10 +211,6 @@ export class DocumentHelper { const text = serializer .serialize(node) .replace(/(^|\n)\\(\n|$)/g, "\n\n") - .replace(/“/g, '"') - .replace(/”/g, '"') - .replace(/‘/g, "'") - .replace(/’/g, "'") .trim(); if ( diff --git a/shared/editor/lib/markdown/serializer.ts b/shared/editor/lib/markdown/serializer.ts index ff00f0dabd..8512cb9ce2 100644 --- a/shared/editor/lib/markdown/serializer.ts +++ b/shared/editor/lib/markdown/serializer.ts @@ -547,7 +547,10 @@ export class MarkdownSerializerState { // content. If `startOfLine` is true, also escape characters that // has special meaning only at the start of the line. esc(str = "", startOfLine) { - str = str.replace(/[`*\\~[\]]/g, "\\$&"); + str = str.replace(/[`*\\~]/g, "\\$&"); + // Only escape an opening square bracket when the same line later contains a + // `](`, meaning it could otherwise be parsed as an inline link or image + str = str.replace(/\[(?=[^\n]*\]\()/g, "\\$&"); if (startOfLine) { str = str.replace(/^[:#\-*+]/, "\\$&").replace(/^(\d+)\./, "$1\\."); }