mirror of
https://github.com/docmost/docmost.git
synced 2026-08-31 02:46:27 +08:00
handle unknown prosemirror nodes
This commit is contained in:
@@ -41,7 +41,8 @@ import { generateHTML, generateJSON } from '../common/helpers/prosemirror/html';
|
|||||||
// see: https://github.com/ueberdosis/tiptap/issues/5352
|
// see: https://github.com/ueberdosis/tiptap/issues/5352
|
||||||
// see:https://github.com/ueberdosis/tiptap/issues/4089
|
// see:https://github.com/ueberdosis/tiptap/issues/4089
|
||||||
//import { generateJSON } from '@tiptap/html';
|
//import { generateJSON } from '@tiptap/html';
|
||||||
import { Node } from '@tiptap/pm/model';
|
import { Node, Schema } from '@tiptap/pm/model';
|
||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
|
||||||
export const tiptapExtensions = [
|
export const tiptapExtensions = [
|
||||||
StarterKit.configure({
|
StarterKit.configure({
|
||||||
@@ -110,9 +111,53 @@ export function jsonToText(tiptapJson: JSONContent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function jsonToNode(tiptapJson: JSONContent) {
|
export function jsonToNode(tiptapJson: JSONContent) {
|
||||||
return Node.fromJSON(getSchema(tiptapExtensions), tiptapJson);
|
const schema = getSchema(tiptapExtensions);
|
||||||
|
try {
|
||||||
|
return Node.fromJSON(schema, tiptapJson);
|
||||||
|
} catch (error) {
|
||||||
|
if (
|
||||||
|
error instanceof RangeError &&
|
||||||
|
error.message.includes('Unknown node type')
|
||||||
|
) {
|
||||||
|
Logger.warn('Stripping unknown node types from document:', error.message);
|
||||||
|
const cleanedJson = stripUnknownNodes(tiptapJson, schema);
|
||||||
|
return Node.fromJSON(schema, cleanedJson);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPageId(documentName: string) {
|
export function getPageId(documentName: string) {
|
||||||
return documentName.split('.')[1];
|
return documentName.split('.')[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stripUnknownNodes(
|
||||||
|
json: JSONContent,
|
||||||
|
schema: Schema,
|
||||||
|
): JSONContent | null {
|
||||||
|
if (!json || typeof json !== 'object') return json;
|
||||||
|
|
||||||
|
// Recursively clean children first, flattening any unwrapped content
|
||||||
|
if (json.content && Array.isArray(json.content)) {
|
||||||
|
const newContent: JSONContent[] = [];
|
||||||
|
for (const child of json.content) {
|
||||||
|
const cleaned = stripUnknownNodes(child, schema);
|
||||||
|
if (Array.isArray(cleaned)) {
|
||||||
|
newContent.push(...cleaned);
|
||||||
|
} else if (cleaned) {
|
||||||
|
newContent.push(cleaned);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
json.content = newContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this node is unknown AFTER processing children
|
||||||
|
if (json.type && !schema.nodes[json.type]) {
|
||||||
|
// Unwrap: return cleaned children directly instead of wrapping
|
||||||
|
return (
|
||||||
|
json.content && json.content.length > 0 ? json.content : null
|
||||||
|
) as any;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user