Skip to content

Commit 2f3b936

Browse files
authored
add router to 404 page back to nearest component version (PR #87)
1 parent 040a896 commit 2f3b936

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/partials/body-404.hbs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,51 @@
33
<article class="doc">
44
<h1 class="page">{{{or page.title 'Page Not Found'}}}</h1>
55
<div class="paragraph">
6-
<p>The page you're looking for does not exist. It may have been moved.</p>
6+
<p>The page you&#8217;re looking for does not exist. It may have been moved. You can{{#with site.homeUrl}} return to the <a id="return-link" href="{{{this}}}">start page</a>, or{{/with}} follow one of the links in the navigation above.</p>
77
</div>
88
<div class="paragraph">
9-
<p>If you arrived on this page by clicking on a link, please notify the owner of the site that the link is broken.
9+
<p>If you arrived on this page by clicking a link on another site, please notify the owner of that site that the link is broken.
1010
If you typed the URL of this page manually, please double check that you entered the address correctly.</p>
1111
</div>
1212
</article>
13+
<script>
14+
;(function (pathname, returnLink) {
15+
if (!returnLink) return
16+
var startPages = [
17+
{{#each site.components}}
18+
{ name: "{{{./name}}}", title: "{{{./title}}}", url: "{{{@root.site.path}}}{{{./url}}}", prefix: "{{{@root.site.path}}}/{{{./name}}}" },
19+
{{#each ./versions}}
20+
{{#unless (eq ./version 'master')}}
21+
{ name: "{{{./name}}}", title: "{{{./title}}}", version: "{{{./version}}}", url: "{{{@root.site.path}}}{{{./url}}}", prefix: "{{{@root.site.path}}}/{{{./name}}}/{{{./version}}}" },
22+
{{/unless}}
23+
{{/each}}
24+
{{/each}}
25+
].reduce(function (accum, it) {
26+
var prefix = it.prefix
27+
delete it.prefix
28+
if (('version' in it) && !it.url.startsWith(prefix + '/')) {
29+
var prefixSegments = prefix.split('/')
30+
var versionSegmentIdx = prefixSegments.findIndex((segment) => segment === it.version)
31+
prefixSegments.splice(versionSegmentIdx, 1, it.url.split('/')[versionSegmentIdx])
32+
prefix = prefixSegments.join('/')
33+
}
34+
accum[prefix] = it
35+
return accum
36+
}, {})
37+
var returnTo
38+
var segments = pathname.split('/').slice(0, -1)
39+
while (segments.length > 1) {
40+
if ((returnTo = startPages[segments.join('/')])) break
41+
segments.pop()
42+
}
43+
if (returnTo) {
44+
returnLink.href = returnTo.url
45+
returnLink.innerText =
46+
'start page for the ' + returnTo.title + ' ' +
47+
(returnTo.version ? returnTo.version + ' ' : '') +
48+
'documentation'
49+
}
50+
})(window.location.pathname, document.getElementById('return-link'))
51+
</script>
1352
</main>
1453
</div>

0 commit comments

Comments
 (0)