Skip to content

Commit 348f728

Browse files
404 fallback logic to /reference and /articles
1 parent ffd6637 commit 348f728

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

web/public/_redirects

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
/wiki /
22
/wiki/* /:splat
33
/Main_Page /
4-
5-
# Fallbacks
6-
/* /articles/:splat 200
7-
/* /reference/:splat 200

web/src/components/EnhancedMarkdown.astro

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ interface Props {
99
const { content } = Astro.props;
1010
1111
12-
// We can't do this as we're now organizing pages in folders
13-
14-
// function convertMediaWikiLinks(text: string): string {
15-
// return text.replace(
16-
// /\[\[([^|\]#]+)(?:#([^\]]+))?(?:\|([^\]]+))?\]\]/g,
17-
// (_, link, hash, text) => {
18-
// const url = `/${link}${hash ? `#${hash}` : ''}`;
19-
// return `[${text || link}](${url})`;
20-
// }
21-
// );
22-
// }
23-
24-
// const processedContent = convertMediaWikiLinks(content);
25-
// const tokens = marked.lexer(processedContent);
12+
// We shouldn't this as we're now organizing pages in folders
13+
// TODO let's move away from MediaWiki links in the future
14+
15+
function convertMediaWikiLinks(text: string): string {
16+
return text.replace(
17+
/\[\[([^|\]#]+)(?:#([^\]]+))?(?:\|([^\]]+))?\]\]/g,
18+
(_, link, hash, text) => {
19+
const url = `/${link}${hash ? `#${hash}` : ''}`;
20+
return `[${text || link}](${url})`;
21+
}
22+
);
23+
}
2624
27-
const tokens = marked.lexer(content);
25+
const processedContent = convertMediaWikiLinks(content);
26+
const tokens = marked.lexer(processedContent);
2827
---
2928

3029
{

web/src/pages/404.astro

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,39 @@ import { OLD_WIKI_URL, OLD_WIKI_REDIRECT } from '@src/content.constants';
2020
if (!OLD_WIKI_REDIRECT) {
2121
return;
2222
}
23-
let countdown = 5;
24-
const el = document.getElementById('countdown');
25-
const interval = setInterval(() => {
26-
countdown -= 1;
27-
if (el) el.textContent = countdown;
28-
if (countdown <= 0) {
29-
clearInterval(interval);
23+
24+
async function tryFallbacks() {
3025
let path = location.pathname;
3126
// Remove trailing slash if any
3227
path = path.endsWith('/') ? path.slice(0, -1) : path;
33-
window.location.replace(OLD_WIKI_URL + path);
28+
29+
const tryPath = async (prefix) => {
30+
const testPath = `${prefix}${path}`;
31+
try {
32+
const res = await fetch(testPath, { method: 'HEAD' });
33+
if (res.ok) {
34+
window.location.replace(testPath);
35+
return true;
36+
}
37+
} catch (_) {}
38+
return false;
39+
};
40+
41+
if (await tryPath('/reference')) return;
42+
if (await tryPath('/articles')) return;
43+
44+
// Fallback to old wiki
45+
let countdown = 5;
46+
const el = document.getElementById('countdown');
47+
const interval = setInterval(() => {
48+
countdown -= 1;
49+
if (el) el.textContent = countdown;
50+
if (countdown <= 0) {
51+
clearInterval(interval);
52+
window.location.replace(OLD_WIKI_URL + path);
53+
}
54+
}, 1000);
3455
}
35-
}, 1000);
56+
57+
tryFallbacks();
3658
</script>

0 commit comments

Comments
 (0)