Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(web): avoid duplicate Tiptap link extension (#262)

I'm Groot 🌳87fbf976c46f
diff --git a/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.test.ts b/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.test.ts
new file mode 100644
index 00000000..92b88ba8
--- /dev/null
+++ b/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.test.ts
@@ -0,0 +1,103 @@
+import assert from 'node:assert/strict';
+import { afterEach, beforeEach, describe, it } from 'node:test';
+import { Editor } from '@tiptap/core';
+import Link from '@tiptap/extension-link';
+import StarterKit from '@tiptap/starter-kit';
+import { JSDOM } from 'jsdom';
+import { issueEditorStarterKitOptions } from './IssueMarkdownEditor';
+import { openLinkOnModifierClick } from '../../utils/modifierClickLink';
+
+let dom: JSDOM;
+let originalGlobalDescriptors: Map<string, PropertyDescriptor | undefined>;
+
+beforeEach(() => {
+ originalGlobalDescriptors = new Map(
+ ['window', 'document', 'navigator'].map((name) => [
+ name,
+ Object.getOwnPropertyDescriptor(globalThis, name),
+ ]),
+ );
+ dom = new JSDOM('<!doctype html><div></div>');
+ Object.defineProperties(globalThis, {
+ window: { configurable: true, value: dom.window },
+ document: { configurable: true, value: dom.window.document },
+ navigator: { configurable: true, value: dom.window.navigator },
+ });
+});
+
+afterEach(() => {
+ dom.window.close();
+ for (const [name, descriptor] of originalGlobalDescriptors) {
+ if (descriptor) Object.defineProperty(globalThis, name, descriptor);
+ else Reflect.deleteProperty(globalThis, name);
+ }
+});
+
+describe('IssueMarkdownEditor extensions', () => {
+ it('registers the configured link extension once', () => {
+ const editor = new Editor({
+ extensions: [
+ StarterKit.configure(issueEditorStarterKitOptions),
+ Link.configure({ openOnClick: false, autolink: true }),
+ ],
+ content: '',
+ });
+
+ assert.equal(
+ editor.extensionManager.extensions.filter((extension) => extension.name === 'link').length,
+ 1,
+ );
+ editor.destroy();
+ });
+
+ it('opens links only with the platform modifier', () => {
+ const root = dom.window.document.querySelector('div')!;
+ root.innerHTML = '<a href="https://example.com/docs">Docs</a>';
+ const link = root.querySelector('a')!;
+ const opened: Array<unknown> = [];
+ dom.window.open = (...args: Parameters<typeof window.open>) => {
+ opened.push(args);
+ return null;
+ };
+
+ const plain = new dom.window.MouseEvent('click', {
+ bubbles: true,
+ cancelable: true,
+ button: 0,
+ });
+ link.dispatchEvent(plain);
+ assert.equal(openLinkOnModifierClick(plain, root), false);
+
+ for (const modifier of [{ metaKey: true }, { ctrlKey: true }]) {
+ const event = new dom.window.MouseEvent('click', {
+ bubbles: true,
+ cancelable: true,
+ button: 0,
+ ...modifier,
+ });
+ link.dispatchEvent(event);
+ assert.equal(openLinkOnModifierClick(event, root), true);
+ assert.equal(event.defaultPrevented, true);
+ }
+
+ assert.deepEqual(opened, [
+ ['https://example.com/docs', '_blank', 'noopener,noreferrer'],
+ ['https://example.com/docs', '_blank', 'noopener,noreferrer'],
+ ]);
+ });
+
+ it('does not open unsafe link protocols', () => {
+ const root = dom.window.document.querySelector('div')!;
+ root.innerHTML = '<a href="javascript:alert(1)">Unsafe</a>';
+ const link = root.querySelector('a')!;
+ const event = new dom.window.MouseEvent('click', {
+ bubbles: true,
+ cancelable: true,
+ metaKey: true,
+ });
+ link.dispatchEvent(event);
+
+ assert.equal(openLinkOnModifierClick(event, root), false);
+ assert.equal(event.defaultPrevented, false);
+ });
+});
diff --git a/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.tsx b/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.tsx
index 01ff69a0..9a77ec4e 100644
--- a/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.tsx
+++ b/apps/web/src/features/issue/components/editor/IssueMarkdownEditor.tsx
@@ -13,6 +13,7 @@ import { SlashCommand } from '@/lib/tiptap-slash-command';
import { MarkdownTable } from '../../utils/tiptap-table';
import { Video } from '../../utils/tiptap-video';
import { attachmentHtml, type Embeddable } from '../../utils/attachmentEmbed';
+import { openLinkOnModifierClick } from '../../utils/modifierClickLink';
import EditorImagePicker from './EditorImagePicker';
import EditorSelectionMenu from '@/components/common/editor/EditorSelectionMenu';
import EditorTableMenu from '@/components/common/editor/EditorTableMenu';
@@ -23,6 +24,11 @@ import { useTranslations } from 'next-intl';
// highlightAuto, so there is no language picker.
const lowlight = createLowlight(common);
+export const issueEditorStarterKitOptions = {
+ codeBlock: false,
+ link: false,
+} as const;
+
// A minimal WYSIWYG editor over markdown text — no persistent toolbar, just a
// bubble menu on selection and a "/" command list (Linear/Notion-style). Content
// in and out is plain markdown (via tiptap-markdown), matching how descriptions
@@ -84,7 +90,7 @@ export default function IssueMarkdownEditor({
editable,
extensions: [
// Replaces StarterKit's plain code block, keeping the node name codeBlock.
- StarterKit.configure({ codeBlock: false }),
+ StarterKit.configure(issueEditorStarterKitOptions),
CodeBlockLowlight.configure({ lowlight }),
Placeholder.configure({ placeholder }),
Link.configure({ openOnClick: false, autolink: true }),
@@ -121,6 +127,9 @@ export default function IssueMarkdownEditor({
// flex-1 so the typing area covers a container taller than the text.
class: 'md-content flex-1 focus:outline-none',
},
+ handleClick(view, _pos, event) {
+ return openLinkOnModifierClick(event, view.dom);
+ },
// Files dropped from the OS are uploaded, then inserted at the drop
// position. Internal moves and attachment-card drags (which carry
// text/html, not files) fall through to tiptap's default handling.
diff --git a/apps/web/src/features/issue/utils/modifierClickLink.ts b/apps/web/src/features/issue/utils/modifierClickLink.ts
new file mode 100644
index 00000000..a1979357
--- /dev/null
+++ b/apps/web/src/features/issue/utils/modifierClickLink.ts
@@ -0,0 +1,21 @@
+const OPENABLE_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'tel:']);
+
+export function openLinkOnModifierClick(event: MouseEvent, root: HTMLElement): boolean {
+ if (event.button !== 0 || (!event.metaKey && !event.ctrlKey)) return false;
+ const target = event.target as Element | null;
+ if (!target || typeof target.closest !== 'function') return false;
+ const link = target.closest<HTMLAnchorElement>('a[href]');
+ if (!link || !root.contains(link)) return false;
+
+ let url: URL;
+ try {
+ url = new URL(link.href, window.location.href);
+ } catch {
+ return false;
+ }
+ if (!OPENABLE_PROTOCOLS.has(url.protocol)) return false;
+
+ event.preventDefault();
+ window.open(url.href, link.target || '_blank', 'noopener,noreferrer');
+ return true;
+}