From 621affaa063550f086ea827811607bcf864f89e3 Mon Sep 17 00:00:00 2001 From: Blaise Date: Sat, 29 Oct 2022 14:25:04 -0500 Subject: Only highlight text nodes --- proselight.js | 130 +++++++++++++++++++++++++++++++--------------------------- 1 file 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 @@ i, microlighted, + cn, //child nodes el; // current microlighted element to run through @@ -35,70 +36,77 @@ microlighted = _document.querySelectorAll('p'); for (i = 0; el = microlighted[i++];) { - var text = el.textContent, - pos = 0, // current position - next1 = text[0], // next character - chr = 1, // current character - prev1, // previous character - prev2, // the one before the previous - token = // current token content - el.innerHTML = '', // (and cleaning the node) - - // current token type: - tokenType = 0, - - // flag determining if token is multi-character - multichar, - node; - - // running through characters and highlighting - while (prev2 = prev1, prev1 = chr) { - chr = next1; - next1=text[++pos]; - multichar = token.length > 1; - - // checking if current token should be finalized - if (!chr || // end of content - [ // finalize conditions for other token types - // 0: unformatted - /[":;,\\.?!\])\/{}[(|]/[test](chr), - // 1: parentesis or braces - 1, // consist of a single character - // 2: terminators - 1, // consist of a single character - // 3: separators - 1, // consist of a single character - // 4: quotes - prev1 == '"' && multichar, - ][tokenType] - ) { - // appending the token to the result - if (token) { - // map token type into class - el[appendChild]( - node = _document.createElement('span') - ).setAttribute('class', 'ph'+tokenType); - - node[appendChild](_document.createTextNode(token)); + cn = Array.from(el.childNodes); + for (j = 0; j < cn.length; j++) { + if (cn[j].nodeType != Node.TEXT_NODE) { + el.appendChild(cn[j]); + continue; + } + el.removeChild(cn[j]); + var text = cn[j].textContent, + pos = 0, // current position + next1 = text[0], // next character + chr = 1, // current character + prev1, // previous character + prev2, // the one before the previous + token = '', // current token content + + // current token type: + tokenType = 0, + + // flag determining if token is multi-character + multichar, + node; + + // running through characters and highlighting + while (prev2 = prev1, prev1 = chr) { + chr = next1; + next1=text[++pos]; + multichar = token.length > 1; + + // checking if current token should be finalized + if (!chr || // end of content + [ // finalize conditions for other token types + // 0: unformatted + /[":;,\\.?!\])\/{}[(|]/[test](chr), + // 1: parentesis or braces + 1, // consist of a single character + // 2: terminators + 1, // consist of a single character + // 3: separators + 1, // consist of a single character + // 4: quotes + prev1 == '"' && multichar, + ][tokenType] + ) { + // appending the token to the result + if (token) { + // map token type into class + el[appendChild]( + node = _document.createElement('span') + ).setAttribute('class', 'ph'+tokenType); + + node[appendChild](_document.createTextNode(token)); + } + + // initializing a new token + token = ''; + + // determining the new token type (going up the + // list until matching a token type start + // condition) + tokenType = 5; + while (![ + 1, // 0: unformatted + /[\])\/{}[(|]/[test](chr), // 1: parenthesis or braces + /[\\.?!]/[test](chr), // 2: terminators + /[:;,]/[test](chr), // 3: separators + chr == '"', // 4: quotes + ][--tokenType]); } - // initializing a new token - token = ''; - - // determining the new token type (going up the - // list until matching a token type start - // condition) - tokenType = 5; - while (![ - 1, // 0: unformatted - /[\])\/{}[(|]/[test](chr), // 1: parenthesis or braces - /[\\.?!]/[test](chr), // 2: terminators - /[:;,]/[test](chr), // 3: separators - chr == '"', // 4: quotes - ][--tokenType]); + token += chr; } - - token += chr; } } } -- cgit v1.2.3