|
1 | 1 | --- |
2 | 2 | import Layout from "../layouts/BaseLayout.astro"; |
3 | | -
|
4 | | -/** |
5 | | - * Returns the last (non-empty) segment from the slash-separated path, or "" |
6 | | - * |
7 | | - * Discards any hash part of the URL before splitting it. |
8 | | - * |
9 | | - * @param path string to extract the last part of |
10 | | - * @returns the last non-empty segment or an empty string |
11 | | - * |
12 | | - * @example |
13 | | - * An input of `/reference/p5/basematerialshader/` will return `basematerialshader` |
14 | | - * An input of `/reference/p5.Vector/dist/#Examples` will return `dist` |
15 | | - */ |
16 | | -function getLastPathElementOrEmptyString(path: string): string { |
17 | | - //Get the part of the path before the (first) hash |
18 | | - const preHashPath = path.split("#")[0] ?? ""; |
19 | | - //Then get the last non-empty slash-separated segment from that |
20 | | - return preHashPath.split("/").filter(Boolean).pop() ?? ""; |
21 | | -} |
22 | | -
|
23 | | -const currentPath = Astro.url.pathname; |
24 | | -const finalTerm = getLastPathElementOrEmptyString(currentPath); |
25 | 3 | --- |
26 | 4 |
|
27 | 5 | <Layout title="404: Page Not Found"> |
28 | 6 | <main class="flex flex-col items-start gap-8 py-8 px-2 prose max-w-none"> |
29 | 7 | <p> |
30 | 8 | Sorry, we couldn't find your exact page |
31 | | - <code>{currentPath}</code>. |
| 9 | + <code id="display-path"></code>. |
32 | 10 |
|
33 | 11 | <div class="flex flex-col gap-4"> |
34 | | - <a |
35 | | - href={`https://beta.p5js.org/search/?term=${finalTerm}`} |
36 | | - class="btn" |
37 | | - > |
38 | | - Search here for <code>{finalTerm}</code> |
| 12 | + <a id="search-link" href="/search/" class="btn"> |
| 13 | + Search here for <code id="display-search-term"></code> |
39 | 14 | </a> |
40 | 15 | or |
41 | 16 | <a href="/" class="btn secondary">go home</a> |
42 | 17 | </div> |
43 | 18 | </p> |
44 | 19 | </main> |
| 20 | + <script is:inline> |
| 21 | + (function update404PageDetails() { |
| 22 | + function getLastPathSegment(path) { |
| 23 | + return path.split("/").filter(Boolean).pop() ?? ""; |
| 24 | + } |
| 25 | + |
| 26 | + // 1. Get the last meaningful segment from the URL path, excluding hash part |
| 27 | + // 2. Use that in the link href to the search |
| 28 | + // 3. Also display it and the failed path |
| 29 | + |
| 30 | + //(This .pathname already strips away any hash part) |
| 31 | + const realPath = window.location.pathname; |
| 32 | + const term = getLastPathSegment(realPath); |
| 33 | + |
| 34 | + const displayPathEl = document.getElementById("display-path"); |
| 35 | + const searchLinkEl = document.getElementById("search-link"); |
| 36 | + const displaySearchTermEl = document.getElementById("display-search-term"); |
| 37 | + |
| 38 | + if (displayPathEl) { |
| 39 | + displayPathEl.textContent = realPath; |
| 40 | + } |
| 41 | + if (displaySearchTermEl) { |
| 42 | + displaySearchTermEl.textContent = term; |
| 43 | + } |
| 44 | + if (searchLinkEl) { |
| 45 | + searchLinkEl.href = `/search/?term=${encodeURIComponent(term)}`; |
| 46 | + } |
| 47 | + })(); |
| 48 | + </script> |
45 | 49 | </Layout> |
0 commit comments