Skip to content

Commit f71d859

Browse files
committed
Fixed skipping first characters of search phrase
1 parent 5dd4c0e commit f71d859

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

docs/js/error-page.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
return;
66
}
77

8-
// Replace all TOC links with the correct version
8+
// Find the correct version
99
let currentVersion = '5.0';
1010
const branchNameRegexp = /\/en\/([a-z0-9-_.]*)\//g.exec(document.location.href);
1111

1212
if (branchNameRegexp !== null && branchNameRegexp.hasOwnProperty(1) && branchNameRegexp[1].length) {
1313
currentVersion = branchNameRegexp[1];
1414
}
1515

16+
// Replace all links in the TOC and in the error page content
1617
doc.querySelectorAll('.md-sidebar--primary .md-nav__item a, .page-not-found a').forEach(link => {
1718
link.href = link.href.replace(/\/en\/([a-z0-9-_.]*)\//, `/en/${currentVersion}/`);
1819
});
1920

21+
// Use the 404 URL path in initial search query
22+
const searchLink = document.querySelector('#search-link');
23+
const suffix = window.location.href.split('/en/' + currentVersion + '/')[1];
24+
25+
const searchQuery = suffix.replaceAll('/', ' ').replaceAll('_', ' ');
26+
searchLink.href = searchLink.href.replace('?sq=', '?sq=' + encodeURIComponent(searchQuery));
27+
2028
})(window, window.document);

theme/404.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,4 @@ <h1>Page not found.</h1>
88
<p>This link might be out-of-date. Go to the <a href="{{ site_url }}">main documentation page</a> or <a id="search-link"href="{{ site_url }}search_results/?sq=&p=1">use the search</a> to find what you are looking for.</p>
99
</div>
1010

11-
<script>
12-
// Get the actual 404 URL path and use it in initial search query
13-
const searchLink = document.querySelector('#search-link');
14-
const basePath = '{{ site_url }}';
15-
const suffix = window.location.href.substring(basePath.length);
16-
const searchQuery = suffix.replaceAll('/', ' ').replaceAll('_', ' ');
17-
searchLink.href = searchLink.href.replace('?sq=', '?sq=' + encodeURIComponent(searchQuery));
18-
</script>
19-
2011
{% endblock %}

0 commit comments

Comments
 (0)