Lightweight links for Prism.js

  • Reading time: 1 min
  • Published 4 years ago

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.

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.