feat: Add HTML export option (#4056)
* tidy * Add title to HTML export * fix: Add compatability for documents without collab state * Add HTML download option to UI * docs * fix nodes that required document to render * Refactor to allow for styling of HTML export * div>article for easier programatic content extraction
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
SearchQuery,
|
||||
Event,
|
||||
} from "@server/models";
|
||||
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||
import {
|
||||
buildShare,
|
||||
buildCollection,
|
||||
@@ -462,7 +463,22 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should return document text with accept=text/markdown", async () => {
|
||||
const { user, document } = await seed();
|
||||
const res = await server.post("/api/documents.export", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
id: document.id,
|
||||
},
|
||||
headers: {
|
||||
accept: "text/markdown",
|
||||
},
|
||||
});
|
||||
const body = await res.text();
|
||||
expect(body).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should return archived document", async () => {
|
||||
@@ -476,7 +492,7 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should not return published document in collection not a member of", async () => {
|
||||
@@ -509,7 +525,7 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should return document from shareId without token", async () => {
|
||||
@@ -526,7 +542,7 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should not return document from revoked shareId", async () => {
|
||||
@@ -576,7 +592,7 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should return draft document from shareId with token", async () => {
|
||||
@@ -596,7 +612,7 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should return document from shareId in collection not a member of", async () => {
|
||||
@@ -616,7 +632,7 @@ describe("#documents.export", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toEqual(document.toMarkdown());
|
||||
expect(body.data).toEqual(DocumentHelper.toMarkdown(document));
|
||||
});
|
||||
|
||||
it("should require authorization without token", async () => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import fs from "fs-extra";
|
||||
import invariant from "invariant";
|
||||
import Router from "koa-router";
|
||||
import mime from "mime-types";
|
||||
import { Op, ScopeOptions, WhereOptions } from "sequelize";
|
||||
import { subtractDate } from "@shared/utils/date";
|
||||
import documentCreator from "@server/commands/documentCreator";
|
||||
@@ -27,6 +28,7 @@ import {
|
||||
User,
|
||||
View,
|
||||
} from "@server/models";
|
||||
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||
import { authorize, cannot } from "@server/policies";
|
||||
import {
|
||||
presentCollection,
|
||||
@@ -439,14 +441,46 @@ router.post(
|
||||
async (ctx) => {
|
||||
const { id, shareId } = ctx.body;
|
||||
assertPresent(id || shareId, "id or shareId is required");
|
||||
|
||||
const { user } = ctx.state;
|
||||
const accept = ctx.request.headers["accept"];
|
||||
|
||||
const { document } = await documentLoader({
|
||||
id,
|
||||
shareId,
|
||||
user,
|
||||
// We need the collaborative state to generate HTML.
|
||||
includeState: accept === "text/html",
|
||||
});
|
||||
|
||||
let contentType;
|
||||
let content;
|
||||
|
||||
if (accept?.includes("text/html")) {
|
||||
contentType = "text/html";
|
||||
content = DocumentHelper.toHTML(document);
|
||||
} else if (accept?.includes("text/markdown")) {
|
||||
contentType = "text/markdown";
|
||||
content = DocumentHelper.toMarkdown(document);
|
||||
} else {
|
||||
contentType = "application/json";
|
||||
content = DocumentHelper.toMarkdown(document);
|
||||
}
|
||||
|
||||
if (contentType !== "application/json") {
|
||||
ctx.set("Content-Type", contentType);
|
||||
ctx.set(
|
||||
"Content-Disposition",
|
||||
`attachment; filename="${document.title}.${mime.extension(
|
||||
contentType
|
||||
)}"`
|
||||
);
|
||||
ctx.body = content;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: document.toMarkdown(),
|
||||
data: content,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user