diff --git a/server/models/helpers/DocumentHelper.test.ts b/server/models/helpers/DocumentHelper.test.ts index f333a581e7..2f4e8964b1 100644 --- a/server/models/helpers/DocumentHelper.test.ts +++ b/server/models/helpers/DocumentHelper.test.ts @@ -1,5 +1,5 @@ import Revision from "@server/models/Revision"; -import { buildDocument } from "@server/test/factories"; +import { buildCollection, buildDocument } from "@server/test/factories"; import { ChangesetHelper } from "@shared/editor/lib/ChangesetHelper"; import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper"; import { DocumentHelper } from "./DocumentHelper"; @@ -608,6 +608,64 @@ This is a [test paragraph](https://example.net)`, expect(result).toContain("[ ] todo"); expect(result).toContain("[x] done"); }); + + it("should include collection title by default", async () => { + const collection = await buildCollection({ + name: "Test Collection", + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "Collection description" }], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(collection); + expect(result).toContain("# Test Collection"); + expect(result).toContain("Collection description"); + }); + + it("should include collection emoji icon in title", async () => { + const collection = await buildCollection({ + name: "Test Collection", + icon: "📚", + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "Collection description" }], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(collection); + expect(result).toContain("# 📚 Test Collection"); + }); + + it("should not include collection title when includeTitle is false", async () => { + const collection = await buildCollection({ + name: "Test Collection", + icon: "📚", + content: { + type: "doc", + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "Collection description" }], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(collection, { + includeTitle: false, + }); + expect(result).not.toContain("# "); + expect(result).not.toContain("Test Collection"); + expect(result).toContain("Collection description"); + }); }); describe("toPlainText", () => { diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 4ff0511561..18c4704506 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -188,20 +188,16 @@ export class DocumentHelper { .replace(/’/g, "'") .trim(); - if (document instanceof Collection) { - return text; - } - if ( - (document instanceof Document || document instanceof Revision) && + (document instanceof Collection || + document instanceof Document || + document instanceof Revision) && options?.includeTitle !== false ) { const iconType = determineIconType(document.icon); - - const title = `${iconType === IconType.Emoji ? document.icon + " " : ""}${ - document.title - }`; - + const name = + document instanceof Collection ? document.name : document.title; + const title = `${iconType === IconType.Emoji ? document.icon + " " : ""}${name}`; return `# ${title}\n\n${text}`; } diff --git a/server/routes/api/collections/collections.ts b/server/routes/api/collections/collections.ts index 7dc5549222..36f33485f3 100644 --- a/server/routes/api/collections/collections.ts +++ b/server/routes/api/collections/collections.ts @@ -86,7 +86,9 @@ router.post( }); if (data) { - collection.description = await DocumentHelper.toMarkdown(collection); + collection.description = await DocumentHelper.toMarkdown(collection, { + includeTitle: false, + }); } await collection.saveWithCtx(ctx); @@ -641,7 +643,9 @@ router.post( if (data !== undefined) { collection.content = data; - collection.description = await DocumentHelper.toMarkdown(collection); + collection.description = await DocumentHelper.toMarkdown(collection, { + includeTitle: false, + }); } if (icon !== undefined) {