Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(md): mention parsing - use fallback on missing fields (#4501)

Seamusec03300a6e1e
diff --git a/js/lexical-core/nodes/ClassedBlockNode.ts b/js/lexical-core/nodes/ClassedBlockNode.ts
index dd226e212e..a90130392f 100644
--- a/js/lexical-core/nodes/ClassedBlockNode.ts
+++ b/js/lexical-core/nodes/ClassedBlockNode.ts
@@ -15,9 +15,12 @@ import {
import { $applyIdFromSerialized } from '../plugins/nodeIdPlugin';
type TagName = keyof HTMLElementTagNameMap;
-
const ALLOWED_TAGS = new Set<TagName>(['div', 'p', 'span', 'blockquote']);
+export const isAllowedTagName = (name: TagName | string): name is TagName => {
+ return ALLOWED_TAGS.has(name as TagName);
+}
+
export type ClassedBlockData = {
tag: TagName;
classes: string[];
diff --git a/js/lexical-core/tests/internal-transformer-fallbacks.test.ts b/js/lexical-core/tests/internal-transformer-fallbacks.test.ts
new file mode 100644
index 0000000000..b9e5c7b252
--- /dev/null
+++ b/js/lexical-core/tests/internal-transformer-fallbacks.test.ts
@@ -0,0 +1,126 @@
+import { $convertFromMarkdownString } from '@lexical/markdown';
+import { $getRoot, $isParagraphNode, createEditor } from 'lexical';
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+import { SupportedNodeTypes } from '../node-list';
+import {
+ $isUnknownMentionNode,
+ type UnknownMentionNode,
+} from '../nodes/UnknownMentionNode';
+import { ALL_TRANSFORMERS } from '../transformers';
+
+async function importMarkdown(markdown: string) {
+ const editor = createEditor({
+ nodes: SupportedNodeTypes,
+ onError: (error) => {
+ throw error;
+ },
+ });
+
+ await new Promise<void>((resolve) => {
+ editor.update(
+ () => {
+ $convertFromMarkdownString(markdown, ALL_TRANSFORMERS);
+ },
+ { onUpdate: () => resolve() }
+ );
+ });
+
+ return editor;
+}
+
+function findUnknownMention(): UnknownMentionNode | null {
+ const nodes = $getRoot().getAllTextNodes();
+ for (const node of nodes) {
+ const nextSibling = node.getNextSibling();
+ if ($isUnknownMentionNode(nextSibling)) {
+ return nextSibling;
+ }
+ }
+
+ const rootChildren = $getRoot().getChildren();
+ for (const child of rootChildren) {
+ if ($isUnknownMentionNode(child)) {
+ return child;
+ }
+
+ if ($isParagraphNode(child)) {
+ for (const paragraphChild of child.getChildren()) {
+ if ($isUnknownMentionNode(paragraphChild)) {
+ return paragraphChild;
+ }
+ }
+ }
+ }
+
+ return null;
+}
+
+describe('internal transformer fallbacks', () => {
+ let consoleError: ReturnType<typeof vi.spyOn>;
+
+ beforeAll(() => {
+ consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
+ });
+
+ afterAll(() => {
+ consoleError.mockRestore();
+ });
+
+ it('keeps valid user mentions as user mention nodes', async () => {
+ const editor = await importMarkdown(
+ '<m-user-mention>{"userId":"u1","email":"[email protected]"}</m-user-mention>'
+ );
+
+ editor.getEditorState().read(() => {
+ const node = $getRoot().getFirstChild()?.getFirstChild();
+ expect(node?.getType()).toBe('user-mention');
+ });
+ });
+
+ it.each([
+ ['<m-user-mention>{bad}</m-user-mention>', 'Unknown User'],
+ ['<m-user-mention>{"email":"[email protected]"}</m-user-mention>', 'Unknown User'],
+ ['<m-katex-equation>{bad}</m-katex-equation>', 'Unknown Equation'],
+ [
+ '<m-katex-equation>{"equation":"x"}</m-katex-equation>',
+ 'Unknown Equation',
+ ],
+ ['<m-snapshot>{bad}</m-snapshot>', 'Unknown Snapshot'],
+ ['<m-await>{bad}</m-await>', 'Unknown Await'],
+ ['<m-link>{bad}</m-link>', 'Unknown Link'],
+ ])('falls back for malformed text transformer payload %#', async (markdown, name) => {
+ const editor = await importMarkdown(markdown);
+
+ editor.getEditorState().read(() => {
+ const unknown = findUnknownMention();
+ expect(unknown?.getName()).toBe(name);
+ });
+ });
+
+ it.each([
+ ['<m-document-card>{bad}</m-document-card>', 'Unknown Item'],
+ [
+ '<m-document-card>{"documentName":"Doc"}</m-document-card>',
+ 'Unknown Item',
+ ],
+ ['<m-paste>{bad}</m-paste>', 'Unknown Paste'],
+ ['<m-image>{bad}</m-image>', 'Unknown Image'],
+ ['<m-image>{"alt":"missing url"}</m-image>', 'Unknown Image'],
+ ['<m-video>{bad}</m-video>', 'Unknown Video'],
+ ['<m-watermark>{bad}</m-watermark>', 'Unknown Watermark'],
+ [
+ '<m-email-thread-embed>{bad}</m-email-thread-embed>',
+ 'Unknown Email Thread',
+ ],
+ ])('wraps malformed element transformer fallback in a paragraph %#', async (markdown, name) => {
+ const editor = await importMarkdown(markdown);
+
+ editor.getEditorState().read(() => {
+ const firstChild = $getRoot().getFirstChild();
+ expect($isParagraphNode(firstChild)).toBe(true);
+
+ const unknown = findUnknownMention();
+ expect(unknown?.getName()).toBe(name);
+ });
+ });
+});
diff --git a/js/lexical-core/transformers/await.ts b/js/lexical-core/transformers/await.ts
index 6fd2dc3806..94060ad873 100644
--- a/js/lexical-core/transformers/await.ts
+++ b/js/lexical-core/transformers/await.ts
@@ -1,10 +1,10 @@
import type { TextMatchTransformer } from '@lexical/markdown';
import type { TextNode } from 'lexical';
+import { $createAwaitNode, $isAwaitNode, AwaitNode } from '../nodes/AwaitNode';
import {
- $createAwaitNode,
- $isAwaitNode,
- AwaitNode,
-} from '../nodes/AwaitNode';
+ replaceTextWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
/**
* Internal transformer for persisted await placeholders.
@@ -14,7 +14,7 @@ import {
* static channel markdown can render the same placeholder node.
*/
export const I_AWAIT_NODE: TextMatchTransformer = {
- dependencies: [AwaitNode],
+ dependencies: [AwaitNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-await>(.*?)<\/m-await>/,
importRegExp: /<m-await>(.*?)<\/m-await>/,
@@ -37,6 +37,7 @@ export const I_AWAIT_NODE: TextMatchTransformer = {
);
} catch (e) {
console.error('Error in I_AWAIT_NODE replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Await');
}
},
};
diff --git a/js/lexical-core/transformers/classedBlock.ts b/js/lexical-core/transformers/classedBlock.ts
index 3c82d29d49..58e9792433 100644
--- a/js/lexical-core/transformers/classedBlock.ts
+++ b/js/lexical-core/transformers/classedBlock.ts
@@ -11,6 +11,7 @@ import {
$createClassedBlockNode,
$isClassedBlockNode,
ClassedBlockNode,
+ isAllowedTagName,
} from '../nodes/ClassedBlockNode';
import { CUSTOM_TRANSFORMERS } from './customTransformers';
import { I_EQUATION_NODE } from './katex';
@@ -20,6 +21,10 @@ import {
I_USER_MENTION,
} from './mentions';
import { xmlMatcher } from './transformers';
+import {
+ replaceElementWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
const TAG_MACRO_QUOTE = 'm-email-thread-embed';
const REG_EXP_XML_MACRO_QUOTE = xmlMatcher(TAG_MACRO_QUOTE, '');
@@ -181,7 +186,7 @@ export const HTML_BLOCKQUOTE: MultilineElementTransformer = {
};
export const I_MACRO_QUOTE: ElementTransformer = {
- dependencies: [ClassedBlockNode],
+ dependencies: [ClassedBlockNode, UnknownMentionNode],
type: 'element',
regExp: REG_EXP_XML_MACRO_QUOTE,
@@ -224,12 +229,14 @@ export const I_MACRO_QUOTE: ElementTransformer = {
);
if (!metadataMatch || !metadataMatch[1]) {
- console.error('Error parsing macro-quote: no metadata found');
- return;
+ throw new Error('Missing macro-quote metadata');
}
const metadata = JSON.parse(metadataMatch[1]);
const { tag, classes } = metadata;
+ if (typeof tag !== 'string' || !isAllowedTagName(tag) || !Array.isArray(classes)) {
+ throw new Error('Invalid macro-quote metadata');
+ }
// Extract content after metadata
const contentStart = metadataMatch[0].length;
@@ -237,7 +244,10 @@ export const I_MACRO_QUOTE: ElementTransformer = {
const content = xmlContent.substring(contentStart, contentEnd);
// Create the ClassedBlockNode
- const classedBlockNode = $createClassedBlockNode({ tag, classes });
+ const classedBlockNode = $createClassedBlockNode({
+ tag: tag as keyof HTMLElementTagNameMap,
+ classes,
+ });
// Parse and append children
$convertFromMarkdownString(
@@ -249,6 +259,7 @@ export const I_MACRO_QUOTE: ElementTransformer = {
node.replace(classedBlockNode);
} catch (error) {
console.error('Error parsing macro-quote:', error);
+ replaceElementWithUnknownMention(node, 'Unknown Email Thread');
}
},
};
diff --git a/js/lexical-core/transformers/image.ts b/js/lexical-core/transformers/image.ts
index e62ac8a513..f1e9865d07 100644
--- a/js/lexical-core/transformers/image.ts
+++ b/js/lexical-core/transformers/image.ts
@@ -1,10 +1,14 @@
import type { ElementTransformer } from '@lexical/markdown';
import type { ElementNode, LexicalNode } from 'lexical';
import { $createImageNode, $isImageNode, ImageNode } from '../nodes/ImageNode';
+import {
+ replaceElementWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// Internal transformer for images with constrained dimensions
export const I_IMAGE_CONSTRAINED: ElementTransformer = {
- dependencies: [ImageNode],
+ dependencies: [ImageNode, UnknownMentionNode],
type: 'element',
regExp: /<m-image>(.*?)<\/m-image>/,
export: (node: LexicalNode) => {
@@ -51,6 +55,7 @@ export const I_IMAGE_CONSTRAINED: ElementTransformer = {
parent.append(imageNode);
} catch (e) {
console.error('Failed to parse m-image:', e);
+ replaceElementWithUnknownMention(parent, 'Unknown Image');
}
},
};
diff --git a/js/lexical-core/transformers/katex.ts b/js/lexical-core/transformers/katex.ts
index 8236baaee1..5ad2db79a8 100644
--- a/js/lexical-core/transformers/katex.ts
+++ b/js/lexical-core/transformers/katex.ts
@@ -8,6 +8,10 @@ import {
$isEquationNode,
EquationNode,
} from '../nodes/EquationNode';
+import {
+ replaceTextWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// Internal Equation Node
@@ -18,7 +22,7 @@ const REG_EXP_KATEX_EQUATION = new RegExp(
);
export const I_EQUATION_NODE: TextMatchTransformer = {
- dependencies: [EquationNode],
+ dependencies: [EquationNode, UnknownMentionNode],
type: 'text-match',
regExp: REG_EXP_KATEX_EQUATION,
importRegExp: REG_EXP_KATEX_EQUATION,
@@ -40,7 +44,8 @@ export const I_EQUATION_NODE: TextMatchTransformer = {
const equationNode = $createEquationNode(data.equation, data.inline);
node.replace(equationNode);
} catch (e) {
- console.error(e);
+ console.error('Error in I_EQUATION_NODE replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Equation');
}
},
};
diff --git a/js/lexical-core/transformers/mentions.ts b/js/lexical-core/transformers/mentions.ts
index 460371e80b..86df0919b1 100644
--- a/js/lexical-core/transformers/mentions.ts
+++ b/js/lexical-core/transformers/mentions.ts
@@ -11,12 +11,17 @@ import { GroupMentionNode } from '../nodes/GroupMentionNode';
import { PullRequestMentionNode } from '../nodes/PullRequestMentionNode';
import { ThemeMentionNode } from '../nodes/ThemeMentionNode';
import { UserMentionNode } from '../nodes/UserMentionNode';
+import {
+ replaceElementWithUnknownMention,
+ replaceTextWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// NOTE: If you are changing this file, you may need to update the `mention_utils` crate in `macro-api` as well. Please notify @hutch should you update this file.
// Internal User Mentions
export const I_USER_MENTION: TextMatchTransformer = {
- dependencies: [UserMentionNode],
+ dependencies: [UserMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-user-mention>(.*?)<\/m-user-mention>/,
importRegExp: /<m-user-mention>(.*?)<\/m-user-mention>/,
@@ -38,6 +43,7 @@ export const I_USER_MENTION: TextMatchTransformer = {
node.replace(userMentionNode);
} catch (e) {
console.error('Error in I_USER_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown User');
}
},
};
@@ -69,7 +75,7 @@ export const E_USER_MENTION: ElementTransformer = {
// Internal Contact Mentions
export const I_CONTACT_MENTION: TextMatchTransformer = {
- dependencies: [ContactMentionNode],
+ dependencies: [ContactMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-contact-mention>(.*?)<\/m-contact-mention>/,
importRegExp: /<m-contact-mention>(.*?)<\/m-contact-mention>/,
@@ -97,7 +103,8 @@ export const I_CONTACT_MENTION: TextMatchTransformer = {
);
node.replace(contactMentionNode);
} catch (e) {
- console.error(e);
+ console.error('Error in I_CONTACT_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Contact');
}
},
};
@@ -132,7 +139,7 @@ export const E_CONTACT_MENTION: ElementTransformer = {
// Internal Date Mentions
export const I_DATE_MENTION: TextMatchTransformer = {
- dependencies: [DateMentionNode],
+ dependencies: [DateMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-date-mention>(.*?)<\/m-date-mention>/,
importRegExp: /<m-date-mention>(.*?)<\/m-date-mention>/,
@@ -158,7 +165,8 @@ export const I_DATE_MENTION: TextMatchTransformer = {
);
node.replace(dateMentionNode);
} catch (e) {
- console.error(e);
+ console.error('Error in I_DATE_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Date');
}
},
};
@@ -192,7 +200,7 @@ export const E_DATE_MENTION: ElementTransformer = {
// Internal Document Mentions
export const I_DOCUMENT_MENTION: TextMatchTransformer = {
- dependencies: [DocumentMentionNode],
+ dependencies: [DocumentMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-document-mention>(.*?)<\/m-document-mention>/,
importRegExp: /<m-document-mention>(.*?)<\/m-document-mention>/,
@@ -224,6 +232,7 @@ export const I_DOCUMENT_MENTION: TextMatchTransformer = {
node.replace(documentMentionNode);
} catch (e) {
console.error('Error in I_DOCUMENT_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Item');
}
},
};
@@ -281,7 +290,7 @@ export const E_DOCUMENT_MENTION: ElementTransformer = {
// Internal Pull Request Mentions
export const I_PR_MENTION: TextMatchTransformer = {
- dependencies: [PullRequestMentionNode],
+ dependencies: [PullRequestMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-pr-mention>(.*?)<\/m-pr-mention>/,
importRegExp: /<m-pr-mention>(.*?)<\/m-pr-mention>/,
@@ -308,6 +317,7 @@ export const I_PR_MENTION: TextMatchTransformer = {
node.replace(prMentionNode);
} catch (e) {
console.error('Error in I_PR_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Pull Request');
}
},
};
@@ -343,7 +353,7 @@ export const E_PR_MENTION: ElementTransformer = {
// Internal Group Mentions (e.g., @here)
export const I_GROUP_MENTION: TextMatchTransformer = {
- dependencies: [GroupMentionNode],
+ dependencies: [GroupMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-group-mention>(.*?)<\/m-group-mention>/,
importRegExp: /<m-group-mention>(.*?)<\/m-group-mention>/,
@@ -357,11 +367,14 @@ export const I_GROUP_MENTION: TextMatchTransformer = {
replace: (node: TextNode, match: RegExpMatchArray) => {
try {
const data = JSON.parse(match[1]);
- if (!('groupAlias' in data)) throw new Error('Missing field groupAlias');
+ if (!('groupAlias' in data)) {
+ throw new Error('Missing field groupAlias');
+ }
const groupMentionNode = new GroupMentionNode(data.groupAlias);
node.replace(groupMentionNode);
} catch (e) {
console.error('Error in I_GROUP_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Group');
}
},
};
@@ -394,7 +407,7 @@ export const E_GROUP_MENTION: ElementTransformer = {
// Internal Document Cards
export const I_DOCUMENT_CARD: ElementTransformer = {
- dependencies: [DocumentCardNode],
+ dependencies: [DocumentCardNode, UnknownMentionNode],
type: 'element',
regExp: /<m-document-card>(.*?)<\/m-document-card>/,
export: (node) => {
@@ -432,13 +445,14 @@ export const I_DOCUMENT_CARD: ElementTransformer = {
parentNode.replace(documentCardNode);
} catch (e) {
console.error('Error in I_DOCUMENT_CARD replace:', e);
+ replaceElementWithUnknownMention(parentNode, 'Unknown Item');
}
},
};
// Internal Theme Mentions
export const I_THEME_MENTION: TextMatchTransformer = {
- dependencies: [ThemeMentionNode],
+ dependencies: [ThemeMentionNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-theme-mention>(.*?)<\/m-theme-mention>/,
importRegExp: /<m-theme-mention>(.*?)<\/m-theme-mention>/,
@@ -457,12 +471,14 @@ export const I_THEME_MENTION: TextMatchTransformer = {
typeof parsed !== 'object' ||
parsed === null ||
typeof parsed.name !== 'string'
- )
+ ) {
throw new Error('Invalid theme mention JSON');
+ }
const themeMentionNode = new ThemeMentionNode(parsed.name, parsed.data);
node.replace(themeMentionNode);
} catch (e) {
console.error('Error in I_THEME_MENTION replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Theme');
}
},
};
diff --git a/js/lexical-core/transformers/paste.ts b/js/lexical-core/transformers/paste.ts
index 34bddaca4b..c66e716563 100644
--- a/js/lexical-core/transformers/paste.ts
+++ b/js/lexical-core/transformers/paste.ts
@@ -1,6 +1,10 @@
import type { ElementTransformer } from '@lexical/markdown';
import type { ElementNode, LexicalNode } from 'lexical';
import { $createPasteNode, $isPasteNode, PasteNode } from '../nodes/PasteNode';
+import {
+ replaceElementWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// Internal Paste Node - uses XML-based format for serialization so the
// arbitrary pasted text survives a markdown round-trip without being parsed
@@ -8,7 +12,7 @@ import { $createPasteNode, $isPasteNode, PasteNode } from '../nodes/PasteNode';
// escapes so that XML tags inside the content don't get matched by other
// element transformers during import (JSON.parse handles </>).
export const I_PASTE_NODE: ElementTransformer = {
- dependencies: [PasteNode],
+ dependencies: [PasteNode, UnknownMentionNode],
type: 'element',
regExp: /<m-paste>(.*?)<\/m-paste>/,
export: (node) => {
@@ -32,6 +36,7 @@ export const I_PASTE_NODE: ElementTransformer = {
parentNode.replace(pasteNode);
} catch (e) {
console.error('Error in I_PASTE_NODE replace:', e);
+ replaceElementWithUnknownMention(parentNode, 'Unknown Paste');
}
},
};
diff --git a/js/lexical-core/transformers/snapshot.ts b/js/lexical-core/transformers/snapshot.ts
index b2b75d33e9..49de91e2ee 100644
--- a/js/lexical-core/transformers/snapshot.ts
+++ b/js/lexical-core/transformers/snapshot.ts
@@ -8,10 +8,14 @@ import {
$isSnapshotNode,
SnapshotNode,
} from '../nodes/SnapshotNode';
+import {
+ replaceTextWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// Internal Snapshot Node - uses XML-based format for serialization
export const I_SNAPSHOT_NODE: TextMatchTransformer = {
- dependencies: [SnapshotNode],
+ dependencies: [SnapshotNode, UnknownMentionNode],
type: 'text-match',
regExp: /<m-snapshot>(.*?)<\/m-snapshot>/,
importRegExp: /<m-snapshot>(.*?)<\/m-snapshot>/,
@@ -57,6 +61,7 @@ export const I_SNAPSHOT_NODE: TextMatchTransformer = {
node.replace(snapshotNode);
} catch (e) {
console.error('Error in I_SNAPSHOT_NODE replace:', e);
+ replaceTextWithUnknownMention(node, 'Unknown Snapshot');
}
},
};
diff --git a/js/lexical-core/transformers/transformers.ts b/js/lexical-core/transformers/transformers.ts
index cec9dd2dda..55a7b2c9b9 100644
--- a/js/lexical-core/transformers/transformers.ts
+++ b/js/lexical-core/transformers/transformers.ts
@@ -32,6 +32,10 @@ import {
$createSearchMatchNode,
SearchMatchNode,
} from '../nodes/SearchMatchNode';
+import {
+ replaceTextWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
export function wrapXml(tag: string, attrs: Record<string, any>) {
return `<${tag}>${JSON.stringify(attrs)}</${tag}>`;
@@ -178,7 +182,7 @@ function escapeUrl(url: string): string {
}
export const LINK_XML: TextMatchTransformer = {
- dependencies: [LinkNode],
+ dependencies: [LinkNode, UnknownMentionNode],
export: (node: LexicalNode) => {
if (!($isLinkNode(node) || $isAutoLinkNode(node))) return null;
const text = node.getTextContent();
@@ -202,7 +206,8 @@ export const LINK_XML: TextMatchTransformer = {
linkNode.append(linkTextNode);
textNode.replace(linkNode);
} catch (e) {
- console.error(e);
+ console.error('Error in LINK_XML replace:', e);
+ replaceTextWithUnknownMention(textNode, 'Unknown Link');
}
},
type: 'text-match',
diff --git a/js/lexical-core/transformers/unknownFallback.ts b/js/lexical-core/transformers/unknownFallback.ts
new file mode 100644
index 0000000000..b9fcb8b2cb
--- /dev/null
+++ b/js/lexical-core/transformers/unknownFallback.ts
@@ -0,0 +1,23 @@
+import { $createParagraphNode, type ElementNode, type TextNode } from 'lexical';
+import {
+ $createUnknownMentionNode,
+ UnknownMentionNode,
+} from '../nodes/UnknownMentionNode';
+
+export { UnknownMentionNode };
+
+export function replaceTextWithUnknownMention(
+ node: TextNode,
+ name: string
+): void {
+ node.replace($createUnknownMentionNode({ name }));
+}
+
+export function replaceElementWithUnknownMention(
+ node: ElementNode,
+ name: string
+): void {
+ const paragraph = $createParagraphNode();
+ paragraph.append($createUnknownMentionNode({ name }));
+ node.replace(paragraph);
+}
diff --git a/js/lexical-core/transformers/video.ts b/js/lexical-core/transformers/video.ts
index 5142d5c37a..19bbcd0908 100644
--- a/js/lexical-core/transformers/video.ts
+++ b/js/lexical-core/transformers/video.ts
@@ -1,10 +1,14 @@
import type { ElementTransformer } from '@lexical/markdown';
import type { ElementNode, LexicalNode } from 'lexical';
import { $createVideoNode, $isVideoNode, VideoNode } from '../nodes/VideoNode';
+import {
+ replaceElementWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// Internal transformer — always uses <m-video> for unambiguous round-tripping.
export const I_VIDEO: ElementTransformer = {
- dependencies: [VideoNode],
+ dependencies: [VideoNode, UnknownMentionNode],
type: 'element',
regExp: /<m-video>(.*?)<\/m-video>/,
export: (node: LexicalNode) => {
@@ -56,6 +60,7 @@ export const I_VIDEO: ElementTransformer = {
parent.append(videoNode);
} catch (e) {
console.error('Failed to parse m-video:', e);
+ replaceElementWithUnknownMention(parent, 'Unknown Video');
}
},
};
diff --git a/js/lexical-core/transformers/watermark.ts b/js/lexical-core/transformers/watermark.ts
index 190079b2dd..097d32cfb5 100644
--- a/js/lexical-core/transformers/watermark.ts
+++ b/js/lexical-core/transformers/watermark.ts
@@ -5,10 +5,14 @@ import {
$isWatermarkNode,
WatermarkNode,
} from '../nodes/WatermarkNode';
+import {
+ replaceElementWithUnknownMention,
+ UnknownMentionNode,
+} from './unknownFallback';
// Internal Watermark
export const I_WATERMARK: ElementTransformer = {
- dependencies: [WatermarkNode],
+ dependencies: [WatermarkNode, UnknownMentionNode],
type: 'element',
regExp: /<m-watermark>(.*?)<\/m-watermark>/,
export: (node) => {
@@ -30,7 +34,8 @@ export const I_WATERMARK: ElementTransformer = {
const watermarkNode = $createWatermarkNode({ content: data.content });
parent.append(watermarkNode);
} catch (e) {
- console.error(e);
+ console.error('Error in I_WATERMARK replace:', e);
+ replaceElementWithUnknownMention(parent, 'Unknown Watermark');
}
},
};