-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3324 from tvdeyen/guides/algolia-docsearch
[Guides] Add Algolia Docsearch
- Loading branch information
Showing
13 changed files
with
222 additions
and
170 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.tmp | ||
node_modules | ||
build |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,36 @@ | ||
import lunr from 'lunr'; | ||
import DOMPurify from 'dompurify'; | ||
|
||
var query = DOMPurify.sanitize(decodeURIComponent(window.location.search.match(/inputSearch=(.*?)(&|$)/)[1]).replace('+', ' ')); | ||
|
||
function populateIndex(data) { | ||
var index = lunr(function(){ | ||
this.field('title', { boost: 10 }); | ||
this.field('content'); | ||
this.field('url'); | ||
this.ref('id'); | ||
var lunr_index = this | ||
|
||
data.forEach(function(item) { | ||
lunr_index.add(item); | ||
}); | ||
}); | ||
|
||
return index; | ||
} | ||
|
||
function contentList(data) { | ||
var contents = []; | ||
data.forEach(function(item) { | ||
contents.push(item); | ||
}); | ||
return contents; | ||
} | ||
|
||
function search(index, contents){ | ||
let results = index.search(query) | ||
document.querySelector('.search-results-title strong').innerHTML = query; | ||
addSearchResults(results, contents, query) | ||
} | ||
|
||
function addSearchResults(results, contents) { | ||
let template = document.querySelector('.result-template').content; | ||
let resultList = document.querySelector('.results-list'); | ||
|
||
results.forEach(function(result) { | ||
let list_item = template.cloneNode(true); | ||
|
||
let title = contents[result.ref].title; | ||
let body = contents[result.ref].content; | ||
let href = contents[result.ref].url; | ||
|
||
list_item.querySelector('.title a').innerHTML = highlightQuery(title, query); | ||
list_item.querySelector('.title a').href = href; | ||
list_item.querySelector('.category').innerHTML = formatCategory(href); | ||
list_item.querySelector('p').innerHTML = formatBody(body, query) | ||
|
||
resultList.appendChild(list_item); | ||
}); | ||
} | ||
|
||
function formatCategory(href, title) { | ||
let category = href.split('/')[2].split('-').join(' ').replace(/^./g, l => l.toUpperCase()); | ||
return 'Developer Documentation > ' + category; | ||
} | ||
|
||
function highlightQuery(text) { | ||
let re = new RegExp('((?:^|>)[^<>]*?)('+query+')([^<>]*?(?:$|<))', "gim"); | ||
return text.replace(re, '$1<span class="search-highlight">$2</span>$3'); | ||
} | ||
|
||
function formatBody(body) { | ||
return highlightQuery(body.match(/<p>([\s\S]*?)<\/p>/)[0]); | ||
} | ||
|
||
let data = JSON.parse(sessionStorage.getItem('search_index')); | ||
let index = populateIndex(data); | ||
let contents = contentList(data); | ||
search(index, contents); | ||
import docsearch from "docsearch.js" | ||
|
||
const input = document.querySelector('#inputSearch') | ||
const label = document.querySelector('label[for="inputSearch"]') | ||
|
||
docsearch({ | ||
apiKey: 'a021104be0e00dbc782914786fc9fdec', | ||
indexName: 'solidus', | ||
inputSelector: '#inputSearch', | ||
autocompleteOptions: { | ||
autoWidth: false, | ||
openOnFocus: true, | ||
autoselect: true | ||
} | ||
}) | ||
|
||
input.addEventListener('focus', () => { | ||
input.classList.remove('placeholder-shown') | ||
label.classList.add('input-focused') | ||
}) | ||
|
||
input.form.addEventListener('submit', (event) => { | ||
event.preventDefault() | ||
return false | ||
}) | ||
|
||
input.addEventListener('blur', () => { | ||
if (input.value === '') { | ||
input.classList.add('placeholder-shown') | ||
label.classList.remove('input-focused') | ||
} | ||
}) | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
input.value = '' | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.search-box .algolia-autocomplete { | ||
width: 100%; | ||
|
||
.ds-dropdown-menu { | ||
min-width: 100%; | ||
max-width: 100%; | ||
} | ||
|
||
&.algolia-autocomplete-right .ds-dropdown-menu::before { | ||
left: 48px; | ||
} | ||
} | ||
|
||
label[for="inputSearch"].input-focused { | ||
padding-bottom: 0; | ||
background: #ffffff; | ||
font-size: 12px; | ||
padding-top: 8px; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ | |
|
||
|
||
|
||
@import "~docsearch.js/dist/npm/styles/main.scss"; |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.