Blame view

bower_components/prism/plugins/autolinker/prism-autolinker.js 1.52 KB
73bcce88   luigser   COMPONENTS
1
2
  (function(){
  
eb240478   Luigi Serra   public room cards...
3
4
5
6
  if (
  	typeof self !== 'undefined' && !self.Prism ||
  	typeof global !== 'undefined' && !global.Prism
  ) {
73bcce88   luigser   COMPONENTS
7
8
9
10
11
12
13
14
15
16
  	return;
  }
  
  var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:#=?&]+/,
      email = /\b\S+@[\w.]+[a-z]{2}/,
      linkMd = /\[([^\]]+)]\(([^)]+)\)/,
      
  	// Tokens that may contain URLs and emails
      candidates = ['comment', 'url', 'attr-value', 'string'];
  
eb240478   Luigi Serra   public room cards...
17
18
19
20
21
22
  Prism.hooks.add('before-highlight', function(env) {
  	// Abort if grammar has already been processed
  	if (!env.grammar || env.grammar['url-link']) {
  		return;
  	}
  	Prism.languages.DFS(env.grammar, function (key, def, type) {
73bcce88   luigser   COMPONENTS
23
24
25
26
27
28
  		if (candidates.indexOf(type) > -1 && Prism.util.type(def) !== 'Array') {
  			if (!def.pattern) {
  				def = this[key] = {
  					pattern: def
  				};
  			}
eb240478   Luigi Serra   public room cards...
29
  
73bcce88   luigser   COMPONENTS
30
  			def.inside = def.inside || {};
eb240478   Luigi Serra   public room cards...
31
  
73bcce88   luigser   COMPONENTS
32
33
34
35
36
37
38
39
40
  			if (type == 'comment') {
  				def.inside['md-link'] = linkMd;
  			}
  			if (type == 'attr-value') {
  				Prism.languages.insertBefore('inside', 'punctuation', { 'url-link': url }, def);
  			}
  			else {
  				def.inside['url-link'] = url;
  			}
eb240478   Luigi Serra   public room cards...
41
  
73bcce88   luigser   COMPONENTS
42
43
44
  			def.inside['email-link'] = email;
  		}
  	});
eb240478   Luigi Serra   public room cards...
45
46
47
  	env.grammar['url-link'] = url;
  	env.grammar['email-link'] = email;
  });
73bcce88   luigser   COMPONENTS
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  
  Prism.hooks.add('wrap', function(env) {
  	if (/-link$/.test(env.type)) {
  		env.tag = 'a';
  		
  		var href = env.content;
  		
  		if (env.type == 'email-link' && href.indexOf('mailto:') != 0) {
  			href = 'mailto:' + href;
  		}
  		else if (env.type == 'md-link') {
  			// Markdown
  			var match = env.content.match(linkMd);
  			
  			href = match[2];
  			env.content = match[1];
  		}
  		
  		env.attributes.href = href;
  	}
  });
  
eb240478   Luigi Serra   public room cards...
70
  })();