|
| 1 | +{{ $.Scratch.Add "index" slice }} |
| 2 | +{{ range site.Pages }} |
| 3 | +{{ $.Scratch.Add "index" (dict "title" .Title "url" .Permalink "body" .Plain "summary" .Summary) }} |
| 4 | +{{ end }} |
| 5 | +{{ $idx := $.Scratch.Get "index" | jsonify }} |
| 6 | +<script src="https://unpkg.com/lunr/lunr.js"></script> |
| 7 | +<script src="https://code.jquery.com/jquery-3.4.0.min.js" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script> |
| 8 | +{{ $app := resources.Get "js/app.js" | fingerprint }} |
| 9 | +<script src="{{ $app.RelPermalink }}" integrity="{{ $app.Data.Integrity }}"></script> |
| 10 | + |
| 11 | +<script> |
| 12 | + var searchBar = $('#search-bar'), |
| 13 | + searchDropdown = $('#search-dropdown'), |
| 14 | + searchMenu = $('#search-menu'), |
| 15 | + searchContent = $('#search-content'); |
| 16 | + |
| 17 | + var docs = JSON.parse('{{ $idx }}'), |
| 18 | + store = [], |
| 19 | + indexer = function() { |
| 20 | + this.ref('url'); |
| 21 | + this.field('title', {boost:100}); |
| 22 | + this.field('body'); |
| 23 | + this.field('summary'); |
| 24 | + |
| 25 | + // Importance of document length |
| 26 | + this.b(0); |
| 27 | + |
| 28 | + // How quickly a boost given by a common word reaches saturation |
| 29 | + this.k1(1.3); |
| 30 | + }; |
| 31 | + |
| 32 | + var idx = lunr(function() { |
| 33 | + this.use(indexer); |
| 34 | + docs.forEach(function(doc) { |
| 35 | + this.add(doc); |
| 36 | + |
| 37 | + store[doc.url] = { |
| 38 | + 'title': doc.title, |
| 39 | + 'summary': doc.summary |
| 40 | + } |
| 41 | + }, this); |
| 42 | + }); |
| 43 | + |
| 44 | + searchBar.on('focusin', function() { |
| 45 | + searchBar.on('keyup', function() { |
| 46 | + const searchTerm = searchBar.val().toLowerCase(); |
| 47 | + |
| 48 | + if (searchTerm) { |
| 49 | + const finalSearchTerm = `${searchTerm}*`; |
| 50 | + const results = idx.search(finalSearchTerm); |
| 51 | + |
| 52 | + if (results.length > 0) { |
| 53 | + searchMenu.show(); |
| 54 | + |
| 55 | + searchContent.empty(); |
| 56 | + |
| 57 | + var numAdded = 0; |
| 58 | + |
| 59 | + results.forEach(function(res) { |
| 60 | + const url = res.ref, |
| 61 | + title = store[url].title, |
| 62 | + summary = store[url].summary; |
| 63 | + |
| 64 | + const item = ` |
| 65 | + <a class="dropdown-item" href="${url}"> |
| 66 | + <p class="is-size-5 has-text-weight-medium"> |
| 67 | + ${title} |
| 68 | + </p> |
| 69 | +
|
| 70 | + <p> |
| 71 | + ${summary.substring(0, 50)}... |
| 72 | + </p> |
| 73 | + </a> |
| 74 | + ` |
| 75 | + |
| 76 | + if (numAdded < 8) { |
| 77 | + searchContent.append(item); |
| 78 | + numAdded++; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + searchContent.show(); |
| 83 | + } |
| 84 | + } else { |
| 85 | + searchContent.hide(); |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + searchBar.on('focusout', function() { |
| 91 | + |
| 92 | + }); |
| 93 | +</script> |
0 commit comments