|
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'; |
11 | 9 |
|
12 | 10 | let { data } = $props(); |
|
21 | 19 | return `https://github.com/sveltejs/${name}/edit/main/documentation/${link}`; |
22 | 20 | }); |
23 | 21 |
|
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); |
29 | 25 |
|
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 |
31 | 27 | // also semi-handles the case where one appears twice with difference casing |
32 | 28 | // e.g. https://svelte.dev/docs/kit/@sveltejs-kit#redirect vs https://svelte.dev/docs/kit/@sveltejs-kit#Redirect |
33 | 29 | // 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}"]`)) { |
35 | 31 | return; |
36 | 32 | } |
37 | 33 |
|
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]')) { |
41 | 37 | // e.g. we want to redirect progressive-enhancement-use-enhance to Progressive-enhancement-use:enhance |
42 | 38 | 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; |
46 | 41 | } |
47 | 42 | } |
48 | | - } |
49 | | -
|
50 | | - onMount(() => { |
51 | | - const redirect = get_url_to_redirect_to(); |
52 | | - if (redirect) { |
53 | | - goto(redirect, { replaceState: true }); |
54 | | - } |
55 | 43 | }); |
56 | 44 | </script> |
57 | 45 |
|
|
0 commit comments