|
5 | 5 | import { onMount } from 'svelte';
|
6 | 6 | import OnThisPage from './OnThisPage.svelte';
|
7 | 7 | import Breadcrumbs from './Breadcrumbs.svelte';
|
8 |
| - import { goto } from '$app/navigation'; |
9 |
| - import { page } from '$app/stores'; |
10 | 8 | import PageControls from '$lib/components/PageControls.svelte';
|
| 9 | + import { goto } from '$app/navigation'; |
11 | 10 |
|
12 | 11 | let { data } = $props();
|
13 | 12 |
|
|
21 | 20 | return `https://github.com/sveltejs/${name}/edit/main/documentation/${link}`;
|
22 | 21 | });
|
23 | 22 |
|
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; |
| 23 | + onMount(() => { |
| 24 | + // hash was lowercase in v4 docs and varying case in v5 docs |
| 25 | + const hash = location.hash.slice(1); |
29 | 26 |
|
30 |
| - // if there's an exact match, use that. no need to redirect |
| 27 | + // if there's no hash, or an exact match, no need to redirect |
31 | 28 | // also semi-handles the case where one appears twice with difference casing
|
32 | 29 | // e.g. https://svelte.dev/docs/kit/@sveltejs-kit#redirect vs https://svelte.dev/docs/kit/@sveltejs-kit#Redirect
|
33 | 30 | // but browsers make it impossible to really do: https://github.com/sveltejs/svelte.dev/issues/590
|
34 |
| - if (document.querySelector(`[id="${hash}"]`)) { |
| 31 | + if (hash === '' || content.querySelector(`[id="${hash}"]`)) { |
35 | 32 | return;
|
36 | 33 | }
|
37 | 34 |
|
38 |
| - const heading = document.querySelector(`[id="${hash}" i]`); |
39 |
| - if (heading) { |
40 |
| - const url = new URL($page.url); |
41 |
| - url.hash = heading.id; |
42 |
| - return url; |
43 |
| - } |
44 |
| - } |
| 35 | + const id = hash.toLowerCase().replaceAll(':', '-'); |
45 | 36 |
|
46 |
| - onMount(() => { |
47 |
| - const redirect = get_url_to_redirect_to(); |
48 |
| - if (redirect) { |
49 |
| - goto(redirect, { replaceState: true }); |
| 37 | + for (const heading of content.querySelectorAll('[id]')) { |
| 38 | + // e.g. we want to redirect progressive-enhancement-use-enhance to Progressive-enhancement-use:enhance |
| 39 | + if (heading.id.toLowerCase().replaceAll(':', '-') === id) { |
| 40 | + goto(`#${heading.id}`, { |
| 41 | + replaceState: true |
| 42 | + }); |
| 43 | +
|
| 44 | + break; |
| 45 | + } |
50 | 46 | }
|
51 | 47 | });
|
52 | 48 | </script>
|
|
0 commit comments