Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ces3001 opened this issue Apr 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ces3001
Copy link

ces3001 commented Apr 12, 2024

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):

  • OS: macOS
  • Obsidian: Version 1.5.12
  • QuickAdd 1.6.1

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 ces3001 added the bug Something isn't working label Apr 12, 2024
@ces3001 ces3001 changed the title [BUG] js quickadd String replace function assumes literal {{VALUE}}, not selection [BUG] js quickadd String replace, test function and other manipulations assumes literal {{VALUE}}, not selection Apr 12, 2024
@ces3001
Copy link
Author

ces3001 commented Apr 12, 2024

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
Copy link
Author

ces3001 commented Apr 13, 2024

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 -> #483

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant