Skip to content
Merged
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
47 changes: 34 additions & 13 deletions packages/chronicle/src/components/mdx/link.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { Link as ApsaraLink } from '@raystack/apsara';
import type { ComponentProps } from 'react';
import { Link as RouterLink } from 'react-router';
import type { ComponentProps, MouseEvent } from 'react';
import { useNavigate } from 'react-router';

type LinkProps = ComponentProps<'a'>;

export function Link({ href, children, ...props }: LinkProps) {
export function Link({ href, children, onClick: onClickProp, ...props }: LinkProps) {
const navigate = useNavigate();

if (!href) {
return <span {...props}>{children}</span>;
}

const isExternal = href.startsWith('http://') || href.startsWith('https://');
const isAnchor = href.startsWith('#');

if (isAnchor) {
return (
<ApsaraLink href={href} {...props}>
{children}
</ApsaraLink>
);
}

if (isExternal) {
return (
<ApsaraLink
Expand All @@ -33,9 +27,36 @@ export function Link({ href, children, ...props }: LinkProps) {
);
}

if (isAnchor) {
return (
<ApsaraLink href={href} {...props}>
{children}
</ApsaraLink>
);
}

const onClick = (e: MouseEvent<HTMLAnchorElement>) => {
if (
e.defaultPrevented ||
e.button !== 0 ||
e.metaKey ||
e.ctrlKey ||
e.shiftKey ||
e.altKey
) {
return;
}

onClickProp?.(e);
if (e.defaultPrevented) return;

e.preventDefault();
navigate(href);
};

return (
<RouterLink to={href} className={props.className}>
<ApsaraLink href={href} {...props} onClick={onClick}>
{children}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
</RouterLink>
</ApsaraLink>
);
}
2 changes: 1 addition & 1 deletion packages/chronicle/src/pages/DocsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function DocsPage({ slug }: DocsPageProps) {
/>
<Page
page={{
slug,
slug: page.slug,
frontmatter: page.frontmatter,
content: page.content,
toc: page.toc
Expand Down
1 change: 1 addition & 0 deletions packages/chronicle/src/server/api/page/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './[...slug]';