Can I use the code that marks external links to rewrite them?

Hmm... Unless I'm missing something it looks like Plone is using js to do this anyway (Plone 5 with mockup at least) https://github.com/plone/mockup/tree/master/mockup/patterns/markspeciallinks