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(lexical): add parser for m-links in plaintext (#477)

teoc1406ec77b0e
diff --git a/js/app/packages/lexical-core/tests/mentions.test.ts b/js/app/packages/lexical-core/tests/mentions.test.ts
new file mode 100644
index 0000000000..bf27c7537c
--- /dev/null
+++ b/js/app/packages/lexical-core/tests/mentions.test.ts
@@ -0,0 +1,157 @@
+import { describe, expect, it } from 'vitest';
+import {
+ markdownToPlainText,
+ parseContactMentions,
+ parseDateMentions,
+ parseDocumentMentions,
+ parseLinks,
+ parseUserMentions,
+} from '../utils/parsers';
+
+describe('parseUserMentions', () => {
+ it('extracts email from user mention', () => {
+ const input =
+ '<m-user-mention>{"email":"[email protected]"}</m-user-mention>';
+ expect(parseUserMentions(input)).toBe('[email protected]');
+ });
+
+ it('returns empty string for missing email', () => {
+ const input = '<m-user-mention>{"name":"John"}</m-user-mention>';
+ expect(parseUserMentions(input)).toBe('');
+ });
+
+ it('returns empty string for invalid JSON', () => {
+ const input = '<m-user-mention>invalid</m-user-mention>';
+ expect(parseUserMentions(input)).toBe('');
+ });
+
+ it('handles multiple mentions', () => {
+ const input =
+ 'Hello <m-user-mention>{"email":"[email protected]"}</m-user-mention> and <m-user-mention>{"email":"[email protected]"}</m-user-mention>';
+ expect(parseUserMentions(input)).toBe('Hello [email protected] and [email protected]');
+ });
+
+ it('passes through text without mentions', () => {
+ const input = 'Hello world';
+ expect(parseUserMentions(input)).toBe('Hello world');
+ });
+});
+
+describe('parseContactMentions', () => {
+ it('extracts name from contact mention', () => {
+ const input = '<m-contact-mention>{"name":"Jane Doe"}</m-contact-mention>';
+ expect(parseContactMentions(input)).toBe('Jane Doe');
+ });
+
+ it('falls back to emailOrDomain when name is missing', () => {
+ const input =
+ '<m-contact-mention>{"emailOrDomain":"[email protected]"}</m-contact-mention>';
+ expect(parseContactMentions(input)).toBe('[email protected]');
+ });
+
+ it('prefers name over emailOrDomain', () => {
+ const input =
+ '<m-contact-mention>{"name":"Jane","emailOrDomain":"[email protected]"}</m-contact-mention>';
+ expect(parseContactMentions(input)).toBe('Jane');
+ });
+
+ it('returns empty string for invalid JSON', () => {
+ const input = '<m-contact-mention>not-json</m-contact-mention>';
+ expect(parseContactMentions(input)).toBe('');
+ });
+});
+
+describe('parseDateMentions', () => {
+ it('extracts displayFormat from date mention', () => {
+ const input =
+ '<m-date-mention>{"displayFormat":"Tomorrow"}</m-date-mention>';
+ expect(parseDateMentions(input)).toBe('Tomorrow');
+ });
+
+ it('returns empty string for missing displayFormat', () => {
+ const input = '<m-date-mention>{"date":"2024-01-01"}</m-date-mention>';
+ expect(parseDateMentions(input)).toBe('');
+ });
+
+ it('returns empty string for invalid JSON', () => {
+ const input = '<m-date-mention>{broken</m-date-mention>';
+ expect(parseDateMentions(input)).toBe('');
+ });
+});
+
+describe('parseDocumentMentions', () => {
+ it('extracts documentName from document mention', () => {
+ const input =
+ '<m-document-mention>{"documentName":"My Doc"}</m-document-mention>';
+ expect(parseDocumentMentions(input)).toBe('My Doc');
+ });
+
+ it('returns empty string for missing documentName', () => {
+ const input = '<m-document-mention>{"id":"123"}</m-document-mention>';
+ expect(parseDocumentMentions(input)).toBe('');
+ });
+
+ it('returns empty string for invalid JSON', () => {
+ const input = '<m-document-mention>???</m-document-mention>';
+ expect(parseDocumentMentions(input)).toBe('');
+ });
+});
+
+describe('parseLinks', () => {
+ it('extracts text from link', () => {
+ const input =
+ '<m-link>{"url":"https://example.com","text":"Example"}</m-link>';
+ expect(parseLinks(input)).toBe('Example');
+ });
+
+ it('falls back to url when text is missing', () => {
+ const input = '<m-link>{"url":"https://example.com"}</m-link>';
+ expect(parseLinks(input)).toBe('https://example.com');
+ });
+
+ it('prefers text over url', () => {
+ const input =
+ '<m-link>{"url":"https://example.com","text":"Click here"}</m-link>';
+ expect(parseLinks(input)).toBe('Click here');
+ });
+
+ it('returns empty string for invalid JSON', () => {
+ const input = '<m-link>broken</m-link>';
+ expect(parseLinks(input)).toBe('');
+ });
+
+ it('handles link with title', () => {
+ const input =
+ '<m-link>{"url":"https://example.com","text":"Example","title":"A title"}</m-link>';
+ expect(parseLinks(input)).toBe('Example');
+ });
+});
+
+describe('markdownToPlainText', () => {
+ it('converts mixed content to plain text', () => {
+ const input =
+ 'Hello <m-user-mention>{"email":"[email protected]"}</m-user-mention>, ' +
+ 'please review <m-document-mention>{"documentName":"Report"}</m-document-mention> ' +
+ 'by <m-date-mention>{"displayFormat":"Friday"}</m-date-mention>.';
+ expect(markdownToPlainText(input)).toBe(
+ 'Hello [email protected], please review Report by Friday.'
+ );
+ });
+
+ it('handles text with links', () => {
+ const input =
+ 'Check out <m-link>{"url":"https://example.com","text":"this link"}</m-link> for more info.';
+ expect(markdownToPlainText(input)).toBe(
+ 'Check out this link for more info.'
+ );
+ });
+
+ it('returns original text when no mentions present', () => {
+ const input = 'Just plain text here.';
+ expect(markdownToPlainText(input)).toBe('Just plain text here.');
+ });
+
+ it('handles empty string', () => {
+ expect(markdownToPlainText('')).toBe('');
+ });
+});
diff --git a/js/app/packages/lexical-core/utils/mentions.ts b/js/app/packages/lexical-core/utils/mentions.ts
index 7eebcbeb50..b705691aef 100644
--- a/js/app/packages/lexical-core/utils/mentions.ts
+++ b/js/app/packages/lexical-core/utils/mentions.ts
@@ -75,68 +75,11 @@ export function buildMentionMarkdownString(info: MentionInfo): string {
}
}
-/**
- * Converts markdown text with XML mention tags to plain text.
- * Extracts the readable text from mention nodes:
- * - User mentions: email
- * - Contact mentions: name (fallback to emailOrDomain)
- * - Date mentions: displayFormat
- * - Document mentions: documentName
- */
-export function markdownToPlainText(markdown: string): string {
- let result = markdown;
-
- // Replace user mentions with email
- result = result.replace(
- /<m-user-mention>(.*?)<\/m-user-mention>/g,
- (_, json) => {
- try {
- const data = JSON.parse(json);
- return data.email || '';
- } catch {
- return '';
- }
- }
- );
-
- // Replace contact mentions with name or emailOrDomain
- result = result.replace(
- /<m-contact-mention>(.*?)<\/m-contact-mention>/g,
- (_, json) => {
- try {
- const data = JSON.parse(json);
- return data.name || data.emailOrDomain || '';
- } catch {
- return '';
- }
- }
- );
-
- // Replace date mentions with displayFormat
- result = result.replace(
- /<m-date-mention>(.*?)<\/m-date-mention>/g,
- (_, json) => {
- try {
- const data = JSON.parse(json);
- return data.displayFormat || '';
- } catch {
- return '';
- }
- }
- );
-
- // Replace document mentions with documentName
- result = result.replace(
- /<m-document-mention>(.*?)<\/m-document-mention>/g,
- (_, json) => {
- try {
- const data = JSON.parse(json);
- return data.documentName || '';
- } catch {
- return '';
- }
- }
- );
-
- return result;
-}
+export {
+ markdownToPlainText,
+ parseContactMentions,
+ parseDateMentions,
+ parseDocumentMentions,
+ parseLinks,
+ parseUserMentions,
+} from './parsers';
diff --git a/js/app/packages/lexical-core/utils/parsers.ts b/js/app/packages/lexical-core/utils/parsers.ts
new file mode 100644
index 0000000000..f4a5938b3f
--- /dev/null
+++ b/js/app/packages/lexical-core/utils/parsers.ts
@@ -0,0 +1,77 @@
+export function parseUserMentions(text: string): string {
+ return text.replace(/<m-user-mention>(.*?)<\/m-user-mention>/g, (_, json) => {
+ try {
+ const data = JSON.parse(json);
+ return data.email || '';
+ } catch {
+ return '';
+ }
+ });
+}
+
+export function parseContactMentions(text: string): string {
+ return text.replace(
+ /<m-contact-mention>(.*?)<\/m-contact-mention>/g,
+ (_, json) => {
+ try {
+ const data = JSON.parse(json);
+ return data.name || data.emailOrDomain || '';
+ } catch {
+ return '';
+ }
+ }
+ );
+}
+
+export function parseDateMentions(text: string): string {
+ return text.replace(/<m-date-mention>(.*?)<\/m-date-mention>/g, (_, json) => {
+ try {
+ const data = JSON.parse(json);
+ return data.displayFormat || '';
+ } catch {
+ return '';
+ }
+ });
+}
+
+export function parseDocumentMentions(text: string): string {
+ return text.replace(
+ /<m-document-mention>(.*?)<\/m-document-mention>/g,
+ (_, json) => {
+ try {
+ const data = JSON.parse(json);
+ return data.documentName || '';
+ } catch {
+ return '';
+ }
+ }
+ );
+}
+
+export function parseLinks(text: string): string {
+ return text.replace(/<m-link>(.*?)<\/m-link>/g, (_, json) => {
+ try {
+ const data = JSON.parse(json);
+ return data.text || data.url || '';
+ } catch {
+ return '';
+ }
+ });
+}
+
+/**
+ * Converts markdown text with XML mention tags to plain text.
+ * Extracts the readable text from mention nodes:
+ * - User mentions: email
+ * - Contact mentions: name (fallback to emailOrDomain)
+ * - Date mentions: displayFormat
+ * - Document mentions: documentName
+ * - Links: text (fallback to url)
+ */
+export function markdownToPlainText(markdown: string): string {
+ return parseLinks(
+ parseDocumentMentions(
+ parseDateMentions(parseContactMentions(parseUserMentions(markdown)))
+ )
+ );
+}
diff --git a/js/app/packages/lexical-core/vitest.config.ts b/js/app/packages/lexical-core/vitest.config.ts
new file mode 100644
index 0000000000..6657df289e
--- /dev/null
+++ b/js/app/packages/lexical-core/vitest.config.ts
@@ -0,0 +1,14 @@
+import tsconfigPaths from 'vite-tsconfig-paths';
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ plugins: [
+ tsconfigPaths({
+ root: '../../',
+ }),
+ ],
+ test: {
+ environment: 'jsdom',
+ globals: true,
+ },
+} as any);