Blame view

bower_components/prism/plugins/line-numbers/prism-line-numbers.js 1.41 KB
eb240478   Luigi Serra   public room cards...
1
2
3
4
5
6
7
8
9
10
11
  (function() {
  
  if (typeof self === 'undefined' || !self.Prism || !self.document) {
  	return;
  }
  
  Prism.hooks.add('complete', function (env) {
  	if (!env.code) {
  		return;
  	}
  
73bcce88   luigser   COMPONENTS
12
13
14
15
16
  	// works only for <code> wrapped inside <pre> (not inline)
  	var pre = env.element.parentNode;
  	var clsReg = /\s*\bline-numbers\b\s*/;
  	if (
  		!pre || !/pre/i.test(pre.nodeName) ||
eb240478   Luigi Serra   public room cards...
17
  			// Abort only if nor the <pre> nor the <code> have the class
73bcce88   luigser   COMPONENTS
18
19
20
21
22
  		(!clsReg.test(pre.className) && !clsReg.test(env.element.className))
  	) {
  		return;
  	}
  
eb240478   Luigi Serra   public room cards...
23
24
25
26
27
  	if (env.element.querySelector(".line-numbers-rows")) {
  		// Abort if line numbers already exists
  		return;
  	}
  
73bcce88   luigser   COMPONENTS
28
29
30
31
32
33
34
35
36
  	if (clsReg.test(env.element.className)) {
  		// Remove the class "line-numbers" from the <code>
  		env.element.className = env.element.className.replace(clsReg, '');
  	}
  	if (!clsReg.test(pre.className)) {
  		// Add the class "line-numbers" to the <pre>
  		pre.className += ' line-numbers';
  	}
  
eb240478   Luigi Serra   public room cards...
37
38
  	var match = env.code.match(/\n(?!$)/g);
  	var linesNum = match ? match.length + 1 : 1;
73bcce88   luigser   COMPONENTS
39
40
  	var lineNumbersWrapper;
  
eb240478   Luigi Serra   public room cards...
41
  	var lines = new Array(linesNum + 1);
73bcce88   luigser   COMPONENTS
42
43
44
45
46
47
48
49
50
51
52
53
  	lines = lines.join('<span></span>');
  
  	lineNumbersWrapper = document.createElement('span');
  	lineNumbersWrapper.className = 'line-numbers-rows';
  	lineNumbersWrapper.innerHTML = lines;
  
  	if (pre.hasAttribute('data-start')) {
  		pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1);
  	}
  
  	env.element.appendChild(lineNumbersWrapper);
  
eb240478   Luigi Serra   public room cards...
54
55
56
  });
  
  }());