Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/SiteTitle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import config from 'virtual:starlight/user-config';
const { siteTitle, siteTitleHref } = Astro.locals.starlightRoute;
---

<a href={'/intro'} class='site-title sl-flex'>
<a href={'/intro/'} class='site-title sl-flex'>
{
config.logo && logos.dark && (
<>
Expand Down
8 changes: 6 additions & 2 deletions src/modules/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ export function getRedirectedPath(hash: string) {
if (!hash || hash === '#/' || hash === '#') {
return '/intro/';
} else if (hash.startsWith('#/')) {
const [newPath, newHash] = hash.slice(2).split('?id=');
return newHash ? `/${newPath}/#${newHash}` : `/${newPath}/`;
const [rawPath, newHash] = hash.slice(2).split('?id=');
const cleanPath = rawPath.replace(/^\/+|\/+$/g, '');
if (!cleanPath) {
return '/intro/';
}
return newHash ? `/${cleanPath}/#${newHash}` : `/${cleanPath}/`;
} else {
return '/intro/';
}
Expand Down
6 changes: 2 additions & 4 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
-->
<script>
import { getRedirectedPath } from '../modules/redirect';
document.addEventListener('DOMContentLoaded', () => {
const hash = getRedirectedPath(window.location.hash);
window.location.replace(hash);
});
const hash = getRedirectedPath(window.location.hash);
window.location.replace(hash);
</script>
<style>
.loader {
Expand Down
15 changes: 15 additions & 0 deletions tests/redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ describe('getRedirectedPath', () => {
expect(result).toBe('/path/');
});

it('should redirect "#/path/" with trailing slash to /path/ without duplicate slashes', () => {
const result = getRedirectedPath('#/path/');
expect(result).toBe('/path/');
});

it('should redirect "#/path?id=hash" to /path/#hash', () => {
const result = getRedirectedPath('#/path?id=hash');
expect(result).toBe('/path/#hash');
});

it('should redirect "#/path/?id=hash" with trailing slash to /path/#hash', () => {
const result = getRedirectedPath('#/path/?id=hash');
expect(result).toBe('/path/#hash');
});

it('should redirect "#///" to /intro/', () => {
const result = getRedirectedPath('#///');
expect(result).toBe('/intro/');
});
});
Loading