|
| 1 | +const ALGOLIA_APP_ID = 'KPPUDTIAVX'; |
| 2 | +const ALGOLIA_API_KEY = '1fc841595212a2c3afe8c24dd4cb8790'; |
| 3 | +const ALGOLIA_INDEX_NAME = 'alt-logfire-docs'; |
| 4 | + |
| 5 | +const { liteClient: algoliasearch } = window['algoliasearch/lite']; |
| 6 | +const searchClient = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY); |
| 7 | + |
| 8 | +const search = instantsearch({ |
| 9 | + indexName: ALGOLIA_INDEX_NAME, |
| 10 | + searchClient, |
| 11 | + searchFunction(helper) { |
| 12 | + const query = helper.state.query |
| 13 | + |
| 14 | + if (query && query.length > 1) { |
| 15 | + document.querySelector('#hits').hidden = false |
| 16 | + document.querySelector('#type-to-start-searching').hidden = true |
| 17 | + helper.search(); |
| 18 | + } else { |
| 19 | + document.querySelector('#hits').hidden = true |
| 20 | + document.querySelector('#type-to-start-searching').hidden = false |
| 21 | + } |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +// create custom widget, to integrate with MkDocs built-in markup |
| 26 | +const customSearchBox = instantsearch.connectors.connectSearchBox((renderOptions, isFirstRender) => { |
| 27 | + const { query, refine, clear } = renderOptions; |
| 28 | + |
| 29 | + if (isFirstRender) { |
| 30 | + document.querySelector('#searchbox').addEventListener('input', event => { |
| 31 | + refine(event.target.value); |
| 32 | + }); |
| 33 | + |
| 34 | + document.querySelector('#searchbox').addEventListener('focus', () => { |
| 35 | + document.querySelector('#__search').checked = true; |
| 36 | + }); |
| 37 | + |
| 38 | + document.querySelector('#searchbox-clear').addEventListener('click', () => { |
| 39 | + clear(); |
| 40 | + }); |
| 41 | + |
| 42 | + document.querySelector('#searchbox').addEventListener('keydown', (event) => { |
| 43 | + // on down arrow, find the first search result and focus it |
| 44 | + if (event.key === 'ArrowDown') { |
| 45 | + document.querySelector('.md-search-result__link').focus(); |
| 46 | + event.preventDefault(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // for Hits, add keyboard navigation |
| 51 | + document.querySelector('#hits').addEventListener('keydown', (event) => { |
| 52 | + if (event.key === 'ArrowDown') { |
| 53 | + const next = event.target.parentElement.nextElementSibling; |
| 54 | + if (next) { |
| 55 | + next.querySelector('.md-search-result__link').focus(); |
| 56 | + event.preventDefault(); |
| 57 | + } |
| 58 | + } else if (event.key === 'ArrowUp') { |
| 59 | + const prev = event.target.parentElement.previousElementSibling; |
| 60 | + if (prev) { |
| 61 | + prev.querySelector('.md-search-result__link').focus(); |
| 62 | + } else { |
| 63 | + document.querySelector('#searchbox').focus(); |
| 64 | + } |
| 65 | + event.preventDefault(); |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + document.addEventListener('keydown', (event) => { |
| 70 | + // if forward slash is pressed, focus the search box |
| 71 | + if (event.key === '/' && event.target.tagName !== 'INPUT') { |
| 72 | + document.querySelector('#searchbox').focus(); |
| 73 | + event.preventDefault(); |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + document.querySelector('#type-to-start-searching').hidden = query.length > 1; |
| 80 | + document.querySelector('#searchbox').value = query; |
| 81 | +}); |
| 82 | + |
| 83 | +search.addWidgets([ |
| 84 | + customSearchBox({}), |
| 85 | + |
| 86 | + instantsearch.widgets.hits({ |
| 87 | + container: '#hits', |
| 88 | + cssClasses: { |
| 89 | + 'list': 'md-search-result__list', |
| 90 | + 'item': 'md-search-result__item' |
| 91 | + }, |
| 92 | + templates: { |
| 93 | + item: (hit, { html, components }) => { |
| 94 | + return html` |
| 95 | + <a href="${hit.abs_url}" class="md-search-result__link" tabindex="-1"> |
| 96 | + <div class="md-search-result__article md-typeset"> |
| 97 | + <div class="md-search-result__icon md-icon"></div> |
| 98 | + <h1>${components.Highlight({ attribute: 'title', hit })}</h1> |
| 99 | + <article>${components.Snippet({ attribute: 'content', hit })} </article> |
| 100 | + </div> |
| 101 | + </a>` |
| 102 | + }, |
| 103 | + }, |
| 104 | + }) |
| 105 | +]); |
| 106 | + |
| 107 | +search.start(); |
0 commit comments