|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1"> |
| 6 | + <title>Search</title> |
| 7 | + <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin> |
| 8 | + <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin> |
| 9 | + <style> |
| 10 | + body {margin: 0 1em;} |
| 11 | + footer, |
| 12 | + #search-status { |
| 13 | + font: 14px normal; |
| 14 | + color: grey; |
| 15 | + } |
| 16 | + |
| 17 | + footer {text-align: right;} |
| 18 | + |
| 19 | + a { |
| 20 | + color: #058; |
| 21 | + text-decoration: none; |
| 22 | + transition: color .3s ease-in-out; |
| 23 | + } |
| 24 | + a:hover {color: #e82;} |
| 25 | + |
| 26 | + li {padding-top: 10px;} |
| 27 | + </style> |
| 28 | + <base target="_parent"> |
| 29 | +</head> |
| 30 | +<body> |
| 31 | +<noscript> |
| 32 | + JavaScript is not supported/enabled in your browser. The search feature won't work. |
| 33 | +</noscript> |
| 34 | +<main> |
| 35 | + <h3 id="search-status"></h3> |
| 36 | + <ul id="search-results"></ul> |
| 37 | +</main> |
| 38 | +<footer> |
| 39 | + <p>Search results provided by <a href="https://lunrjs.com">Lunr.js</a></p> |
| 40 | +</footer> |
| 41 | + |
| 42 | +<script src="index.js"></script> |
| 43 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js" integrity="sha512-HiJdkRySzXhiUcX2VweXaiy8yeY212ep/j51zR/z5IPCX4ZUOxaf6naJ/0dQL/2l+ZL+B9in/u4nT8QJZ/3mig==" crossorigin></script> |
| 44 | +<script> |
| 45 | + 'use strict'; |
| 46 | + |
| 47 | + const lunr_index = build_index(); |
| 48 | + search(decodeURIComponent(new URL(window.location).hash.substring(1))); |
| 49 | + |
| 50 | + function set_status(message) { |
| 51 | + document.getElementById('search-status').textContent = message; |
| 52 | + } |
| 53 | + |
| 54 | + async function build_index() { |
| 55 | + return lunr(function () { |
| 56 | + this.ref('i'); |
| 57 | + this.field('name', {boost: 10}); |
| 58 | + this.field('ref', {boost: 5}); |
| 59 | + this.field('doc'); |
| 60 | + this.metadataWhitelist = ['position']; |
| 61 | + |
| 62 | + INDEX.forEach((doc, i) => { |
| 63 | + const parts = doc.ref.split('.'); |
| 64 | + doc['name'] = parts[parts.length - 1]; |
| 65 | + doc['i'] = i; |
| 66 | + |
| 67 | + this.add(doc); |
| 68 | + }, this); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + function search(query) { |
| 73 | + _search(query).catch(err => { |
| 74 | + set_status("Something went wrong. See development console for details."); |
| 75 | + throw err; |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + async function _search(query) { |
| 80 | + if (!query) { |
| 81 | + set_status('No query provided, so there is nothing to search.'); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const fuzziness = 1; |
| 86 | + if (fuzziness) { |
| 87 | + query = query.split(/\s+/) |
| 88 | + .map(str => str.includes('~') ? str : str + '~' + fuzziness).join(' '); |
| 89 | + } |
| 90 | + |
| 91 | + const results = (await lunr_index).search(query); |
| 92 | + if (!results.length) { |
| 93 | + set_status('No results match your query.'); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + set_status( |
| 98 | + 'Search for "' + encodeURIComponent(query) + '" yielded ' + results.length + ' ' + |
| 99 | + (results.length === 1 ? 'result' : 'results') + ':'); |
| 100 | + |
| 101 | + results.forEach(function (result) { |
| 102 | + const dobj = INDEX[parseInt(result.ref)]; |
| 103 | + const docstring = dobj.doc; |
| 104 | + const url = URLS[dobj.url] + '#' + dobj.ref; |
| 105 | + const pretty_name = dobj.ref + (dobj.func ? '()' : ''); |
| 106 | + let text = ''; |
| 107 | + if (docstring) { |
| 108 | + text = Object.values(result.matchData.metadata) |
| 109 | + .filter(({doc}) => doc !== undefined) |
| 110 | + .map(({doc: {position}}) => { |
| 111 | + return position.map(([start, length]) => { |
| 112 | + const PAD_CHARS = 30; |
| 113 | + const end = start + length; |
| 114 | + return [ |
| 115 | + start, |
| 116 | + (start - PAD_CHARS > 0 ? '…' : '') + |
| 117 | + docstring.substring(start - PAD_CHARS, start) + |
| 118 | + '<mark>' + docstring.slice(start, end) + '</mark>' + |
| 119 | + docstring.substring(end, end + PAD_CHARS) + |
| 120 | + (end + PAD_CHARS < docstring.length ? '…' : '') |
| 121 | + ]; |
| 122 | + }); |
| 123 | + }) |
| 124 | + .flat() |
| 125 | + .sort(([pos1,], [pos2,]) => pos1 - pos2) |
| 126 | + .map(([, text]) => text) |
| 127 | + .join('') |
| 128 | + .replace(/……/g, '…'); |
| 129 | + } |
| 130 | + |
| 131 | + if (text) |
| 132 | + text = '<div>' + text + '</div>'; |
| 133 | + text = '<a href="' + url + '"><code>' + pretty_name + '</code></a>' + text; |
| 134 | + |
| 135 | + const li = document.createElement('li'); |
| 136 | + li.innerHTML = text; |
| 137 | + document.getElementById('search-results').appendChild(li); |
| 138 | + }); |
| 139 | + } |
| 140 | +</script> |
| 141 | +</body> |
0 commit comments