diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-08-19 16:05:57 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-08-19 16:05:57 -0700 |
commit | 14dda3148d34f6168e25a0515b670193041e8d3b (patch) | |
tree | 519401e59772044d7158c3e2c9ac6881fda58689 /theme/javascript/core/glossary.js | |
parent | 511dc9f805f209e0916ddfba144530fe960fbb28 (diff) | |
download | gitbook-14dda3148d34f6168e25a0515b670193041e8d3b.zip gitbook-14dda3148d34f6168e25a0515b670193041e8d3b.tar.gz gitbook-14dda3148d34f6168e25a0515b670193041e8d3b.tar.bz2 |
Improve replace of words with glossary links
Diffstat (limited to 'theme/javascript/core/glossary.js')
-rw-r--r-- | theme/javascript/core/glossary.js | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/theme/javascript/core/glossary.js b/theme/javascript/core/glossary.js index 3e80f64..1d4300f 100644 --- a/theme/javascript/core/glossary.js +++ b/theme/javascript/core/glossary.js @@ -31,6 +31,22 @@ define([ return d.promise(); } + var pregQuote = function( str ) { + // http://kevin.vanzonneveld.net + // + original by: booeyOH + // + improved by: Ates Goral (http://magnetiq.com) + // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) + // + bugfixed by: Onno Marsman + // * example 1: preg_quote("$40"); + // * returns 1: '\$40' + // * example 2: preg_quote("*RRRING* Hello?"); + // * returns 2: '\*RRRING\* Hello\?' + // * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:"); + // * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:' + + return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1"); + }; + var init = function() { // Bind click on glossary item $(document).on("click", ".book-body .page-wrapper .page-inner .glossary-term", function(e) { @@ -41,11 +57,13 @@ define([ }; var replaceTerm = function($el, term) { + var r = new RegExp( "(" + pregQuote(term.name.toLowerCase()) + ")" , 'gi' ); + $el.find("p:contains('"+term.name+"')").each( function( i, element ) { element = $(element); - var content = $(element).html(); - content = content.replace(term.name, '<span class="glossary-term" data-glossary-term="' + term.id + '" title="' + term.description + '">' + term.name + '</span>'); + + content = content.replace(r, '<span class="glossary-term" data-glossary-term="' + term.id + '" title="' + term.description + '">$1</span>'); element.html(content); }); }; |