chhoumann / quickadd

QuickAdd for Obsidian
https://quickadd.obsidian.guide
MIT License
1.52k stars 136 forks source link

[BUG] js quickadd String `replace`, `test` function and other manipulations assumes literal `{{VALUE}}`, not selection #672

Open ces3001 opened 5 months ago

ces3001 commented 5 months ago

Describe the bug Using js quickadd in the Capture Fromat: where val = “{{VALUE}}”, val.replace function assumes literal {{VALUE}}, not selection, if grep matches something in the literal string ”{{VALUE}}”, otherwise leaves selection. If you simply return val, it returns the selection.

To Reproduce

let v = "{{VALUE}}";
return v.replace(/[VWXYZwxyz]/g, '9');

will incorrectly return {{9ALUE}} as the V matches in {{VALUE}}. {{VALUE}} seemingly doesn’t get replaced with the selected text, but is taken literally.

Whereas, omitting the V in the grep pattern:

let v = "{{VALUE}}";
return v.replace(/[WXYZwxyz]/g, '9');

This will do nothing, and correctly return the selected text (not {{VALUE}}), because /[WXYZwxyz]/g doesn’t match anything in the string {{VALUE}}.

Expected behavior replace should use the replacement selection string. In the example above, depending on the selection. If selection = VWA, then replaced with 99A, not {{9ALUE}}

Desktop (please complete the following information):

Additional context I’m trying to implement a “Convert Phone to Link” function:

function convertPhoneNumberToLink(linkNumber) {
  // Remove all non-alphanumeric characters except +
  linkNumber = linkNumber.replace(/[^a-zA-Z0-9+]/g, '');

  // Replace letters with their corresponding numbers
linkNumber = linkNumber.replace(/[ABCabc]/g, '2');
linkNumber = linkNumber.replace(/[DEFdef]/g, '3');
linkNumber = linkNumber.replace(/[GHIghi]/g, '4');
linkNumber = linkNumber.replace(/[JKLjkl]/g, '5');
linkNumber = linkNumber.replace(/[MNOmno]/g, '6');
linkNumber = linkNumber.replace(/[PQRSpqrs]/g, '7');
linkNumber = linkNumber.replace(/[TUVtuv]/g, '8');
linkNumber = linkNumber.replace(/[WXYZwxyz]/g, '9');

  return `tel:${linkNumber}`
}
return `[{{VALUE}}](${convertPhoneNumberToLink("{{VALUE}}")})`;

This will always return [ACTUAL PHONE NUMBER SELECTED](tel:82583) because 82582 is VALUE on the numeric keypad.

ces3001 commented 5 months ago

ps. replacing the last line with

let v = "{{VALUE}}";
return `[${v}](${convertPhoneNumberToLink(v)})`;

Still gives the wrong result. [+1-800-CALL-APPL](tel:+182583) it doesn’t change the bug.

ces3001 commented 5 months ago

Please excuse the multiple follow-ups. It seems to me that QuickAdd isn’t replacing the {{VALUE}} string in the code before the execution of the javascript, but only replacing it after in the javascript's output.

So it’s perhaps a bug, perhaps a design decision. For now, I guess, we request it as feature -> https://github.com/chhoumann/quickadd/issues/483