Skip to content

Commit 7da4f02

Browse files
committed
fix/simplify
1 parent ce13f93 commit 7da4f02

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

apps/svelte.dev/src/routes/docs/[...path]/+page.svelte

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import { onMount } from 'svelte';
66
import OnThisPage from './OnThisPage.svelte';
77
import Breadcrumbs from './Breadcrumbs.svelte';
8-
import { goto } from '$app/navigation';
9-
import { page } from '$app/stores';
108
import PageControls from '$lib/components/PageControls.svelte';
119
1210
let { data } = $props();
@@ -21,37 +19,27 @@
2119
return `https://github.com/sveltejs/${name}/edit/main/documentation/${link}`;
2220
});
2321
24-
// make hash case-insensitive
25-
// hash was lowercase in v4 docs and varying case in v5 docs
26-
function get_url_to_redirect_to() {
27-
const hash = $page.url.hash.slice(1);
28-
if (hash === '') return;
22+
onMount(() => {
23+
// hash was lowercase in v4 docs and varying case in v5 docs
24+
const hash = location.hash.slice(1);
2925
30-
// if there's an exact match, use that. no need to redirect
26+
// if there's no hash, or an exact match, no need to redirect
3127
// also semi-handles the case where one appears twice with difference casing
3228
// e.g. https://svelte.dev/docs/kit/@sveltejs-kit#redirect vs https://svelte.dev/docs/kit/@sveltejs-kit#Redirect
3329
// but browsers make it impossible to really do: https://github.com/sveltejs/svelte.dev/issues/590
34-
if (document.querySelector(`[id="${hash}"]`)) {
30+
if (hash === '' || content.querySelector(`[id="${hash}"]`)) {
3531
return;
3632
}
3733
38-
const headings = document.querySelectorAll('h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]');
39-
const id = hash.toLowerCase();
40-
for (const heading of headings) {
34+
const id = hash.toLowerCase().replaceAll(':', '-');
35+
36+
for (const heading of content.querySelectorAll('[id]')) {
4137
// e.g. we want to redirect progressive-enhancement-use-enhance to Progressive-enhancement-use:enhance
4238
if (heading.id.toLowerCase().replaceAll(':', '-') === id) {
43-
const url = new URL($page.url);
44-
url.hash = heading.id;
45-
return url;
39+
location.hash = heading.id;
40+
break;
4641
}
4742
}
48-
}
49-
50-
onMount(() => {
51-
const redirect = get_url_to_redirect_to();
52-
if (redirect) {
53-
goto(redirect, { replaceState: true });
54-
}
5543
});
5644
</script>
5745

0 commit comments

Comments
 (0)