1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00
Files
stedolan-jq/docs/public/js/manual-search.js
itchyny c8e28da129 Redesign website (#2628)
* Bump up Bootstrap to v5.3.1, Bootstrap Icon to v1.10.5.
* Use autoComplete.js to drop dependency on jQuery and typeahead.js.
* Support dark mode.
* New svg logo and icon with responsive color mode support.
* Normalize section ids to lower kebab-case for easiness of linking.
* Use relative paths for links for local development (--root /output).
* Various markup cleanups and accessibility improvements.
2023-07-31 09:52:52 +09:00

35 lines
1.3 KiB
JavaScript

(() => {
const searchInput = document.querySelector('input#searchbox');
const sectionIDs = JSON.parse(document.querySelector('#section-ids').innerText);
const sanitize = (string) => string.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
new autoComplete({
selector: `#${searchInput.id}`,
wrapper: false,
data: {
src: Object.keys(sectionIDs),
filter: (list) => list.sort((x, y) =>
x.match.indexOf('<') - y.match.indexOf('<') || x.value.length - y.value.length),
},
searchEngine: (query, value) => {
const index = value.toLowerCase().indexOf(query.toLowerCase());
if (index >= 0) {
return sanitize(value.substring(0, index)) +
`<mark>${sanitize(value.substring(index, index + query.length))}</mark>` +
sanitize(value.substring(index + query.length));
}
},
});
searchInput.addEventListener('selection', (event) => {
event.target.value = event.detail.selection.value;
location.hash = `#${sectionIDs[event.detail.selection.value]}`;
});
document.addEventListener('keydown', (event) => {
if (event.code === 'Slash' && !event.altKey && !event.ctrlKey && !event.metaKey
&& !event.shiftKey && !/^(INPUT|TEXTAREA)$/.test(event.target.nodeName)) {
searchInput.focus();
searchInput.select();
event.preventDefault();
}
});
})();