Skip to content

Commit 9bedfdd

Browse files
authored
Make error page version aware (#2935)
* Make error page version aware * Fixed search link * Removed alt text from image * Removed double slash
1 parent dc8ea75 commit 9bedfdd

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

docs/css/page-not-found.css

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,13 @@
11
.page-not-found {
2-
text-align: center;
3-
margin: 50px;
2+
display:flex;
3+
flex-direction: column;
4+
align-items: center;
5+
margin: 50px;
46
}
57

68
.page-not-found h1 {
79
font-weight: normal;
10+
text-align: center;
811
font-size: 37px;
912
margin: 30px 0 10px;
1013
}
11-
12-
.page-not-found h2 {
13-
font-weight: normal;
14-
font-size: 30px;
15-
margin: 0 0 70px;
16-
}
17-
18-
.page-not-found h3 {
19-
font-weight: normal;
20-
font-size: 25px;
21-
margin: 0 0 20px;
22-
color: var(--dark-grey);
23-
}
24-
25-
.page-not-found ul {
26-
display: inline-flex;
27-
list-style-type: none;
28-
margin: 0;
29-
}
30-
31-
.page-not-found ul li {
32-
padding: 0 30px;
33-
margin: 0;
34-
}
35-
36-
.page-not-found li + li {
37-
border-left: 1px solid black;
38-
}

docs/js/error-page.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function (global, doc) {
2+
const errorPageContainer = doc.querySelector('.page-not-found');
3+
4+
if (!errorPageContainer) {
5+
return;
6+
}
7+
8+
// Replace all TOC links with the correct version
9+
let currentVersion = '5.0';
10+
const branchNameRegexp = /\/en\/([a-z0-9-_.]*)\//g.exec(document.location.href);
11+
12+
if (branchNameRegexp !== null && branchNameRegexp.hasOwnProperty(1) && branchNameRegexp[1].length) {
13+
currentVersion = branchNameRegexp[1];
14+
}
15+
16+
doc.querySelectorAll('.md-sidebar--primary .md-nav__item a, .page-not-found a').forEach(link => {
17+
link.href = link.href.replace(/\/en\/([a-z0-9-_.]*)\//, `/en/${currentVersion}/`);
18+
});
19+
20+
})(window, window.document);

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ extra_javascript:
10051005
- js/jquery.min.js
10061006
- js/jquery-ui.min.js
10071007
- js/custom.js
1008+
- js/error-page.js
10081009
- js/release-notes.js
10091010
- js/docs.switcher.js
10101011
- '//cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js'

theme/404.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
{% block content %}
44
<div class="page-not-found">
5-
<img src="/images/404.png" alt="Not found" />
65
<h1>Page not found.</h1>
7-
<a href="/">Back to front page</a>
6+
<img src="/images/404.png" alt="" />
7+
{% set site_url = config.site_url | default(nav.homepage.url, true) | url %}
8+
<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>
89
</div>
10+
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+
920
{% endblock %}

0 commit comments

Comments
 (0)