Improve search performance

This commit is contained in:
squidfunk 2017-03-21 15:36:46 +01:00 committed by Martin Donath
parent 50efda702a
commit 89fff8a6ab
3 changed files with 14 additions and 4 deletions

View File

@ -151,7 +151,7 @@
{% endblock %}
</div>
{% block scripts %}
<script src="{{ base_url }}/assets/javascripts/application-01fc542e64.js"></script>
<script src="{{ base_url }}/assets/javascripts/application-134345668e.js"></script>
<script>app.initialize({url:{base:"{{ base_url }}"}})</script>
{% for path in extra_javascript %}
<script src="{{ path }}"></script>

View File

@ -40,6 +40,7 @@ export default class Result {
* @property {HTMLElement} list_ - Search result list
* @property {Object} message_ - Search result messages
* @property {Object} index_ - Search index
* @property {string} value_ - Last input value
*
* @param {(string|HTMLElement)} el - Selector or HTML element
* @param {(Array<Object>|Function)} data - Function providing data or array
@ -153,13 +154,22 @@ export default class Result {
if (!(target instanceof HTMLInputElement))
throw new ReferenceError
/* Abort early, if input hasn't changed */
if (target.value === this.value_)
return
/* Clear current list */
while (this.list_.firstChild)
this.list_.removeChild(this.list_.firstChild)
/* Abort early, if search input is empty */
this.value_ = target.value
if (this.value_.length === 0)
return
/* Perform search on index and group sections by document */
const result = this.index_
.search(target.value)
.search(this.value_)
.reduce((items, item) => {
const doc = this.docs_.get(item.ref)
if (doc.parent) {
@ -171,7 +181,7 @@ export default class Result {
/* Assemble highlight regex from query string */
const match = new RegExp(
`\\b(${target.value.trim().replace(" ", "|")})`, "img")
`\\b(${this.value_.trim().replace(" ", "|")})`, "img")
const highlight = string => `<em>${string}</em>`
/* Render results */