$(function(){ var urlParams = new URLSearchParams(window.location.search); var t=urlParams.get('t'); var i=urlParams.get('i'); var w=urlParams.get('w'); function locate(top, hash=undefined) { $('html, body, .wrapper').stop().animate({ 'scrollTop': top }, 400, 'swing', function () { if (false && hash) window.location.hash = hash; }); } function highlight(word, $element) { var rgxp = new RegExp(word, 'g'); var repl = '' + word + ''; console.log('regxp '+rgxp); console.log('repl '+repl); // for testing; keep hidden copyh of original version of paragraph // $element.parent().prepend($('').addClass('d-none').html($element.html())); var newcode=''; if ($element.contents().length) { $element.contents().each(function(){ var $child=$(this); console.log('type '+this.nodeType); if (this.nodeType===3) // not an html element { newcode = newcode + this.nodeValue.replace(rgxp, repl); } else newcode = newcode + $child.prop('outerHTML'); }); $element.html(newcode); } else { console.log('html', $element.html()); $element.html($element.html().replace(rgxp, repl)); } return $element.find('.highlighted'); } if (t) { console.log('topic '+t); var $topic=$('[data-topic="'+t+'"]'); if ($topic.length) { var $indexes=$topic.find('[data-index="'+i+'"]'); $indexes.each(function(){ var $index=$(this); if ($index.length) { console.log('index '+i,$index); var $highlight=highlight(w, $index); if ($highlight.length) { console.log($highlight); console.log('index top: '+$index.offset().top); console.log('highlight top: '+$highlight.offset().top); } else console.log('word to highlight '+w+' not found in search scope'); if ($highlight.length) { locate($highlight.offset().top-80, "index-"+i); } else locate($index.offset().top-80, "index-"+i); } else locate($topic.offset().top-80, "topic-"+t); }); } else console.log('requested topic '+t+' not found in topicset'); } /*******************************************************************/ /* ftrtch dino */ /* [type: JS] [file:project-highlight-snippet] [21.412] DESIGN*/ /*******************************************************************/ /* 0.11 D>D */ /* db 151 */ });