Skip to content

Commit aefe9b0

Browse files
committed
Auto merge of #54609 - kzys:404-search, r=GuillaumeGomez
Add the library search box on the 404 page It actually has a link to search already, but it would be better to have the search "box" as like index.md to be consistent. <style> can be shared with index.md, but these pages currently use https://doc.rust-lang.org/rust.css directly. Fixes #14572.
2 parents b9adc33 + 23af6bb commit aefe9b0

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

Diff for: src/doc/not_found.md

+54-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
#TOC { display: none; }
66
.header-section-number { display: none; }
77
li {list-style-type: none; }
8+
#search-input {
9+
width: calc(100% - 100px);
10+
}
11+
#search-but {
12+
cursor: pointer;
13+
}
14+
#search-but, #search-input {
15+
padding: 4px;
16+
border: 1px solid #ccc;
17+
border-radius: 3px;
18+
outline: none;
19+
font-size: 0.7em;
20+
background-color: #fff;
21+
}
22+
#search-but:hover, #search-input:focus {
23+
border-color: #55a9ff;
24+
}
25+
#search-from {
26+
border: none;
27+
padding: 0;
28+
font-size: 0.7em;
29+
}
830
</style>
931

1032
Looks like you've taken a wrong turn.
@@ -13,11 +35,20 @@ Some things that might be helpful to you though:
1335

1436
# Search
1537

16-
<form action="https://duckduckgo.com/">
17-
<input type="text" id="site-search" name="q" size="80"></input>
18-
<input type="submit" value="Search DuckDuckGo"></form>
19-
20-
Rust doc search: <span id="core-search"></span>
38+
<div>
39+
<form id="search-form" action="https://duckduckgo.com/">
40+
<input id="search-input" type="search" name="q"></input>
41+
<input type="submit" value="Search" id="search-but">
42+
<!--
43+
Don't show the options by default,
44+
since "From the Standary Library" doesn't work without JavaScript
45+
-->
46+
<fieldset id="search-from" style="display:none">
47+
<label><input name="from" value="library" type="radio"> From the Standard Library</label>
48+
<label><input name="from" value="duckduckgo" type="radio" checked> From DuckDuckGo</label>
49+
</fieldset>
50+
</form>
51+
</div>
2152

2253
# Reference
2354

@@ -44,26 +75,28 @@ function get_url_fragments() {
4475
return op;
4576
}
4677

47-
function populate_site_search() {
48-
var op = get_url_fragments();
78+
function on_submit(event) {
79+
var form = event.target;
80+
var q = form['q'].value;
81+
82+
event.preventDefault();
4983

50-
var search = document.getElementById('site-search');
51-
search.value = op.join(' ') + " site:doc.rust-lang.org";
84+
if (form['from'].value === 'duckduckgo') {
85+
document.location.href = form.action + '?q=' + encodeURIComponent(q + ' site:doc.rust-lang.org');
86+
} else if (form['from'].value === 'library') {
87+
document.location.href = 'std/index.html?search=' + encodeURIComponent(q);
88+
}
5289
}
5390

54-
function populate_rust_search() {
55-
var op = get_url_fragments();
56-
var lt = op.pop();
91+
function populate_search() {
92+
var form = document.getElementById('search-form');
93+
form.addEventListener('submit', on_submit);
94+
document.getElementById('search-from').style.display = '';
5795

58-
// #18540, use a single token
96+
form['from'].value = 'library';
5997

60-
var a = document.createElement("a");
61-
a.href = "https://doc.rust-lang.org/core/?search=" + encodeURIComponent(lt);
62-
a.textContent = lt;
63-
var search = document.getElementById('core-search');
64-
search.innerHTML = "";
65-
search.appendChild(a);
98+
var op = get_url_fragments();
99+
document.getElementById('search-input').value = op.join(' ');
66100
}
67-
populate_site_search();
68-
populate_rust_search();
101+
populate_search();
69102
</script>

0 commit comments

Comments
 (0)