|
2 | 2 | import { Text } from '@sveltejs/site-kit/components';
|
3 | 3 | import { legacy_details } from '@sveltejs/site-kit/actions';
|
4 | 4 | import { setupDocsHovers } from '@sveltejs/site-kit/docs';
|
| 5 | + import { onMount } from 'svelte'; |
5 | 6 | import OnThisPage from './OnThisPage.svelte';
|
6 | 7 | import Breadcrumbs from './Breadcrumbs.svelte';
|
| 8 | + import { goto } from '$app/navigation'; |
| 9 | + import { page } from '$app/stores'; |
7 | 10 | import PageControls from '$lib/components/PageControls.svelte';
|
8 | 11 |
|
9 | 12 | let { data } = $props();
|
|
17 | 20 | const link = 'docs/' + data.document.file.split('/').slice(2).join('/');
|
18 | 21 | return `https://github.com/sveltejs/${name}/edit/main/documentation/${link}`;
|
19 | 22 | });
|
| 23 | +
|
| 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; |
| 29 | +
|
| 30 | + // if there's an exact match, use that. no need to redirect |
| 31 | + // also semi-handles the case where one appears twice with difference casing |
| 32 | + // e.g. https://svelte.dev/docs/kit/@sveltejs-kit#redirect vs https://svelte.dev/docs/kit/@sveltejs-kit#Redirect |
| 33 | + // but browsers make it impossible to really do: https://github.com/sveltejs/svelte.dev/issues/590 |
| 34 | + if (document.querySelector(`[id="${hash}"]`)) { |
| 35 | + return; |
| 36 | + } |
| 37 | +
|
| 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 | + } |
| 45 | +
|
| 46 | + onMount(() => { |
| 47 | + const redirect = get_url_to_redirect_to(); |
| 48 | + if (redirect) { |
| 49 | + goto(redirect, { replaceState: true }); |
| 50 | + } |
| 51 | + }); |
20 | 52 | </script>
|
21 | 53 |
|
22 | 54 | <svelte:head>
|
|
0 commit comments