How do I automatically collapse linked mentions?

The core plugins aren’t open source. The way to handle this would probably be with a plugin (writing one if one doesn’t exist; if one exists it’s probably mentioned in that feature request thread). I don’t know if plugins can modify the core plugins, but if not I imagine reimplementing wouldn’t be terrible since it’s not a very complicated feature.