Open YousufSSyed opened 1 year ago
Thanks for the question, but the answer is no, regex replacing is not supported yet. And I think what you want may be this https://github.com/Gru80/obsidian-regex-replace
Do you plan on supporting regex in the future? What I want is a plugin that can automatically replace text as I'm typing, and with regex if I want to. I already installed that plugin awhile ago and it doesn't do that. It's a find and replace UI in a popup modal.
Yeah, I do have a plan for that, but there are some technical problems in implementing it efficiently. I will think about it, please stay tuned.
OK.
Yeah, I do have a plan for that, but there are some technical problems in implementing it efficiently. I will think about it, please stay tuned.
What about is challenging, is it more complicated then just passing the text through a regex library or functions?
The main issue is performance. A regex pattern can match a long text, so how much text should be extracted to be parsed when typing a char? The entire line seems too much for me, I hope the text should be as small as possible. And in my opinion, it is better to do the pattern match from the end char to the start char one by one so that we can eliminate those patterns without any hope. In the current codebase, it is achieved by a reversed Trie, but how to organize all regex patterns?
Leaving a comment here as a reminder. One possible way is first to ignore all captures, combine all patterns into one state machine, and walk the input string through it to find the matched index. And extract the original regular expression to transform the input text once again.
The main issue is performance. A regex pattern can match a long text, so how much text should be extracted to be parsed when typing a char? The entire line seems too much for me, I hope the text should be as small as possible. And in my opinion, it is better to do the pattern match from the end char to the start char one by one so that we can eliminate those patterns without any hope. In the current codebase, it is achieved by a reversed Trie, but how to organize all regex patterns?
Any updates on this?
Sorry for the delay. I'm working on this recently and I have a question to discuss.
In your example, r/obsidian
is wanted to be recognized by (r/.*)
as typing, right? But when o in r/o
is being typed, r/o
itself is captured by pattern (r/.*)
. How does the program know r/obsidan
is the real target? Maybe some specific suffix will help? What do you think? @YousufSSyed
I think its fine if the plugin supports as much regex as possible. I only need to capture that patteron (r/.*)
, I wouldn't write out r/o
in practice, but its fine if that pattern matches both r/o
and r/obsidian
. If I want it to capture a more specific pattern, then I'd specify another regex to use.
But would that pattern only match if it started with r/
? That's what I want, and if I wanted it to capture r/.*
anywhere in a phrase (an individual phrase that doesn't contain spaces), then I'd write something like (.+?)(r/.*)
, I could do something with $1 or not and even remove the () from it.
It seemed that I didn't make myself clear.
The feature we want is automatically replace text as I'm typing
, as you said.
Now let's imagine that the plugin allows us to establish a rule like (r/.+) -> [$1](https://reddit.com/$1)
.
By that rule, we expect that when writing down r/obsidianmd
, magically, it is replaced by [r/obsidianmd](https://reddit.com/r/obsidianmd)
, or when writing down r/nowhere
, it is replaced by [r/nowhere](https://reddit.com/r/nowhere)
.
The problem is, if we want to write r/obsidianmd
, we have to write r/o
first, but the r/o
is also matched with the pattern (r/.+)
, so how to prevent the plugin from replacing it with [r/o](https://reddit.com/r/o)
automatically?
The find and replace could be triggered when a space is added, or .
(a period and a space). If the regex includes a space, then it could replace that pattern too after its triggered. For example, if I type "Typing Transformer" to be replaced with "TypingTransformer" then it could trigger after I write a space after "Transfomer"
Is it possible to only look at text the user had just typed, so there are no unexpected retroactive replaces?
So you are comfortable with adding a specific suffix, like .
, to trigger the replacement, right?
And what does only look at text the user had just typed
mean, more examples?
I said .
because it should do the replacement after the sentence ends, and it keeps the .
.
What the user had typed since opening that note or switching to that tab. It might be problematic to do replacements on all the text in the note written up to that point, especially if the note is really long. Either Typing Transformer doesn't make replacement on text written before either event, or there's a setting where the users choose to.
Good point, and yes, we have to control the length of the text to be checked. It is a user problem and also a performance problem if we have a lot of regex.
What the user had typed since opening that note or switching to that tab. It might be problematic to do replacements on all the text in the note written up to that point, especially if the note is really long. Either Typing Transformer doesn't make replacement on text written before either event, or there's a setting where the users choose to.
Is it acceptable for you that the replacement happens when finishing a sentence? Ah, never thought that before, it might be a much simpler approach…
I said . because it should do the replacement after the sentence ends, and it keeps the . .
maybe we can move to the discussion page, https://github.com/aptend/typing-transformer-obsidian/discussions/71, if anything new comes up
I'll reiterate, it could be done after a word / phrase is completed when a space is typed after it, and or when a sentence is completed.
What I'd like to do is replace a phrase like
r/obsidian
to[r/obsidianmd](https://reddit.com/r/obsidianmd
. I would match(r/.*)
and replace with[$1](https://reddit.com/$1)
.