Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(editor): preserve file: links through rich Markdown copy and paste (#35789)

OrcaWin12b589474c23
diff --git a/src/renderer/src/components/editor/rich-markdown-extensions.ts b/src/renderer/src/components/editor/rich-markdown-extensions.ts
index be727b3eeed..fa82c1004c5 100644
--- a/src/renderer/src/components/editor/rich-markdown-extensions.ts
+++ b/src/renderer/src/components/editor/rich-markdown-extensions.ts
@@ -7,7 +7,7 @@ import {
} from '@tiptap/core'
import StarterKit from '@tiptap/starter-kit'
import { RichMarkdownTrailingParagraph } from './rich-markdown-trailing-paragraph'
-import Link from '@tiptap/extension-link'
+import { RichMarkdownLink } from './rich-markdown-link-mark'
import { Code } from '@tiptap/extension-code'
import Image from '@tiptap/extension-image'
import Placeholder from '@tiptap/extension-placeholder'
@@ -97,7 +97,7 @@ export function createRichMarkdownExtensions({
lowlight,
defaultLanguage: null
}),
- Link.configure({
+ RichMarkdownLink.configure({
openOnClick: false,
autolink: true,
linkOnPaste: true
diff --git a/src/renderer/src/components/editor/rich-markdown-file-uri-links.test.ts b/src/renderer/src/components/editor/rich-markdown-file-uri-links.test.ts
new file mode 100644
index 00000000000..4f36bdce00f
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-file-uri-links.test.ts
@@ -0,0 +1,78 @@
+// @vitest-environment happy-dom
+import { Editor, type JSONContent } from '@tiptap/core'
+import { afterEach, expect, it } from 'vitest'
+import { createRichMarkdownExtensions } from './rich-markdown-extensions'
+import { createRichMarkdownEditorCodec } from './rich-markdown-source-transport'
+
+const editors: Editor[] = []
+function createEditor(content: string | JSONContent): Editor {
+ const element = document.createElement('div')
+ document.body.append(element)
+ const editor = new Editor({
+ element,
+ extensions: createRichMarkdownExtensions({ codec: createRichMarkdownEditorCodec() }),
+ content,
+ ...(typeof content === 'string' ? { contentType: 'markdown' } : {})
+ })
+ editors.push(editor)
+ return editor
+}
+function copy(editor: Editor): string {
+ editor.commands.selectAll()
+ return editor.view.serializeForClipboard(editor.state.selection.content()).dom.innerHTML
+}
+function paste(editor: Editor, html: string): void {
+ editor.commands.selectAll()
+ editor.view.pasteHTML(html)
+}
+afterEach(() => {
+ editors.splice(0).forEach((editor) => editor.destroy())
+ document.getSelection()?.removeAllRanges()
+ document.body.replaceChildren()
+})
+
+it.each(['file:///repo/notes/guide.md#section', 'file:///C:/repo/a%20b.md#L10'])(
+ 'preserves %s through copy, paste and Markdown reopen',
+ (uri) => {
+ const source = createEditor(`Before **[guide](${uri} "Reference")** after.`)
+ const html = copy(source)
+ expect(html).toContain(`href="${uri}"`)
+ const target = createEditor('Replace me.')
+ paste(target, html)
+ const link = target.view.dom.querySelector('a')
+ expect(link?.getAttribute('href')).toBe(uri)
+ expect(link?.getAttribute('title')).toBe('Reference')
+ const reopened = createEditor(target.getMarkdown())
+ expect(reopened.view.dom.querySelector('a')?.getAttribute('href')).toBe(uri)
+ }
+)
+
+it('keeps javascript: links unavailable through copy and paste', () => {
+ const source = createEditor({
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ {
+ type: 'text',
+ text: 'guide',
+ marks: [{ type: 'link', attrs: { href: 'javascript:alert(1)' } }]
+ }
+ ]
+ }
+ ]
+ })
+ expect(source.view.dom.querySelector('a')?.getAttribute('href')).toBe('')
+ const target = createEditor('')
+ paste(target, copy(source))
+ expect(target.view.dom.querySelector('a')).toBeNull()
+ expect(target.getText()).toBe('guide')
+})
+
+it('does not turn pasted plain-text file URIs into links', () => {
+ const target = createEditor('')
+ target.view.pasteText('see file:///repo/notes/guide.md now')
+ expect(target.view.dom.querySelector('a')).toBeNull()
+ expect(target.getMarkdown()).toBe('see file:///repo/notes/guide.md now')
+})
diff --git a/src/renderer/src/components/editor/rich-markdown-link-mark.ts b/src/renderer/src/components/editor/rich-markdown-link-mark.ts
new file mode 100644
index 00000000000..50be1244101
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-link-mark.ts
@@ -0,0 +1,26 @@
+import { mergeAttributes } from '@tiptap/core'
+import Link from '@tiptap/extension-link'
+
+function isFileUri(href: unknown): href is string {
+ return typeof href === 'string' && /^file:/i.test(href.trim())
+}
+
+// Why: Tiptap blanks file: hrefs in clipboard HTML, so copy/paste dropped those links. Admitting
+// them only in HTML transfer keeps autolink and plain-URL paste rules ignoring bare file: text.
+export const RichMarkdownLink = Link.extend({
+ parseHTML() {
+ return [
+ {
+ tag: 'a[href]',
+ getAttrs: (dom) => (isFileUri(dom.getAttribute('href')) ? null : false)
+ },
+ ...(this.parent?.() ?? [])
+ ]
+ },
+ renderHTML({ HTMLAttributes, mark }) {
+ if (isFileUri(HTMLAttributes.href)) {
+ return ['a', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
+ }
+ return this.parent?.({ HTMLAttributes, mark }) ?? ['a', HTMLAttributes, 0]
+ }
+})