There was an attempt a while ago to allow modules from the Scribunto
extension (which are written in Lua, not plain wikitext, though) to
render as images: https://gerrit.wikimedia.org/r/#/c/113759/
There's also https://www.mediawiki.org/wiki/Extension:GraphViz
be confused with Extension:Graph), which renders images from wikitext
and uploads them back to the wiki as regular files in File: namespace.
Perhaps one of these will serve as inspiration :)