mirror of
https://github.com/johannesjo/super-productivity.git
synced 2026-08-01 20:12:11 +00:00
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:
parent
775d202fd4
commit
b26fb1093f
2 changed files with 24 additions and 11 deletions
|
|
@ -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(' 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://',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = ``;
|
||||
const newText =
|
||||
text.substring(0, selectionStart) + imageMarkdown + text.substring(selectionEnd);
|
||||
// Place cursor at URL position: , URL starts at 10 = 0 + 5 + 5
|
||||
const urlStart = selectionStart + selectedText.length + 5; // After " + text.length + 2 ("](") = text.length + 4 characters.
|
||||
const urlStart = selectionStart + selectedText.length + 4; // After "![text]("
|
||||
return {
|
||||
text: newText,
|
||||
selectionStart: urlStart,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue