I'm currently looking to fix issue 4132, and I think the best way to make sure syntax highlighting is consistent between the code editor and the rendered markdown would be to use the same module to highlight code. CodeMirror has an add-on called runMode which allow to do it without the whole editor. However, I couldn't find a way to automatically highlight code when the language isn't specified. CodeMirror doesn't seem to have this feature (even in the editor).
I would like to have some feedback on this.
I also think that we should extract the highlight function in its own module for easier reusability and testing.