import { pasteHTML, pasteText } from '@prosekit/core/test' import { describe, expect, it } from 'vitest' import { setupTest } from '../testing/index.ts' describe('defineLinkPasteRule', () => { const { editor, n, m } = setupTest() it('should convert URLs to links when pasting plain text', () => { const doc = n.doc(n.p('')) editor.set(doc) pasteHTML(editor.view, '

Visit https://example.com for more info

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Visit ', m.link({ href: 'https://example.com' }, 'https://example.com'), ' for more info', )).toJSON(), ) }) it('should handle multiple URLs in pasted text', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Check https://google.com and https://github.com

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Check ', m.link({ href: 'https://google.com' }, 'https://google.com'), ' and ', m.link({ href: 'https://github.com' }, 'https://github.com'), )).toJSON(), ) }) it('should handle URLs without protocol', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Visit example.com for details

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Visit ', m.link({ href: 'example.com' }, 'example.com'), ' for details', )).toJSON(), ) }) it('should not modify text without URLs', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteText(editor.view, 'This is just plain text without any links') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p('This is just plain text without any links')).toJSON(), ) }) it('should preserve existing marks when adding links', () => { const doc = n.doc(n.p('')) editor.set(doc) pasteHTML(editor.view, '

Visit https://example.com for info

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Visit ', m.bold(m.link({ href: 'https://example.com' }, 'https://example.com')), ' for info', )).toJSON(), ) }) it('should not convert URLs inside existing links', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Visit https://example.com for more

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( m.link({ href: 'https://google.com' }, 'Visit https://example.com for more'), )).toJSON(), ) }) it('should not convert URLs inside code blocks', () => { const doc = n.doc(n.codeBlock('')) editor.set(doc) pasteText(editor.view, 'https://example.com') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.codeBlock('https://example.com')).toJSON(), ) }) it('should not convert URLs inside code marks', () => { const doc = n.doc(n.p('')) editor.set(doc) pasteHTML(editor.view, '

https://example.com

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p(m.code('https://example.com'))).toJSON(), ) }) it('should handle URLs with paths and query parameters', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Visit https://example.com/path?param=value#section

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Visit ', m.link({ href: 'https://example.com/path?param=value#section' }, 'https://example.com/path?param=value#section'), )).toJSON(), ) }) it('should handle URLs at the beginning of text', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

https://example.com is a great site

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( m.link({ href: 'https://example.com' }, 'https://example.com'), ' is a great site', )).toJSON(), ) }) it('should handle URLs at the end of text', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Check out https://example.com

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Check out ', m.link({ href: 'https://example.com' }, 'https://example.com'), )).toJSON(), ) }) it('should handle URLs with punctuation after them', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Visit https://example.com, it is great!

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.p( 'Visit ', m.link({ href: 'https://example.com' }, 'https://example.com'), ', it is great!', )).toJSON(), ) }) it('should handle URLs inside nested block structures', () => { const doc = n.doc(n.p('
')) editor.set(doc) pasteHTML(editor.view, '

Quote: Visit https://example.com for more info

') expect(editor.view.state.doc.toJSON()).toEqual( n.doc(n.blockquote(n.p( 'Quote: Visit ', m.link({ href: 'https://example.com' }, 'https://example.com'), ' for more info', ))).toJSON(), ) }) })