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.

feat(markdown): render video file links as video elements (#7643)

gbirman8e175cb4637d
diff --git a/.gitignore b/.gitignore
index bf0162c414..92ff2435d9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ run-local.fish
# Per-instance generated local-stack artifacts (compose override, env, kickstart, Caddyfile).
infra/local/generated/
**/target/
+/target
**/ai_tools/schemas/
.pi/outputs/
.commandcode/
diff --git a/packages/lexical-core/tests/tables.test.ts b/packages/lexical-core/tests/tables.test.ts
index a68204239a..6571126d82 100644
--- a/packages/lexical-core/tests/tables.test.ts
+++ b/packages/lexical-core/tests/tables.test.ts
@@ -533,3 +533,101 @@ describe('pipe table external transformer', () => {
expect(lines[1]).toBe('| x | y | z |');
});
});
+
+describe('video link transformer', () => {
+ it('converts markdown links with video URLs to video nodes', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(
+ editor,
+ '[Screen Recording 2026-10-06 at 4.03.28 PM.mov](https://static-file-service.macro.com/file/3d7ab8c8-05b7-477f-aa83-7dd514219867)'
+ );
+
+ editor.getEditorState().read(() => {
+ const root = $getRoot();
+ const video = root.getChildren().find($isVideoNode);
+ expect(video).toBeDefined();
+ expect(video!.getUrl()).toBe(
+ 'https://static-file-service.macro.com/file/3d7ab8c8-05b7-477f-aa83-7dd514219867'
+ );
+ });
+ });
+
+ it('converts links with .mp4 extension to video nodes', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(editor, '[Video](https://example.com/video.mp4)');
+
+ editor.getEditorState().read(() => {
+ const root = $getRoot();
+ const video = root.getChildren().find($isVideoNode);
+ expect(video).toBeDefined();
+ expect(video!.getUrl()).toBe('https://example.com/video.mp4');
+ });
+ });
+
+ it('converts links with .mov extension to video nodes', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(editor, '[Recording](https://example.com/clip.mov)');
+
+ editor.getEditorState().read(() => {
+ const root = $getRoot();
+ const video = root.getChildren().find($isVideoNode);
+ expect(video).toBeDefined();
+ expect(video!.getUrl()).toBe('https://example.com/clip.mov');
+ });
+ });
+
+ it('leaves non-video links as links', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(editor, '[Document](https://example.com/doc.pdf)');
+
+ editor.getEditorState().read(() => {
+ const root = $getRoot();
+ expect(root.getChildren().find($isVideoNode)).toBeUndefined();
+ expect(root.getTextContent()).toContain('Document');
+ });
+ expect(exportMarkdown(editor)).toContain('https://example.com/doc.pdf');
+ });
+
+ it('leaves static-file PDF links as links', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(
+ editor,
+ '[Report.pdf](https://static-file-service.macro.com/file/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee)'
+ );
+
+ editor.getEditorState().read(() => {
+ expect($getRoot().getChildren().find($isVideoNode)).toBeUndefined();
+ expect($getRoot().getTextContent()).toContain('Report.pdf');
+ });
+ expect(exportMarkdown(editor)).toContain(
+ 'static-file-service.macro.com/file/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
+ );
+ });
+
+ it('does not turn a trailing inline video link into a block video', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(
+ editor,
+ 'Watch this: [clip](https://example.com/clip.mp4)'
+ );
+
+ editor.getEditorState().read(() => {
+ expect($getRoot().getChildren().find($isVideoNode)).toBeUndefined();
+ expect($getRoot().getTextContent()).toContain('Watch this:');
+ });
+ });
+
+ it('ignores a video extension that only appears in a query string', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(
+ editor,
+ '[Document](https://example.com/document.pdf?next=clip.mp4)'
+ );
+
+ editor.getEditorState().read(() => {
+ expect($getRoot().getChildren().find($isVideoNode)).toBeUndefined();
+ expect($getRoot().getTextContent()).toContain('Document');
+ });
+ expect(exportMarkdown(editor)).toContain('document.pdf?next=clip.mp4');
+ });
+});
diff --git a/packages/lexical-core/transformers/index.ts b/packages/lexical-core/transformers/index.ts
index f753bbd539..0c23059547 100644
--- a/packages/lexical-core/transformers/index.ts
+++ b/packages/lexical-core/transformers/index.ts
@@ -52,7 +52,7 @@ import {
SEARCH_MATCH,
} from './transformers';
import { UNKNOWN_MENTION } from './unknownMention';
-import { I_VIDEO } from './video';
+import { I_VIDEO, VIDEO_LINK } from './video';
import { E_WATERMARK, I_WATERMARK } from './watermark';
export { isConversionOnlyTransformer };
@@ -69,6 +69,7 @@ export const INTERNAL_TRANSFORMERS: Transformer[] = [
I_PASTE_NODE, // Must be before mentions to avoid matching inner tags in paste content
I_HTML_RENDER,
PRESERVE_LINES,
+ VIDEO_LINK, // Must be before LINK_XML to catch video URLs
LINK_XML, // Prefer internal xml link to handle []() in link text
MARK_XML,
SEARCH_MATCH,
@@ -110,6 +111,7 @@ export const EXTERNAL_TRANSFORMERS: Transformer[] = [
I_DATABASE_QUERY,
HR,
MARK_XML,
+ VIDEO_LINK,
I_VIDEO,
IMAGE,
BR_TAG_TO_LINE_BREAK,
@@ -153,6 +155,7 @@ export const ALL_TRANSFORMERS: Transformer[] = [
I_HTML_RENDER,
E_PASTE_NODE,
PRESERVE_LINES,
+ VIDEO_LINK, // Must be before LINK_XML to catch video URLs
LINK_XML, // Prefer internal xml link to handle []() in link text
MARK_XML,
SEARCH_MATCH,
diff --git a/packages/lexical-core/transformers/video.ts b/packages/lexical-core/transformers/video.ts
index 78b79fb767..f6b4e3f251 100644
--- a/packages/lexical-core/transformers/video.ts
+++ b/packages/lexical-core/transformers/video.ts
@@ -6,6 +6,79 @@ import {
UnknownMentionNode,
} from './unknownFallback';
+const VIDEO_EXTENSIONS = [
+ '.mp4',
+ '.webm',
+ '.ogg',
+ '.mov',
+ '.avi',
+ '.mkv',
+ '.m4v',
+] as const;
+
+const VIDEO_EXTENSION_PATTERN = VIDEO_EXTENSIONS.map((ext) =>
+ ext.slice(1)
+).join('|');
+
+function pathnameHasVideoExtension(pathname: string): boolean {
+ const lower = pathname.toLowerCase();
+ return VIDEO_EXTENSIONS.some((ext) => lower.endsWith(ext));
+}
+
+function textHasVideoExtension(text: string): boolean {
+ const lower = text.toLowerCase();
+ return VIDEO_EXTENSIONS.some((ext) => lower.includes(ext));
+}
+
+function isStaticFileServiceHost(hostname: string): boolean {
+ const lower = hostname.toLowerCase();
+ return (
+ lower === 'static-file-service.macro.com' ||
+ lower.endsWith('.static-file-service.macro.com')
+ );
+}
+
+/** True when `url` (and optional link label) identify a video to embed. */
+export const isVideoMarkdownLink = (url: string, linkText = ''): boolean => {
+ let parsed: URL;
+ try {
+ parsed = new URL(url);
+ } catch {
+ return false;
+ }
+
+ if (pathnameHasVideoExtension(parsed.pathname)) {
+ return true;
+ }
+
+ // Static-file links often omit an extension in the path; the label usually
+ // still names the file (e.g. "Screen Recording.mov").
+ if (
+ isStaticFileServiceHost(parsed.hostname) &&
+ parsed.pathname.toLowerCase().startsWith('/file/') &&
+ textHasVideoExtension(linkText)
+ ) {
+ return true;
+ }
+
+ return false;
+};
+
+// Standalone markdown link whose URL path ends in a video extension, or whose
+// label names a video file on Macro static-file-service (`/file/…` has no ext).
+// Lexical slices the match before `replace` runs, so this must not match
+// ordinary links. `^`/`$` keep trailing inline links from replacing a paragraph.
+const VIDEO_LINK_REGEXP = new RegExp(
+ '^(?:' +
+ // Label has a video extension; URL is Macro static-file-service /file/…
+ String.raw`\[([^\]]*\.(?:${VIDEO_EXTENSION_PATTERN})[^\]]*)\]\((https?:\/\/(?:[\w.-]+\.)?static-file-service\.macro\.com\/file\/[^)\s]+)(?:\s"([^"]*)"\s*)?\)` +
+ '|' +
+ // URL path ends with a video extension (query/hash allowed after it)
+ String.raw`\[([^\]]*)\]\(([^)\s?#]*\.(?:${VIDEO_EXTENSION_PATTERN})(?:[?#][^)\s]*)?)(?:\s"([^"]*)"\s*)?\)` +
+ ')\\s*$',
+ 'i'
+);
+
// Internal transformer — always uses <m-video> for unambiguous round-tripping.
export const I_VIDEO: ElementTransformer = {
dependencies: [VideoNode, UnknownMentionNode],
@@ -64,3 +137,36 @@ export const I_VIDEO: ElementTransformer = {
}
},
};
+
+/**
+ * Standalone markdown link → video node.
+ *
+ * The regex must only match video-eligible links: Lexical slices the match out
+ * of the paragraph before `replace` runs, so a late reject would drop ordinary
+ * links. Anchoring to the full line keeps surrounding inline text intact.
+ */
+export const VIDEO_LINK: ElementTransformer = {
+ dependencies: [VideoNode],
+ type: 'element',
+ export: (_node: LexicalNode) => null,
+ regExp: VIDEO_LINK_REGEXP,
+ replace: (node, _, match) => {
+ const linkText = match[1] ?? match[4] ?? '';
+ const url = match[2] ?? match[5] ?? '';
+
+ if (!isVideoMarkdownLink(url, linkText)) {
+ return false;
+ }
+
+ const videoNode = $createVideoNode({
+ srcType: 'url',
+ url,
+ width: 0,
+ height: 0,
+ id: '',
+ controls: true,
+ scale: 1,
+ });
+ node.replace(videoNode);
+ },
+};