Meandering Soul

This day is done, I'm going home.
eFranes Penguin Avatar

Lightweight links for Prism.js

09
Apr 2015

Although Prism.js provides a very good autolinker plugin, it did not quite fit my needs for a recent project. Here’s what I came up with as a stupidly simple replacement that’s not as sophisticated (i.e. does not do links in Markdown and such stuff) but works perfectly well with to-be-highlighted JSON.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Prism.hooks.add('wrap', function(env) {
    if (env.type == 'string', env.content.match(/http/))
    {
        env.content = "<a href=\\"" 
                    + env.content.replace('"', '') 
                    + "\\">" 
                    + env.content 
                    + "</a>";
    }
});

Just add this to your site’s scripts and everything will be better.

  • Published on April 09, 2015
  • 64 words