Skip to content

Commit d08a471

Browse files
committed
Auto merge of rust-lang#85876 - jeanlucthumm:master, r=GuillaumeGomez
Add `go_to_first` query param to jump to first result Fixes rust-lang#84214 Note that while the issue initially wanted to navigate to an entry on exact match, the discussion settled on using a query parameter (`&go_to_first=true`) instead, regardless of exact or partial match. Demonstration is attached https://user-images.githubusercontent.com/4934853/120258768-7ff28980-c247-11eb-8c8f-1a2ceb242788.mp4
2 parents 3455304 + 6c5c0a5 commit d08a471

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/doc/rustdoc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
- [Lints](lints.md)
1111
- [Advanced features](advanced-features.md)
1212
- [Unstable features](unstable-features.md)
13+
- [Website features](website-features.md)
1314
- [Passes](passes.md)
1415
- [References](references.md)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Website features
2+
3+
These features are about using the website generated by `rustdoc`.
4+
5+
## Custom search engines
6+
7+
If you find yourself often referencing online Rust docs you might enjoy using a custom search
8+
engine. This allows you to use the navigation bar directly to search a `rustdoc` website.
9+
Most browsers support this feature by letting you define a URL template containing `%s`
10+
which will be substituted for the search term. As an example, for the standard library you could use
11+
this template:
12+
13+
```text
14+
https://doc.rust-lang.org/stable/std/?search=%s
15+
```
16+
17+
Note that this will take you to a results page listing all matches. If you want to navigate to the first
18+
result right away (which is often the best match) use the following instead:
19+
20+
```text
21+
https://doc.rust-lang.org/stable/std/?search=%s&go_to_first=true
22+
```
23+
24+
This URL adds the `go_to_first=true` query parameter which can be appended to any `rustdoc` search URL
25+
to automatically go to the first result.

src/librustdoc/html/static/search.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1058,14 +1058,14 @@ window.initSearch = function(rawSearchIndex) {
10581058
return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>";
10591059
}
10601060

1061-
function showResults(results) {
1061+
function showResults(results, go_to_first) {
10621062
var search = searchState.outputElement();
1063-
if (results.others.length === 1
1063+
if (go_to_first || (results.others.length === 1
10641064
&& getSettingValue("go-to-only-result") === "true"
10651065
// By default, the search DOM element is "empty" (meaning it has no children not
10661066
// text content). Once a search has been run, it won't be empty, even if you press
10671067
// ESC or empty the search input (which also "cancels" the search).
1068-
&& (!search.firstChild || search.firstChild.innerText !== searchState.loadingText))
1068+
&& (!search.firstChild || search.firstChild.innerText !== searchState.loadingText)))
10691069
{
10701070
var elem = document.createElement("a");
10711071
elem.href = results.others[0].href;
@@ -1242,7 +1242,7 @@ window.initSearch = function(rawSearchIndex) {
12421242
}
12431243

12441244
var filterCrates = getFilterCrates();
1245-
showResults(execSearch(query, index, filterCrates));
1245+
showResults(execSearch(query, index, filterCrates), params.go_to_first);
12461246
}
12471247

12481248
function buildIndex(rawSearchIndex) {

0 commit comments

Comments
 (0)