Skip to content

Commit

Permalink
Auto merge of #85876 - jeanlucthumm:master, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Add `go_to_first` query param to jump to first result

Fixes #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
  • Loading branch information
bors committed Jun 28, 2021
2 parents 3455304 + 6c5c0a5 commit d08a471
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/doc/rustdoc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
- [Lints](lints.md)
- [Advanced features](advanced-features.md)
- [Unstable features](unstable-features.md)
- [Website features](website-features.md)
- [Passes](passes.md)
- [References](references.md)
25 changes: 25 additions & 0 deletions src/doc/rustdoc/src/website-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Website features

These features are about using the website generated by `rustdoc`.

## Custom search engines

If you find yourself often referencing online Rust docs you might enjoy using a custom search
engine. This allows you to use the navigation bar directly to search a `rustdoc` website.
Most browsers support this feature by letting you define a URL template containing `%s`
which will be substituted for the search term. As an example, for the standard library you could use
this template:

```text
https://doc.rust-lang.org/stable/std/?search=%s
```

Note that this will take you to a results page listing all matches. If you want to navigate to the first
result right away (which is often the best match) use the following instead:

```text
https://doc.rust-lang.org/stable/std/?search=%s&go_to_first=true
```

This URL adds the `go_to_first=true` query parameter which can be appended to any `rustdoc` search URL
to automatically go to the first result.
8 changes: 4 additions & 4 deletions src/librustdoc/html/static/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,14 +1058,14 @@ window.initSearch = function(rawSearchIndex) {
return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>";
}

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

var filterCrates = getFilterCrates();
showResults(execSearch(query, index, filterCrates));
showResults(execSearch(query, index, filterCrates), params.go_to_first);
}

function buildIndex(rawSearchIndex) {
Expand Down

0 comments on commit d08a471

Please sign in to comment.