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.

chg(md): allow autolinking with no protocol for common tlds (#4202)

Seamus73b65a83356e
diff --git a/js/app/packages/block-email/component/BaseInput.tsx b/js/app/packages/block-email/component/BaseInput.tsx
index 19a0c3b8ac..3fadac07fd 100644
--- a/js/app/packages/block-email/component/BaseInput.tsx
+++ b/js/app/packages/block-email/component/BaseInput.tsx
@@ -1677,6 +1677,7 @@ export function BaseInput(props: {
</div>
<Scroll>
<MarkdownTextarea
+ autoLinkMatchMode="common-tlds"
captureEditor={(editor) => {
setEditor(editor);
form().setCapturedEditor(editor);
diff --git a/js/app/packages/block-email/component/compose/ComposeBody.tsx b/js/app/packages/block-email/component/compose/ComposeBody.tsx
index 4fcfc4fb2c..93693efbbb 100644
--- a/js/app/packages/block-email/component/compose/ComposeBody.tsx
+++ b/js/app/packages/block-email/component/compose/ComposeBody.tsx
@@ -160,6 +160,7 @@ export function ComposeBody(props: {
<Scroll>
<MarkdownTextarea
+ autoLinkMatchMode="common-tlds"
domRef={props.inputRef}
captureEditor={captureEditor}
scrollRef={props.mobileScrollRef}
diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsx
index 6b7e7ac8f0..495f482d01 100644
--- a/js/app/packages/block-md/component/MarkdownEditor.tsx
+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx
@@ -1075,7 +1075,7 @@ export function MarkdownEditor(props: {
<ActionMenu editor={editor} menu={actionsMenuOperations} />
<FloatingMenuGroup>
- <FloatingLinkMenu />
+ <FloatingLinkMenu autoLinkMatchMode="common-tlds" />
<FloatingEquationMenu />
<MarkdownPopup
highlightLayerRef={highlightLayerRef() ?? editorContainerRef}
diff --git a/js/app/packages/channel/Input/configured-markdown-editor.ts b/js/app/packages/channel/Input/configured-markdown-editor.ts
index 86af15c795..75517bafea 100644
--- a/js/app/packages/channel/Input/configured-markdown-editor.ts
+++ b/js/app/packages/channel/Input/configured-markdown-editor.ts
@@ -57,7 +57,7 @@ export function createConfiguredChannelMarkdownEditor(
editor
.withEmojis()
.withActions({ ignoreActionIds: ['hr', 'table', 'latex'] })
- .withLinks({ floatingMenu: true })
+ .withLinks({ floatingMenu: true, autoLinkMatchMode: 'common-tlds' })
.withHistory({ timeGap: 400 })
.withCode()
.withRestoreFocus()
diff --git a/js/app/packages/core/comments/Inputs.tsx b/js/app/packages/core/comments/Inputs.tsx
index 3679b1147c..5ddecd9482 100644
--- a/js/app/packages/core/comments/Inputs.tsx
+++ b/js/app/packages/core/comments/Inputs.tsx
@@ -98,6 +98,7 @@ export function EditInput(props: {
}}
>
<MarkdownTextarea
+ autoLinkMatchMode="common-tlds"
class="text-sm wrap-break-word text-ink"
editable={() => true}
onChange={(value) => {
diff --git a/js/app/packages/core/comments/discussion/configured-discussion-markdown-editor.ts b/js/app/packages/core/comments/discussion/configured-discussion-markdown-editor.ts
index 19c8890ce8..df4b946d77 100644
--- a/js/app/packages/core/comments/discussion/configured-discussion-markdown-editor.ts
+++ b/js/app/packages/core/comments/discussion/configured-discussion-markdown-editor.ts
@@ -45,7 +45,7 @@ export function createConfiguredDiscussionMarkdownEditor(
.withMedia({ fileDrop: true })
.withEmojis()
.withActions({ ignoreActionIds: ['hr', 'table', 'latex'] })
- .withLinks({ floatingMenu: true })
+ .withLinks({ floatingMenu: true, autoLinkMatchMode: 'common-tlds' })
.withHistory({ timeGap: 400 })
.withCode()
.withRestoreFocus()
diff --git a/js/app/packages/core/component/AI/component/input/buildChatEditor.ts b/js/app/packages/core/component/AI/component/input/buildChatEditor.ts
index 099998dcc0..4821f42a82 100644
--- a/js/app/packages/core/component/AI/component/input/buildChatEditor.ts
+++ b/js/app/packages/core/component/AI/component/input/buildChatEditor.ts
@@ -14,7 +14,7 @@ export function buildChatEditor() {
return buildConfig('chat')
.namespace('chat-markdown-area')
.withEmojis()
- .withLinks({ floatingMenu: true })
+ .withLinks({ floatingMenu: true, autoLinkMatchMode: 'common-tlds' })
.withHistory({ timeGap: 400 })
.withCode()
.withRestoreFocus()
diff --git a/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownShell.tsx b/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownShell.tsx
index 95445006b0..125a9322e1 100644
--- a/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownShell.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownShell.tsx
@@ -312,7 +312,9 @@ export const MarkdownShell: Component<
{/* Floating Link Menu */}
<Show when={builderConfig.links?.floatingMenu}>
<FloatingMenuGroup>
- <FloatingLinkMenu />
+ <FloatingLinkMenu
+ autoLinkMatchMode={builderConfig.links?.autoLinkMatchMode}
+ />
</FloatingMenuGroup>
</Show>
</div>
diff --git a/js/app/packages/core/component/LexicalMarkdown/builder/types.ts b/js/app/packages/core/component/LexicalMarkdown/builder/types.ts
index a75b4abe0a..f3108c6491 100644
--- a/js/app/packages/core/component/LexicalMarkdown/builder/types.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/builder/types.ts
@@ -5,6 +5,7 @@ import type { Store } from 'solid-js/store';
import type { MentionBucketId } from '../component/menu/MentionsMenu/MentionsMenuController';
import type { createLexicalWrapper } from '../context/LexicalWrapperContext';
import type {
+ AutoLinkMatchMode,
createAccessoryStore,
createDraggableBlockStore,
createDragInsertStore,
@@ -43,6 +44,7 @@ export type EmojisOptions = Record<string, never>;
export interface LinksOptions {
floatingMenu?: boolean;
+ autoLinkMatchMode?: AutoLinkMatchMode;
}
export interface HistoryOptions {
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx b/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx
index a611d69102..50733bbd23 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx
@@ -29,6 +29,7 @@ import {
LexicalWrapperContext,
} from '../../context/LexicalWrapperContext';
import {
+ type AutoLinkMatchMode,
autoRegister,
awaitPlugin,
codePlugin,
@@ -107,6 +108,7 @@ interface MarkdownTextareaProps {
files: FileSystemFileEntry[],
directories: FileSystemDirectoryEntry[]
) => void;
+ autoLinkMatchMode?: AutoLinkMatchMode;
}
export function MarkdownTextarea(props: MarkdownTextareaProps) {
@@ -379,7 +381,7 @@ export function MarkdownTextarea(props: MarkdownTextareaProps) {
useBlockBoundary={props.useBlockBoundary}
/>
<FloatingMenuGroup>
- <FloatingLinkMenu />
+ <FloatingLinkMenu autoLinkMatchMode={props.autoLinkMatchMode} />
</FloatingMenuGroup>
</div>
</LexicalWrapperContext.Provider>
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/FloatingLinkMenu.tsx b/js/app/packages/core/component/LexicalMarkdown/component/menu/FloatingLinkMenu.tsx
index e63b4f0ad1..3a5992c755 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/FloatingLinkMenu.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/FloatingLinkMenu.tsx
@@ -39,6 +39,7 @@ import { LexicalWrapperContext } from '../../context/LexicalWrapperContext';
import { floatWithElement } from '../../directive/floatWithElement';
import { floatWithSelection } from '../../directive/floatWithSelection';
import {
+ type AutoLinkMatchMode,
type ILinkInfo,
INSERT_LINK_COMMAND,
linksPlugin,
@@ -55,7 +56,10 @@ false && clickOutside;
const MENU_ID = 'floating-link-menu';
const HOVER_ID = 'floating-link-hover';
-export function FloatingLinkMenu(props: { closePopup?: () => void }) {
+export function FloatingLinkMenu(props: {
+ closePopup?: () => void;
+ autoLinkMatchMode?: AutoLinkMatchMode;
+}) {
const { plugins, editor } = useContext(LexicalWrapperContext) ?? {};
if (!plugins || !editor) {
console.error(
@@ -241,6 +245,7 @@ export function FloatingLinkMenu(props: { closePopup?: () => void }) {
onHoverLink,
onClickLink,
onCreateLink,
+ autoLinkMatchMode: props.autoLinkMatchMode,
})
);
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.test.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.test.ts
new file mode 100644
index 0000000000..14d31fdd2f
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from 'vitest';
+
+import { findNextAutoLinkMatch } from './linksPlugin';
+
+describe('findNextAutoLinkMatch', () => {
+ it('requires a protocol in protocol mode', () => {
+ expect(findNextAutoLinkMatch('Visit example.com')).toBeNull();
+ expect(findNextAutoLinkMatch('Visit https://example.rs')?.url).toBe(
+ 'https://example.rs'
+ );
+ });
+
+ it('matches common bare TLDs in common-tlds mode', () => {
+ expect(findNextAutoLinkMatch('Visit example.com', 'common-tlds')?.url).toBe(
+ 'https://example.com'
+ );
+ expect(findNextAutoLinkMatch('Visit macro.co', 'common-tlds')?.url).toBe(
+ 'https://macro.co'
+ );
+ expect(findNextAutoLinkMatch('Visit example.org', 'common-tlds')?.url).toBe(
+ 'https://example.org'
+ );
+ });
+
+ it('does not match file-like non-curated TLDs in common-tlds mode', () => {
+ expect(findNextAutoLinkMatch('Open main.rs', 'common-tlds')).toBeNull();
+ expect(findNextAutoLinkMatch('Open parser.ts', 'common-tlds')).toBeNull();
+ expect(findNextAutoLinkMatch('Open types.d.ts', 'common-tlds')).toBeNull();
+ });
+
+ it('keeps fuzzy mode available for callers that want broader matching', () => {
+ expect(findNextAutoLinkMatch('Visit example.rs', 'fuzzy')?.url).toBe(
+ 'https://example.rs'
+ );
+ });
+});
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.ts
index 8e05064613..0ff0b48830 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/links/linksPlugin.ts
@@ -36,10 +36,51 @@ const strictLinkifier = new linkify(undefined, {
fuzzyLink: false,
});
+const commonTldLinkifier = new linkify(undefined, {
+ fuzzyLink: true,
+}).tlds(
+ [
+ 'app',
+ 'biz',
+ 'ca',
+ 'co',
+ 'com',
+ 'dev',
+ 'edu',
+ 'gov',
+ 'info',
+ 'io',
+ 'me',
+ 'net',
+ 'org',
+ 'shop',
+ 'site',
+ 'store',
+ 'tv',
+ 'uk',
+ 'us',
+ 'xyz',
+ ],
+ false
+);
+
const fuzzyLinkifier = new linkify(undefined, {
fuzzyLink: true,
});
+export type AutoLinkMatchMode = 'protocol' | 'common-tlds' | 'fuzzy';
+
+function getAutoLinkifier(mode: AutoLinkMatchMode) {
+ switch (mode) {
+ case 'common-tlds':
+ return commonTldLinkifier;
+ case 'fuzzy':
+ return fuzzyLinkifier;
+ case 'protocol':
+ return strictLinkifier;
+ }
+}
+
/*
* Basic URL cleaning.
*/
@@ -123,14 +164,26 @@ function getLinkFromDom(
};
}
-function findNextMatch(text: string): Match | null {
- if (!strictLinkifier.test(text)) return null;
- const match = strictLinkifier.match(text);
+export function findNextAutoLinkMatch(
+ text: string,
+ mode: AutoLinkMatchMode = 'protocol'
+): Match | null {
+ const linkifier = getAutoLinkifier(mode);
+ if (!linkifier.test(text)) return null;
+ const match = linkifier.match(text);
if (!match) return null;
- return match[0];
+ const firstMatch = match[0];
+ if (firstMatch.schema === '') {
+ firstMatch.url = cleanURL(firstMatch.raw);
+ }
+ return firstMatch;
}
-function $handleAppendToMatch(prevSibling: AutoLinkNode, textNode: TextNode) {
+function $handleAppendToMatch(
+ prevSibling: AutoLinkNode,
+ textNode: TextNode,
+ autoLinkMatchMode: AutoLinkMatchMode
+) {
const prevText = prevSibling.getTextContent();
const nodeText = textNode.getTextContent();
@@ -142,7 +195,7 @@ function $handleAppendToMatch(prevSibling: AutoLinkNode, textNode: TextNode) {
}
const combinedText = prevText + nodeText;
- const rematch = findNextMatch(combinedText);
+ const rematch = findNextAutoLinkMatch(combinedText, autoLinkMatchMode);
if (!rematch) {
return;
@@ -165,9 +218,12 @@ function $handleAppendToMatch(prevSibling: AutoLinkNode, textNode: TextNode) {
prevSibling.setURL(rematch.url);
}
-function $matchAndCreateAutoLink(textNode: TextNode) {
+function $matchAndCreateAutoLink(
+ textNode: TextNode,
+ autoLinkMatchMode: AutoLinkMatchMode
+) {
const nodeText = textNode.getTextContent();
- const matchedLink = findNextMatch(nodeText);
+ const matchedLink = findNextAutoLinkMatch(nodeText, autoLinkMatchMode);
let currentNode = textNode;
if (!matchedLink) {
@@ -331,6 +387,7 @@ type LinkPluginProps = {
onClickLink?: (link?: ILinkInfo) => void;
onCreateLink?: (link?: ILinkInfo) => void;
closePopup?: () => void;
+ autoLinkMatchMode?: AutoLinkMatchMode;
};
function registerLinksPlugin(editor: LexicalEditor, props: LinkPluginProps) {
@@ -340,6 +397,7 @@ function registerLinksPlugin(editor: LexicalEditor, props: LinkPluginProps) {
onCreateLink: () => {},
...props,
};
+ const autoLinkMatchMode = props.autoLinkMatchMode ?? 'protocol';
let hoveredLink: ILinkInfo | undefined;
@@ -428,11 +486,11 @@ function registerLinksPlugin(editor: LexicalEditor, props: LinkPluginProps) {
}
if (prevSibling && $isAutoLinkNode(prevSibling)) {
- $handleAppendToMatch(prevSibling, textNode);
+ $handleAppendToMatch(prevSibling, textNode, autoLinkMatchMode);
}
if (!insideAnyLink) {
- $matchAndCreateAutoLink(textNode);
+ $matchAndCreateAutoLink(textNode, autoLinkMatchMode);
}
}),