Created
April 6, 2016 13:54
-
-
Save peaeater/c3f3665716555b4e8602befe2339d537 to your computer and use it in GitHub Desktop.
Extracts keywords and quoted phrases from a Solr query string, ignoring field phrases, ranges and query syntax characters. Surrounds terms found with a <b> tag. Native javascript, no dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| andi.highlightTerms = []; | |
| andi.highlight = function (el, terms) { | |
| if (terms === null || terms.length === 0) return; | |
| var term = terms.join('|'); | |
| var regex = new RegExp('\\b('+term+')\\b', 'gi'); | |
| if (el.innerHTML.match(regex) !== null) { | |
| el.innerHTML = el.innerHTML.replace(regex, '<b>$1</b>'); | |
| el.classList === true ? el.classList.add('highlighted') : el.className += ' ' + 'highlighted'; | |
| } | |
| } | |
| andi.highlightAll = function (selector, terms) { | |
| var els = document.querySelectorAll(selector); | |
| // note: els is a NodeList, not an array | |
| Array.prototype.forEach.call(els, function (el) { | |
| andi.highlight(el, terms); | |
| }) | |
| }; | |
| andi.parseHighlightTerms = function (solrquery) { | |
| // reject patterns | |
| var s = solrquery | |
| .replace(/[\w-]+:\((?=[^\)]*\))+[^\)]+\)+/g, '') // field:(value) | |
| .replace(/[\w-]+:\[(?=[^\]]*\])+[^\]]+\]+/g, '') // field:[value] | |
| .replace(/[\w-]+:\u0022(?=[^\u0022]+\u0022)+[^\u0022]+\u0022+/g, '') // field:"value" | |
| .replace(/[\w-]+:[^\s]*/g, '') // field:value | |
| .replace(/\[(?=[^\]]*\])+[^\]]+\]+/g, '') // [value TO value] | |
| .replace(/(^|\s)-\w+\b/g, '') // -value | |
| .replace(/NOT\s\w+\b/g, '') // NOT value | |
| .replace(/(AND|OR|NOT)/g, ''); // AND OR NOT | |
| // split string on whitespace and respect double quoted phrases | |
| var rawTerms = s.match(/(\u0022[^\u0022]*\u0022)|([^\u0022\s]+(\s|$))/g); | |
| var terms = []; | |
| rawTerms.forEach(function (rawTerm) { | |
| // trim whitespace, quotes, apostrophes and query syntax special chars | |
| var term = rawTerm | |
| .replace(/^[\s\u0022\u0027+-][\s\u0022\u0027+-]*/, '') | |
| .replace(/[\s*~\u0022\u0027][\s*~\u0022\u0027]*$/, '') | |
| .replace(/(\^|~)\d+$/, ''); // value^5, value~5 | |
| if (term !== '') { | |
| terms.push(term); | |
| } | |
| }); | |
| return terms; | |
| }; | |
| $(document).ready(function () { | |
| // todo: should probably get query terms from some dedicated source instead of the q input. | |
| andi.highlightTerms = andi.parseHighlightTerms(document.getElementById('q').getAttribute('value')); | |
| if (andi.highlightTerms.length > 0) { | |
| andi.highlightAll('.and-citation-brief dd', andi.highlightTerms); | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment