-
-
Notifications
You must be signed in to change notification settings - Fork 190
fix: redirect hashes with different casing #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a8962e1
8f94aba
84c617e
a4bf76f
534d162
d7e3b07
4d9a7a5
692df61
1739cc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ | |
import { Text } from '@sveltejs/site-kit/components'; | ||
import { legacy_details } from '@sveltejs/site-kit/actions'; | ||
import { setupDocsHovers } from '@sveltejs/site-kit/docs'; | ||
import { onMount } from 'svelte'; | ||
import OnThisPage from './OnThisPage.svelte'; | ||
import Breadcrumbs from './Breadcrumbs.svelte'; | ||
import { goto } from '$app/navigation'; | ||
import { page } from '$app/stores'; | ||
import PageControls from '$lib/components/PageControls.svelte'; | ||
let { data } = $props(); | ||
|
@@ -17,6 +20,37 @@ | |
const link = 'docs/' + data.document.file.split('/').slice(2).join('/'); | ||
return `https://github.com/sveltejs/${name}/edit/main/documentation/${link}`; | ||
}); | ||
// make hash case-insensitive | ||
// hash was lowercase in v4 docs and varying case in v5 docs | ||
function get_url_to_redirect_to() { | ||
const hash = $page.url.hash.replace(/^#/i, ''); | ||
if (!hash) return; | ||
benmccann marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const elements = document.querySelectorAll('*[id]'); | ||
// if there's an exact match, use that. no need to redirect | ||
// also handles the case where one appears twice with difference casing | ||
// e.g. https://svelte.dev/docs/kit/@sveltejs-kit#redirect vs https://svelte.dev/docs/kit/@sveltejs-kit#Redirect | ||
for (const el of elements) { | ||
|
||
if (el.id === hash) { | ||
return; | ||
} | ||
} | ||
for (const el of elements) { | ||
if (el.id.toLocaleLowerCase() === hash.toLocaleLowerCase()) { | ||
const url = $page.url; | ||
url.hash = el.id; | ||
|
||
return url; | ||
} | ||
} | ||
} | ||
onMount(() => { | ||
const redirect = get_url_to_redirect_to(); | ||
if (redirect) { | ||
goto(redirect, { replaceState: true }); | ||
} | ||
}); | ||
</script> | ||
|
||
<svelte:head> | ||
|
Uh oh!
There was an error while loading. Please reload this page.