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(calendar): linkify bare urls in event descriptions (#6551)

gbirmanc587de2ca4dc
diff --git a/apps/web/src/features/calendar/utils/calendar-description.test.ts b/apps/web/src/features/calendar/utils/calendar-description.test.ts
index 4971981dfa..946b54df62 100644
--- a/apps/web/src/features/calendar/utils/calendar-description.test.ts
+++ b/apps/web/src/features/calendar/utils/calendar-description.test.ts
@@ -45,12 +45,47 @@ describe('sanitizeCalendarDescription', () => {
expect(sanitizeCalendarDescription(' ')).toBe('');
});
+ it('links bare urls in text so the detail view can open them', () => {
+ expect(sanitizeCalendarDescription('Join https://zoom.us/j/123 now')).toBe(
+ '<p>Join <a href="https://zoom.us/j/123">https://zoom.us/j/123</a> now</p>'
+ );
+ expect(sanitizeCalendarDescription('See macro.com for details')).toBe(
+ '<p>See <a href="https://macro.com">macro.com</a> for details</p>'
+ );
+ expect(
+ sanitizeCalendarDescription('one https://a.com two https://b.com')
+ ).toBe(
+ '<p>one <a href="https://a.com">https://a.com</a> two <a href="https://b.com">https://b.com</a></p>'
+ );
+ });
+
+ it('links bare urls inside provider html without touching existing links', () => {
+ expect(
+ sanitizeCalendarDescription(
+ '<p>Docs at https://example.com/x and <a href="https://example.com/y">here</a></p>'
+ )
+ ).toBe(
+ '<p>Docs at <a href="https://example.com/x">https://example.com/x</a> and <a href="https://example.com/y">here</a></p>'
+ );
+ });
+
+ it('links bare emails but leaves code-like tokens as text', () => {
+ expect(sanitizeCalendarDescription('Edit parser.ts before sync')).toBe(
+ '<p>Edit parser.ts before sync</p>'
+ );
+ expect(
+ sanitizeCalendarDescription('Email [email protected] to confirm')
+ ).toBe(
+ '<p>Email <a href="mailto:[email protected]">[email protected]</a> to confirm</p>'
+ );
+ });
+
it('keeps angle brackets in plain text as text', () => {
const safe = sanitizeCalendarDescription(
'Send the agenda to <[email protected]>\nOwner: <TBD>'
);
expect(safe).toBe(
- '<p>Send the agenda to &lt;[email protected]&gt;<br>Owner: &lt;TBD&gt;</p>'
+ '<p>Send the agenda to &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;<br>Owner: &lt;TBD&gt;</p>'
);
// Already-safe output must not change on the next pass.
expect(sanitizeCalendarDescription(safe)).toBe(safe);
diff --git a/apps/web/src/features/calendar/utils/calendar-description.ts b/apps/web/src/features/calendar/utils/calendar-description.ts
index fb914a9cf4..aea9698b27 100644
--- a/apps/web/src/features/calendar/utils/calendar-description.ts
+++ b/apps/web/src/features/calendar/utils/calendar-description.ts
@@ -1,3 +1,4 @@
+import { findNextAutoLinkMatch } from '@core/component/LexicalMarkdown/plugins/links/linksPlugin';
import { editorIsEmpty } from '@core/component/LexicalMarkdown/utils';
import { convertDocumentMentionsToLinks } from '@core/component/LexicalMarkdown/utils/convertDocumentMentionsToLinks';
import { getWebOrigin } from '@core/util/webOrigin';
@@ -280,9 +281,61 @@ function wrapInlineRuns(parent: Element) {
flush();
}
+/** The autolink strictness shared by the app's editors: bare hosts on common
+ * TLDs link, but code-like tokens such as `parser.ts` do not. */
+const DESCRIPTION_AUTO_LINK_MODE = 'common-tlds';
+
+/**
+ * Wrap the URLs and bare emails in a plain-text string as anchors so the detail
+ * view can open them — emails become `mailto:` links, which `openExternalUrl`
+ * routes to the in-app composer. Returns null when nothing was linked so the
+ * caller can leave the original text node untouched.
+ */
+function linkifyText(text: string, doc: Document): Node[] | null {
+ const nodes: Node[] = [];
+ let rest = text;
+ let linked = false;
+ while (rest) {
+ const match = findNextAutoLinkMatch(rest, DESCRIPTION_AUTO_LINK_MODE);
+ if (!match || match.lastIndex <= match.index) {
+ nodes.push(doc.createTextNode(rest));
+ break;
+ }
+ linked = true;
+ const before = rest.slice(0, match.index);
+ if (before) nodes.push(doc.createTextNode(before));
+ const anchor = doc.createElement('a');
+ anchor.setAttribute('href', match.url);
+ anchor.textContent = rest.slice(match.index, match.lastIndex);
+ nodes.push(anchor);
+ rest = rest.slice(match.lastIndex);
+ }
+ return linked ? nodes : null;
+}
+
+/** Turn bare URLs in text into anchors, leaving text inside existing links alone. */
+function linkifyBareUrls(body: HTMLElement) {
+ const doc = body.ownerDocument;
+ const walker = doc.createTreeWalker(body, NodeFilter.SHOW_TEXT, {
+ acceptNode: (node) =>
+ node.parentElement?.closest('a')
+ ? NodeFilter.FILTER_REJECT
+ : NodeFilter.FILTER_ACCEPT,
+ });
+ const textNodes: Text[] = [];
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
+ textNodes.push(node as Text);
+ }
+ for (const textNode of textNodes) {
+ const replacement = linkifyText(textNode.textContent ?? '', doc);
+ if (replacement) textNode.replaceWith(...replacement);
+ }
+}
+
function sanitizeDocument(doc: Document, plainText: boolean) {
if (plainText) breakLines(doc.body);
sanitizeChildren(doc.body);
+ linkifyBareUrls(doc.body);
wrapInlineRuns(doc.body);
}