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

Custom regexp flags for RegExp Replace In Note Button Action #372

Closed
3 of 7 tasks
IAmWhitBran opened this issue Jul 2, 2024 · 4 comments
Closed
3 of 7 tasks

Custom regexp flags for RegExp Replace In Note Button Action #372

IAmWhitBran opened this issue Jul 2, 2024 · 4 comments
Labels
feature request New feature or request

Comments

@IAmWhitBran
Copy link

Please fill out these Check-boxes

  • I checked that the plugin is up to date
  • The issue persist with all other plugins and themes disabled

This Issue Occurs on

  • Windows
  • Linux
  • macOS
  • Android
  • iOS

Plugin Version

1.1.2

Describe the Issue

The regexpreplaceinnote action does not seem to work as expected. When pressing the button to replace the regex in the file, the expression in the button recognises and replaces itself, often breaking the button syntax.
Even the example in the docs page doesn't function correctly.
https://www.moritzjung.dev/obsidian-meta-bind-plugin-docs/reference/buttonactions/regexpreplaceinnote/

Steps to Reproduce

  1. Open a new note
  2. paste the following
```meta-bind-button
style: primary
label: RexExp Replace In Note
action:
  type: "regexpReplaceInNote"
  regexp: "(TODO: .*)\\n"
  replacement: "$1 - Done"
```

TODO: ...
TODO: something
TODO: something else
  1. press the button

MetabindExample

Expected Behavior

I would expect the button to not replace the regex in itself.

As an aside, I have tried changing the expression to handle this scenario using the regex start and end of string tokens (^ and $) as well as trying to set the flags (g m i e s etc) but can't seem to work out if / how these would be applied. Could the docs be updated with more complex examples that cover setting expression flags?
image
image

@IAmWhitBran IAmWhitBran added the bug Undesired behavior caused by this plugin label Jul 2, 2024
@IAmWhitBran
Copy link
Author

After more trial and error I got a working regex that won't replace itself by prefixing the query with (?:\\n|\\r|\\n\\r). This is a pretty rough work around so it would be good if there was a way to avoid the button replacing regex within itself.

I also think that it would be immensly helpful to add the m flag to this line:

content = content.replace(new RegExp(action.regexp, 'g'), action.replacement);

Though I appreciate it's a potential breaking change to existing expressions, so maybe the flags could be set in the button builder instead? (Which would allow for the other regex flags to be used if people needed)

@mProjectsCode
Copy link
Owner

mProjectsCode commented Jul 5, 2024

A flags option sounds like a good idea, with the fallback being g.

[...] it would be good if there was a way to avoid the button replacing regex within itself.

That does not sound feasible to me. The plugin would need to somehow modify the regexp to make it not match the button and I have no idea how I would do that. Another thing that complicates this is that not every button knows where it is located in the file (due to limited information available in the obsidian API).

@IAmWhitBran
Copy link
Author

That does not sound feasible to me. The plugin would need to somehow modify the regexp to make it not match the button

I completely appreciate this and had the same thought myself.

I have no idea how I would do that

My hope was some wizardry could be done with something similar to the "replace self" functionality (since this is how I am handling it at the moment).

I'll admit I've not looked into how it works, but the replace self means the button has someway to identify itself as a button(?). My thinking is that a copy of the original button could be taken, regex replace run, then the original button inserted back over the resulting button...
Still, just a suggestion, less of an issue I think.

Flags would go a long way to helping with this.

@mProjectsCode
Copy link
Owner

My thinking is that a copy of the original button could be taken, regex replace run, then the original button inserted back over the resulting button...

This does not work because we do not know how many lines above the button have been changed. E.g. the regex could insert 5 lines above the button position (which we can't know reliably) and if we try to re-insert the button we reinsert it at the wrong place.

@mProjectsCode mProjectsCode changed the title RegExp Replace In Note Button Action Custom regexp flags for RegExp Replace In Note Button Action Jul 5, 2024
@mProjectsCode mProjectsCode added feature request New feature or request and removed bug Undesired behavior caused by this plugin labels Jul 5, 2024
mProjectsCode added a commit that referenced this issue Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants