Skip to content

Commit

Permalink
add abilty to highlight name and path
Browse files Browse the repository at this point in the history
  • Loading branch information
REJack committed Jun 27, 2020
1 parent 798cf73 commit 45d4759
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion build/js/SidebarSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const Default = {
arrowSign: '->',
minLength: 3,
maxResults: 7,
highlightName: true,
highlightPath: false,
highlightClass: 'text-light',
notFoundText: 'No element found!'
}

Expand Down Expand Up @@ -152,12 +155,37 @@ class SidebarSearch {
}

_renderItem(name, link, path) {
path = path.join(` ${this.options.arrowSign} `)

if (this.options.highlightName || this.options.highlightPath) {
const searchValue = $(SELECTOR_SEARCH_INPUT).val().toLowerCase()
const regExp = new RegExp(searchValue, 'gi')

if (this.options.highlightName) {
name = name.replace(
regExp,
str => {
return `<b class="${this.options.highlightClass}">${str}</b>`
}
)
}

if (this.options.highlightPath) {
path = path.replace(
regExp,
str => {
return `<b class="${this.options.highlightClass}">${str}</b>`
}
)
}
}

return `<a href="${link}" class="list-group-item">
<div class="search-title">
${name}
</div>
<div class="search-path">
${path.join(` ${this.options.arrowSign} `)}
${path}
</div>
</a>`
}
Expand Down

0 comments on commit 45d4759

Please sign in to comment.