Skip to content

Commit 1007a87

Browse files
authored
vdoc: enable browser custom search with ?q=<search> (#20949)
1 parent ddf1ae9 commit 1007a87

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

cmd/tools/vdoc/theme/doc.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ function setupDarkMode() {
9090
}
9191

9292
function setupSearch() {
93-
const searchInput = document.getElementById('search');
9493
const onInputChange = debounce((e) => {
9594
const searchValue = e.target.value.toLowerCase();
9695
const docNav = document.querySelector('.doc-nav');
@@ -160,7 +159,23 @@ function setupSearch() {
160159
});
161160
}
162161
});
163-
searchInput.addEventListener('input', onInputChange);
162+
const searchInput = document.querySelector('#search input');
163+
const url = document.location.toString();
164+
if (url.includes('?')) {
165+
const query =
166+
url
167+
.split('?')
168+
.slice(1)
169+
.filter((p) => p.startsWith('q='))
170+
.map((p) => p.replace(/^q=/, ''))[0] || '';
171+
if (query) {
172+
searchInput.value = query;
173+
searchInput.focus();
174+
onInputChange({ target: { value: query } });
175+
}
176+
}
177+
const searchInputDiv = document.getElementById('search');
178+
searchInputDiv.addEventListener('input', onInputChange);
164179
setupSearchKeymaps();
165180
}
166181

0 commit comments

Comments
 (0)