fix(markdown): select exactly the url when wrapping selected text in a link (#9208)

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
This commit is contained in:
Sai Asish Y 2026-07-21 02:29:33 -07:00 committed by GitHub
parent 775d202fd4
commit b26fb1093f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 11 deletions

View file

@ -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://',
);
});
});

View file

@ -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,