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:
@@ -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