From 42f92fa289ca3d6fe1bbcb792d318f276792678d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 28 Feb 2025 07:52:05 -0500 Subject: [PATCH] test --- server/commands/documentCreator.ts | 98 +++++++++++---------- server/models/helpers/ProsemirrorHelper.tsx | 7 +- 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/server/commands/documentCreator.ts b/server/commands/documentCreator.ts index fe4bfa3ce0..63d036f815 100644 --- a/server/commands/documentCreator.ts +++ b/server/commands/documentCreator.ts @@ -3,6 +3,7 @@ import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { TextHelper } from "@shared/utils/TextHelper"; import { Document, Event, User } from "@server/models"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; +import { ProsemirrorHelper as ProsemirrorHelper2 } from "@server/models/helpers/ProsemirrorHelper"; import { APIContext } from "@server/types"; type Props = Optional< @@ -81,53 +82,56 @@ export default async function documentCreator({ } } - const document = await Document.create( - { - id, - urlId, - parentDocumentId, - editorVersion, - collectionId, - teamId: user.teamId, - createdAt, - updatedAt: updatedAt ?? createdAt, - lastModifiedById: user.id, - createdById: user.id, - template, - templateId, - publishedAt, - importId, - sourceMetadata, - fullWidth: templateDocument ? templateDocument.fullWidth : fullWidth, - icon: templateDocument ? templateDocument.icon : icon, - color: templateDocument ? templateDocument.color : color, - title: - title ?? - (templateDocument - ? template - ? templateDocument.title - : TextHelper.replaceTemplateVariables(templateDocument.title, user) - : ""), - text: - text ?? - (templateDocument - ? template - ? templateDocument.text - : TextHelper.replaceTemplateVariables(templateDocument.text, user) - : ""), - content: templateDocument - ? ProsemirrorHelper.replaceTemplateVariables( - await DocumentHelper.toJSON(templateDocument), - user - ) - : content, - state, - }, - { - silent: !!createdAt, - transaction, - } - ); + const titleWithReplacements = + title ?? + (templateDocument + ? template + ? templateDocument.title + : TextHelper.replaceTemplateVariables(templateDocument.title, user) + : ""); + + const contentWithReplacements = text + ? ProsemirrorHelper2.toProsemirror(text).toJSON() + : templateDocument + ? template + ? templateDocument.content + : ProsemirrorHelper.replaceTemplateVariables( + await DocumentHelper.toJSON(templateDocument), + user + ) + : content; + + const document = Document.build({ + id, + urlId, + parentDocumentId, + editorVersion, + collectionId, + teamId: user.teamId, + createdAt, + updatedAt: updatedAt ?? createdAt, + lastModifiedById: user.id, + createdById: user.id, + template, + templateId, + publishedAt, + importId, + sourceMetadata, + fullWidth: fullWidth ?? templateDocument?.fullWidth, + icon: icon ?? templateDocument?.icon, + color: color ?? templateDocument?.color, + title: titleWithReplacements, + content: contentWithReplacements, + state, + }); + + document.text = DocumentHelper.toMarkdown(document); + + await document.save({ + silent: !!createdAt, + transaction, + }); + await Event.create( { name: "documents.create", diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index e8f2da6e95..a57beebbc6 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -118,10 +118,13 @@ export class ProsemirrorHelper { /** * Converts a plain object into a Prosemirror Node. * - * @param data The object to parse + * @param data The ProsemirrorData object or string to parse. * @returns The content as a Prosemirror Node */ - static toProsemirror(data: ProsemirrorData) { + static toProsemirror(data: ProsemirrorData | string) { + if (typeof data === "string") { + return parser.parse(data); + } return Node.fromJSON(schema, data); }