summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaise2022-10-29 14:25:04 -0500
committerBlaise2022-10-29 14:25:04 -0500
commit621affaa063550f086ea827811607bcf864f89e3 (patch)
tree74a0d83ffa538f75bd0fbba03e7eb35d8391cdff
parentb5eda297af9b7984cf29c3c89ca0fa8fa433c093 (diff)
Only highlight text nodes
-rw-r--r--proselight.js130
1 files changed, 69 insertions, 61 deletions
diff --git a/proselight.js b/proselight.js
index c79b8ce..a24ff32 100644
--- a/proselight.js
+++ b/proselight.js
@@ -26,6 +26,7 @@
26 26
27 i, 27 i,
28 microlighted, 28 microlighted,
29 cn, //child nodes
29 el; // current microlighted element to run through 30 el; // current microlighted element to run through
30 31
31 32
@@ -35,70 +36,77 @@
35 microlighted = _document.querySelectorAll('p'); 36 microlighted = _document.querySelectorAll('p');
36 37
37 for (i = 0; el = microlighted[i++];) { 38 for (i = 0; el = microlighted[i++];) {
38 var text = el.textContent, 39 cn = Array.from(el.childNodes);
39 pos = 0, // current position 40 for (j = 0; j < cn.length; j++) {
40 next1 = text[0], // next character 41 if (cn[j].nodeType != Node.TEXT_NODE) {
41 chr = 1, // current character 42 el.appendChild(cn[j]);
42 prev1, // previous character 43 continue;
43 prev2, // the one before the previous 44 }
44 token = // current token content 45 el.removeChild(cn[j]);
45 el.innerHTML = '', // (and cleaning the node) 46 var text = cn[j].textContent,
46 47 pos = 0, // current position
47 // current token type: 48 next1 = text[0], // next character
48 tokenType = 0, 49 chr = 1, // current character
49 50 prev1, // previous character
50 // flag determining if token is multi-character 51 prev2, // the one before the previous
51 multichar, 52 token = '', // current token content
52 node; 53
53 54 // current token type:
54 // running through characters and highlighting 55 tokenType = 0,
55 while (prev2 = prev1, prev1 = chr) { 56
56 chr = next1; 57 // flag determining if token is multi-character
57 next1=text[++pos]; 58 multichar,
58 multichar = token.length > 1; 59 node;
59 60
60 // checking if current token should be finalized 61 // running through characters and highlighting
61 if (!chr || // end of content 62 while (prev2 = prev1, prev1 = chr) {
62 [ // finalize conditions for other token types 63 chr = next1;
63 // 0: unformatted 64 next1=text[++pos];
64 /[":;,\\.?!\])\/{}[(|]/[test](chr), 65 multichar = token.length > 1;
65 // 1: parentesis or braces 66
66 1, // consist of a single character 67 // checking if current token should be finalized
67 // 2: terminators 68 if (!chr || // end of content
68 1, // consist of a single character 69 [ // finalize conditions for other token types
69 // 3: separators 70 // 0: unformatted
70 1, // consist of a single character 71 /[":;,\\.?!\])\/{}[(|]/[test](chr),
71 // 4: quotes 72 // 1: parentesis or braces
72 prev1 == '"' && multichar, 73 1, // consist of a single character
73 ][tokenType] 74 // 2: terminators
74 ) { 75 1, // consist of a single character
75 // appending the token to the result 76 // 3: separators
76 if (token) { 77 1, // consist of a single character
77 // map token type into class 78 // 4: quotes
78 el[appendChild]( 79 prev1 == '"' && multichar,
79 node = _document.createElement('span') 80 ][tokenType]
80 ).setAttribute('class', 'ph'+tokenType); 81 ) {
81 82 // appending the token to the result
82 node[appendChild](_document.createTextNode(token)); 83 if (token) {
84 // map token type into class
85 el[appendChild](
86 node = _document.createElement('span')
87 ).setAttribute('class', 'ph'+tokenType);
88
89 node[appendChild](_document.createTextNode(token));
90 }
91
92 // initializing a new token
93 token = '';
94
95 // determining the new token type (going up the
96 // list until matching a token type start
97 // condition)
98 tokenType = 5;
99 while (![
100 1, // 0: unformatted
101 /[\])\/{}[(|]/[test](chr), // 1: parenthesis or braces
102 /[\\.?!]/[test](chr), // 2: terminators
103 /[:;,]/[test](chr), // 3: separators
104 chr == '"', // 4: quotes
105 ][--tokenType]);
83 } 106 }
84 107
85 // initializing a new token 108 token += chr;
86 token = '';
87
88 // determining the new token type (going up the
89 // list until matching a token type start
90 // condition)
91 tokenType = 5;
92 while (![
93 1, // 0: unformatted
94 /[\])\/{}[(|]/[test](chr), // 1: parenthesis or braces
95 /[\\.?!]/[test](chr), // 2: terminators
96 /[:;,]/[test](chr), // 3: separators
97 chr == '"', // 4: quotes
98 ][--tokenType]);
99 } 109 }
100
101 token += chr;
102 } 110 }
103 } 111 }
104 } 112 }