fix: Import max length not correctly communicated on import (#5434)

This commit is contained in:
Tom Moor
2023-06-17 08:52:57 +01:00
committed by GitHub
parent 9d04d5ebd9
commit 9ef375d83c
6 changed files with 81 additions and 61 deletions

View File

@@ -9,8 +9,8 @@ import { sequelize } from "@server/database/sequelize";
import Logger from "@server/logging/Logger";
import { trace } from "@server/logging/tracing";
import Document from "@server/models/Document";
import ProsemirrorHelper from "@server/models/helpers/ProsemirrorHelper";
import documentCollaborativeUpdater from "../commands/documentCollaborativeUpdater";
import markdownToYDoc from "./utils/markdownToYDoc";
@trace()
export default class PersistenceExtension implements Extension {
@@ -51,11 +51,11 @@ export default class PersistenceExtension implements Extension {
"database",
`Document ${documentId} is not in state, creating from markdown`
);
const ydoc = markdownToYDoc(document.text, fieldName);
const state = Y.encodeStateAsUpdate(ydoc);
const ydoc = ProsemirrorHelper.toYDoc(document.text, fieldName);
const state = ProsemirrorHelper.toState(ydoc);
await document.update(
{
state: Buffer.from(state),
state,
},
{
silent: true,

View File

@@ -1,47 +0,0 @@
import { prosemirrorToYDoc } from "@getoutline/y-prosemirror";
import { Node, Fragment } from "prosemirror-model";
import * as Y from "yjs";
import embeds from "@shared/editor/embeds";
import { parser, schema } from "@server/editor";
export default function markdownToYDoc(
markdown: string,
fieldName = "default"
): Y.Doc {
let node = parser.parse(markdown);
// in the editor embeds were created at runtime by converting links
// into embeds where they match. Because we're converting to a CRDT structure
// on the server we need to mimic this behavior.
function urlsToEmbeds(node: Node): Node | null {
if (node.type.name === "paragraph") {
// @ts-expect-error ts-migrate(2339) FIXME: Property 'content' does not exist on type 'Fragmen... Remove this comment to see the full error message
for (const textNode of node.content.content) {
for (const embed of embeds) {
if (textNode.text && embed.matcher(textNode.text)) {
return schema.nodes.embed.createAndFill({
href: textNode.text,
});
}
}
}
}
if (node.content) {
const contentAsArray =
// @ts-expect-error content
node.content instanceof Fragment ? node.content.content : node.content;
// @ts-expect-error content
node.content = Fragment.fromArray(contentAsArray.map(urlsToEmbeds));
}
return node;
}
if (node) {
node = urlsToEmbeds(node);
}
// @ts-expect-error null node
return prosemirrorToYDoc(node, fieldName);
}