Script for auto-creating internal links

I’ve been working on this script (using Kotlin Scripting for the first time) for a few days, but I’m a bit stuck because I’m not able to make a proper Regex to safely replace the matches.

The idea is simple, it iterates all the notes looking for their titles inside others, if they match they are replaced by an internal link.

At the moment it works fine for lines without other internal/external links or block/inline code (anyway was able to replace +300 missing internal links in my vault :grinning:). It also creates a .log file to perform those replacements manually, although it’s not the most convenient way.

I actually come here looking for help so it’d be great if someone gives a hand with this (it’s easily refined).

PS: programming language changes are also welcome.