tyler-dot-earth / obsidian-blockreffer

An Obsidian plugin to search and embed blocks with ^block-references (aka ^block-ids)
Other
4 stars 1 forks source link

Support searching and replacing block links #6

Open wenlzhang opened 2 months ago

wenlzhang commented 2 months ago

Sometimes, we create block links within a note and then insert these block links into other notes. Occasionally, we might split this note into smaller notes, which means that the block links in other notes referencing the original note become invalid. So, I was wondering if there’s a way to easily replace the old block links with the new block link when searching for block IDs.

tyler-dot-earth commented 2 months ago

Hi @wenlzhang. Thank you for the submission - that is a very understandable use-case.

I'm unsure if that is within the scope of this plugin, though I'm happy to keep this issue open for consideration.

In the meantime, a suggestion: you may try to utilize a regex search-and-replace plugin - it should be able to achieve your goal of splitting blocks.

Here's an example regex: https://regex101.com/r/830xwk/2

It should replace ![[foo#^bar1234]] with 2 lines:

![[foo#^bar1234]]
![[foo#^bar5555]]

It does this by using this for identifying different parts of the string: (!\[\[foo#\^)([^\]]+)(\]\]) and replacing parts using this: $1$2$3\n$1bar5555$3

The regex101 link above explains the parts in the sidebar: image

So $1$2$3\n$1bar5555$3 means: "replace with the content of $1 (group 1) + $2 (group 2) + $3 (group 3) + \n (new line) + $1 (group 1) + 5555 + $3 (group 3)"

wenlzhang commented 2 months ago

@tyler-dot-earth Thanks a lot for the detailed explanation!

Maybe I did not explain clearly before. What if I want to change the file name but keep the block ID? For example, I may want to replace the first block link with the second one:

![[fooA#^bar1234]]
![[fooB#^bar1234]]

Do you have any suggestions on how to achieve this?

tyler-dot-earth commented 2 months ago

@wenlzhang Here's an example to achieve that: https://regex101.com/r/eiHgba/1

(!\[\[)([^#]+)(#\^)([^\]]+)(\]\])
$1fizz$3$4$5\n$1buzz$3bar5555$5

before:

![[foo#^bar1234]]

after:

![[fizz#^bar1234]]
![[buzz#^bar5555]]

EDIT: i changed the block ID in the example above but, you could keep it the same like this:

$1fizz$3$4$5\n$1buzz$3$4$5

That said, i'd find it "wrong-ish" to have two blocks with the same block ID. I think block identifiers should be unique as a general rule of thumb.