mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +03:00
Add child documents list to markdown export for shared documents (#11495)
* Initial plan * Add child documents list to markdown export for shared documents Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> * Apply prettier formatting to app.ts Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> * fix tree context * test: Account for document share --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Co-authored-by: Tom Moor <tom@getoutline.com>
This commit is contained in:
2 files changed
+108
-2
No files matched your search
+46
-2
@@ -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<Buffer> => {
|
||||
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<IntegrationType.Analytics>[] = [];
|
||||
|
||||
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;
|
||||
|
||||
@@ -87,4 +87,66 @@ describe("/s/:id", () => {
|
||||
expect(res.headers.get("content-type")).toContain("text/html");
|
||||
expect(body).toContain(`<title>${document.title}</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]");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user