From bd3ba77eb8dead3c79c8356c7bb77b9e1418a2f7 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 29 Jul 2025 16:02:09 -0700 Subject: [PATCH 1/2] Search automatically on URL query parameter Automatically start searching when ?q= parameter is given in the URL, selecting and visiting the first match if available. If not available, the query will be filled into the search box but we won't navigate away. --- lib/rdoc/generator/template/darkfish/js/darkfish.js | 10 ++++++++++ lib/rdoc/generator/template/darkfish/js/search.js | 12 +++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/rdoc/generator/template/darkfish/js/darkfish.js b/lib/rdoc/generator/template/darkfish/js/darkfish.js index 4c15efde66..15b5d706be 100644 --- a/lib/rdoc/generator/template/darkfish/js/darkfish.js +++ b/lib/rdoc/generator/template/darkfish/js/darkfish.js @@ -76,6 +76,16 @@ function hookSearch() { } search.scrollIntoView = search.scrollInWindow; + + // Check for ?q= URL parameter and trigger search automatically + if (typeof URLSearchParams !== 'undefined') { + var urlParams = new URLSearchParams(window.location.search); + var queryParam = urlParams.get('q'); + if (queryParam) { + input.value = queryParam; + search.search(queryParam, true); + } + } }; function hookFocus() { diff --git a/lib/rdoc/generator/template/darkfish/js/search.js b/lib/rdoc/generator/template/darkfish/js/search.js index d3cded1d57..68e1f77ff8 100644 --- a/lib/rdoc/generator/template/darkfish/js/search.js +++ b/lib/rdoc/generator/template/darkfish/js/search.js @@ -34,6 +34,8 @@ Search.prototype = Object.assign({}, Navigation, new function() { } this.search = function(value, selectFirstMatch) { + this.selectFirstMatch = selectFirstMatch; + value = value.trim().toLowerCase(); if (value) { this.setNavigationActive(true); @@ -76,7 +78,15 @@ Search.prototype = Object.assign({}, Navigation, new function() { //TODO: ECMAScript //if (jQuery.browser.msie) this.$element[0].className += ''; - if (isLast) this.result.setAttribute('aria-busy', 'false'); + if (this.selectFirstMatch && this.current) { + this.selectFirstMatch = false; + this.select(this.current); + } + + if (isLast) { + this.selectFirstMatch = false; + this.result.setAttribute('aria-busy', 'false'); + } } this.move = function(isDown) { From df0cf11ad8f0d49560af8a2d8c9157b031b7d724 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 13 Aug 2025 17:26:32 -0700 Subject: [PATCH 2/2] Keep search query filled after navigating --- .../generator/template/darkfish/js/darkfish.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/rdoc/generator/template/darkfish/js/darkfish.js b/lib/rdoc/generator/template/darkfish/js/darkfish.js index 15b5d706be..6b6e688afb 100644 --- a/lib/rdoc/generator/template/darkfish/js/darkfish.js +++ b/lib/rdoc/generator/template/darkfish/js/darkfish.js @@ -72,7 +72,15 @@ function hookSearch() { } search.select = function(result) { - window.location.href = result.firstChild.firstChild.href; + var href = result.firstChild.firstChild.href; + var query = this.input.value; + if (query) { + var url = new URL(href, window.location.origin); + url.searchParams.set('q', query); + url.searchParams.set('nav', '0'); + href = url.toString(); + } + window.location.href = href; } search.scrollIntoView = search.scrollInWindow; @@ -82,8 +90,10 @@ function hookSearch() { var urlParams = new URLSearchParams(window.location.search); var queryParam = urlParams.get('q'); if (queryParam) { + var navParam = urlParams.get('nav'); + var autoSelect = navParam !== '0'; input.value = queryParam; - search.search(queryParam, true); + search.search(queryParam, autoSelect); } } };