This commit is contained in:
Timothy Jaeryang Baek
2026-01-09 03:31:27 +04:00
parent b2a1f71d92
commit 9a9a824fe9

View File

@@ -4,6 +4,7 @@
import type { Token } from 'marked';
import { getContext } from 'svelte';
import { goto } from '$app/navigation';
const i18n = getContext('i18n');
@@ -24,6 +25,27 @@
export let tokens: Token[];
export let sourceIds = [];
export let onSourceClick: Function = () => {};
/**
* Handle link clicks - intercept same-origin app URLs for in-app navigation
*/
const handleLinkClick = (e: MouseEvent, href: string) => {
try {
const url = new URL(href, window.location.origin);
// Check if same origin and an in-app route
if (
url.origin === window.location.origin &&
(url.pathname.startsWith('/notes/') ||
url.pathname.startsWith('/c/') ||
url.pathname.startsWith('/channels/'))
) {
e.preventDefault();
goto(url.pathname + url.search + url.hash);
}
} catch {
// Invalid URL, let browser handle it
}
};
</script>
{#each tokens as token, tokenIdx (tokenIdx)}
@@ -33,11 +55,11 @@
<HtmlToken {id} {token} {onSourceClick} />
{:else if token.type === 'link'}
{#if token.tokens}
<a href={token.href} target="_blank" rel="nofollow" title={token.title}>
<a href={token.href} target="_blank" rel="nofollow" title={token.title} on:click={(e) => handleLinkClick(e, token.href)}>
<svelte:self id={`${id}-a`} tokens={token.tokens} {onSourceClick} {done} />
</a>
{:else}
<a href={token.href} target="_blank" rel="nofollow" title={token.title}>{token.text}</a>
<a href={token.href} target="_blank" rel="nofollow" title={token.title} on:click={(e) => handleLinkClick(e, token.href)}>{token.text}</a>
{/if}
{:else if token.type === 'image'}
<Image src={token.href} alt={token.text} />