diff --git a/src/app/ui/inline-markdown/markdown-toolbar.util.spec.ts b/src/app/ui/inline-markdown/markdown-toolbar.util.spec.ts index bd6746f91d..d18c26122d 100644 --- a/src/app/ui/inline-markdown/markdown-toolbar.util.spec.ts +++ b/src/app/ui/inline-markdown/markdown-toolbar.util.spec.ts @@ -316,8 +316,15 @@ describe('markdown-toolbar.util', () => { it('should use selection as link text', () => { const result = insertLink('hello world', 0, 5); expect(result.text).toBe('[hello](https://) world'); - expect(result.selectionStart).toBe(9); // URL position - expect(result.selectionEnd).toBe(17); // URL selected + expect(result.selectionStart).toBe(8); // URL position + expect(result.selectionEnd).toBe(16); // URL selected + }); + + it('should select exactly the url placeholder', () => { + const result = insertLink('hello world', 0, 5); + expect(result.text.substring(result.selectionStart, result.selectionEnd)).toBe( + 'https://', + ); }); }); @@ -332,8 +339,15 @@ describe('markdown-toolbar.util', () => { it('should use selection as alt text', () => { const result = insertImage('hello world', 0, 5); expect(result.text).toBe('![hello](https://) world'); - expect(result.selectionStart).toBe(10); // URL position - expect(result.selectionEnd).toBe(18); // URL selected + expect(result.selectionStart).toBe(9); // URL position + expect(result.selectionEnd).toBe(17); // URL selected + }); + + it('should select exactly the url placeholder', () => { + const result = insertImage('hello world', 0, 5); + expect(result.text.substring(result.selectionStart, result.selectionEnd)).toBe( + 'https://', + ); }); }); diff --git a/src/app/ui/inline-markdown/markdown-toolbar.util.ts b/src/app/ui/inline-markdown/markdown-toolbar.util.ts index f1db6893de..72d4f141e3 100644 --- a/src/app/ui/inline-markdown/markdown-toolbar.util.ts +++ b/src/app/ui/inline-markdown/markdown-toolbar.util.ts @@ -488,10 +488,9 @@ export const insertLink = ( const linkMarkdown = `[${selectedText}](${url})`; const newText = text.substring(0, selectionStart) + linkMarkdown + text.substring(selectionEnd); - // Place cursor at URL position: [ + text + ]( = 1 + text.length + 2 = text.length + 3 - // But we want cursor AFTER the opening paren, which is at position: 1 + text.length + 2 = text.length + 3 - // Test expects: for "hello" (5 chars), URL starts at 9 = 0 + 5 + 4 - const urlStart = selectionStart + selectedText.length + 4; // After "[text](" + // Select the url so it is ready to be replaced. The url sits after "[text](", + // which is 1 ("[") + text.length + 2 ("](") = text.length + 3 characters. + const urlStart = selectionStart + selectedText.length + 3; // After "[text](" return { text: newText, selectionStart: urlStart, @@ -523,9 +522,9 @@ export const insertImage = ( const imageMarkdown = `![${selectedText}](${url})`; const newText = text.substring(0, selectionStart) + imageMarkdown + text.substring(selectionEnd); - // Place cursor at URL position: ![ + text + ]( = 2 + text.length + 2 = text.length + 4 - // Test expects: for "hello" (5 chars), URL starts at 10 = 0 + 5 + 5 - const urlStart = selectionStart + selectedText.length + 5; // After "![text](" + // Select the url so it is ready to be replaced. The url sits after "![text](", + // which is 2 ("![") + text.length + 2 ("](") = text.length + 4 characters. + const urlStart = selectionStart + selectedText.length + 4; // After "![text](" return { text: newText, selectionStart: urlStart,