SE currently use Prettify for syntax highlighting. However, there are still some languages which have no support from default Prettify, or by the extensions that SE may be using. If I wanted to test or use an extension to Prettify on SE sites, how can I do so?

I tried, manually using the developer console, to append a <script> tag containing Prettify extension. That worked when I tried it on a post which used syntax highlighting. On a post which didn't have any syntax-highlighted parts, I get this error:

vim.js?_=1461171970788:20 Uncaught ReferenceError: PR is not defined

Now, how do I apply the new extension to a code block on that page? Manually changing the pre tag's class attribute doesn't seem to make an difference.

Turns out, that was because I forgot to append lang- to the language name. This finally worked:

StackExchange.using('prettify', function () {a = document.createElement('script'); a.src="//…/foo.js"; $('head').append(a);})

And then <!-- language: foo --> markers started working.

  • Not sure what you're actually trying but the full un-minified script used by SE is here. Maybe you can figure out if there is something changed or hidden that you expect to be available. – rene Apr 20 '16 at 17:19
  • @rene turns out that question had nothing highlighted and so the script wasn't loaded at all. I tried on another post, and the script loaded fine. – muru Apr 20 '16 at 17:24

You can make sure Prettify is loaded by invoking StackExchange.using('prettify', <callback>) in your script, which will then guarantee that PR is available in the global scope.

I did this a while ago as part of a quick proof-of-concept for Mathematica highlighting (which may or may not work correctly now, depending on whether or not anything's changed).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .