Note Composer: links to blocks and headers should be updated when extracting

@Klaas I will try to answer without too much of a diversion. If it is, I don’t mind deleting the post.

Anyways, for this specific use case @a2jc4life was doing, I was recommending VSCode for a simple search and replace across the vault providing the exact link to the heading for the search and the exact link to the new note for the replace.

I should have probably been more clear in my suggestion of using regex/capture groups with search and replace. The tactic has proved extremely helpful for me when renaming things en masse when some more complexity is required.

Although it may seem very niche, I will provide an exact example of a use case I used it for a couple months ago in the process of creating an offline version of the MAXScript documentation. First I copied and pasted each page into a new note named with the correct name. This was a good start, but all the links between the various pages were formatted as markdown links to the corresponding webpages. I obviously wanted the links to instead go to the corresponding notes inside my vault. Doing this required the use of capture groups.

I wanted to search for the literal [ so I had to start by escaping it with a \ which made the beginning of my expression \[ and this is required because brackets serve a different purpose in regex.

I then wanted to capture the name of the page within the brackets so that I could use it in my replace expression. To do this, I added an opening parentheses, which I would have to soon close, but not yet. At this point the expression is \[(

Inside the parentheses, I added .* which will basically take the characters you give it until it finds the next search character after the closing parentheses. At this point the expression is \[(.*)

You may want to look into the use of the ? as well because it can be useful. There are plenty of guides. Anyways, now I had to add the closing bracket, again escaping it. The expression is now \[(.*)\]

Since these were markdown links to webpages, I also wanted to search for the opening parentheses, the website url text, and the closing parentheses. With the same methods just described, I use the .* within the parentheses. But since I want literal parentheses, not a capture group I escape the opening and closing parentheses. The expression is now [(.*)\]\(.*\)

When doing this, my plan was initially to simply use the replace expression of [[$1]] thus creating wiki links for all the links across the vault based on the page name, which matched the names for my notes, as the $1 replaces whatever is contained within the first capture group parentheses. Unfortunately, I remembered that while I was copy pasting the documentation pages there were many pages which had colons in their names, which I couldn’t use because they are illegal characters in Obsidian note names. I had decided to instead use double hyphens in place of the colon. This was helpful since there were not any other occurrences of double hyphens in note names for the various pages.

So, I adapted my search expression I explained above into the following \[(.*):(.*)\]\(.*\)

Now, since I had split the part of the page name on the left side of the colon into a separate capture group from what is on the right side of the colon, I had to adapt my replace expression into [[$1--$2]]

At this point I finally made sure to check the .* icon next to the search field for the replace in all files. This turns on regex for the search and replace. Of course I had VSCode set with the correct folder within my vault folder as open. I ran the search [(.*):(.*)\]\(.*\) replacing with [[$1--$2]]

This took care of the note names with colons and correctly renamed them all based on my adopted convention. Then, I finally used the first expression to search for just [(.*)\]\(.*\) and replace it with just [[$1]] which took care of what was remaining, the pages without colons in their names.

As a side note, if I wanted, I could have put the url in a separate capture group and replace with that in addition to the wiki link, thus also having the web link. I should also add that I kind of simplified what I did here for the sake of clarity. For example, in actuality, within my search I included the beginning part of the url that was consistently used. This way, I knew I wasn’t breaking links to other external websites within the documentation. All kinds of surprises can pop up when you are dealing with the unknown so I recommend being very careful. I also fairly closely reviewed the list of occurrences before committing; and there were many occurrences of many thousands of links. Again, obviously I made backups, and opted to do this in a dedicated vault.

In the end, this and other tactics saved me so much time and was so effective that I also created offline documentation vaults for 3ds Max, tyFLOW, Zbrush, Arnold, V-Ray, etc. I surely couldn’t have done it otherwise. And when issues arise, it feels like there is usually a way to make things work. For example, occasionally a while back when using the Local Images plugin, I ended up with funky broken links and I was able fix them all in one swoop.

Sorry for the lengthy description. When I type things out on my phone they usually go long. The expressions should be correct as far as I can remember. Even though you were asking about a different scenario, it sounded like you might be interested in something like this. Hopefully this is of some use.

Good luck!

1 Like