diff --git a/server/routes/app.ts b/server/routes/app.ts index 0a214b60a3..3fe7db71d1 100644 --- a/server/routes/app.ts +++ b/server/routes/app.ts @@ -5,7 +5,11 @@ import type { Context, Next } from "koa"; import escape from "lodash/escape"; import { Sequelize } from "sequelize"; import isUUID from "validator/lib/isUUID"; -import { IntegrationType, TeamPreference } from "@shared/types"; +import { + IntegrationType, + TeamPreference, + type NavigationNode, +} from "@shared/types"; import { unicodeCLDRtoISO639 } from "@shared/utils/date"; import env from "@server/env"; import { Integration } from "@server/models"; @@ -22,6 +26,29 @@ const viteHost = env.URL.replace(`:${env.PORT}`, ":3001"); let indexHtmlCache: Buffer | undefined; +/** + * Formats navigation tree children as markdown list items. + * + * @param children Array of navigation nodes + * @param baseUrl Base URL for generating links + * @returns Formatted markdown string + */ +function formatChildDocumentsAsMarkdown( + children: NavigationNode[], + baseUrl: string +): string { + if (!children || children.length === 0) { + return ""; + } + + const lines = children.map((child) => { + const url = baseUrl + child.url; + return `- [${child.title}](${url})`; + }); + + return `\n\n---\n\n**Documents**\n\n${lines.join("\n")}`; +} + const readIndexFile = async (): Promise => { if (env.isProduction || env.isTest) { if (indexHtmlCache) { @@ -174,6 +201,7 @@ export const renderShare = async (ctx: Context, next: Next) => { let share, collection, document, team; let analytics: Integration[] = []; + let sharedTree; try { team = await getTeamFromContext(ctx, { includeStateCookie: false }); const result = await loadPublicShare({ @@ -185,6 +213,7 @@ export const renderShare = async (ctx: Context, next: Next) => { share = result.share; collection = result.collection; document = result.document; + sharedTree = result.sharedTree; if (isUUID(shareId) && share?.urlId) { // Redirect temporarily because the url slug @@ -225,11 +254,26 @@ export const renderShare = async (ctx: Context, next: Next) => { ctx.accepts("text/markdown", "text/html") === "text/markdown"; if (prefersMarkdown && (document || collection)) { - const markdown = await DocumentHelper.toMarkdown(document || collection!, { + let markdown = await DocumentHelper.toMarkdown(document || collection!, { includeTitle: true, signedUrls: 86400, // 24 hours teamId: team?.id, }); + + // Append child documents list if the share includes them + if (share?.includeChildDocuments && sharedTree) { + const node = document + ? (collection?.getDocumentTree(document.id) ?? sharedTree) + : sharedTree; + + if (node?.children?.length) { + markdown += formatChildDocumentsAsMarkdown( + node.children, + share.canonicalUrl + ); + } + } + ctx.type = "text/markdown"; ctx.body = markdown; return; diff --git a/server/routes/index.test.ts b/server/routes/index.test.ts index 4cb52f0fbe..2ed35207fe 100644 --- a/server/routes/index.test.ts +++ b/server/routes/index.test.ts @@ -87,4 +87,66 @@ describe("/s/:id", () => { expect(res.headers.get("content-type")).toContain("text/html"); expect(body).toContain(`${document.title}`); }); + + it("should include child documents list in markdown when includeChildDocuments is true", async () => { + const parent = await buildDocument(); + const child1 = await buildDocument({ + teamId: parent.teamId, + collectionId: parent.collectionId, + parentDocumentId: parent.id, + title: "Child Document 1", + }); + const child2 = await buildDocument({ + teamId: parent.teamId, + collectionId: parent.collectionId, + parentDocumentId: parent.id, + title: "Child Document 2", + }); + + const share = await buildShare({ + documentId: parent.id, + teamId: parent.teamId, + includeChildDocuments: true, + }); + + const res = await server.get(`/s/${share.id}`, { + headers: { Accept: "text/markdown" }, + }); + const body = await res.text(); + + expect(res.status).toEqual(200); + expect(res.headers.get("content-type")).toContain("text/markdown"); + expect(body).toContain(`# ${parent.title}`); + expect(body).toContain("---"); + expect(body).toContain("**Documents**"); + expect(body).toContain(`[${child1.title}]`); + expect(body).toContain(`[${child2.title}]`); + }); + + it("should not include child documents list in markdown when includeChildDocuments is false", async () => { + const parent = await buildDocument(); + await buildDocument({ + teamId: parent.teamId, + collectionId: parent.collectionId, + parentDocumentId: parent.id, + title: "Child Document", + }); + + const share = await buildShare({ + documentId: parent.id, + teamId: parent.teamId, + includeChildDocuments: false, + }); + + const res = await server.get(`/s/${share.id}`, { + headers: { Accept: "text/markdown" }, + }); + const body = await res.text(); + + expect(res.status).toEqual(200); + expect(res.headers.get("content-type")).toContain("text/markdown"); + expect(body).toContain(`# ${parent.title}`); + expect(body).not.toContain("**Documents**"); + expect(body).not.toContain("[Child Document]"); + }); });