Plugin rewrite link based on search for permalink

I would like to make a link-plugin, that transforms how some of my links looks. If the relative link Squat has /exercise/ as the first part, the plugin should query all _exercises for the permalink /exercise/squat/. If the squat-post has icon in the yml-frontmatter, it should output <img src="{{ icon }}"> <a href="{{ url }}">Squat</a> Can anybody help how to achieve this?

I think this should be a new jekyll tag. To be able to access page level information here, you should do

page = context.registers[:page]
# access with page['icon']

in the render method.