Skip to content

Commit

Permalink
fix hash: slug (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Dec 10, 2023
1 parent 478d417 commit b8b4bce
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions packages/ui/app/src/mdx/base-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import classNames from "classnames";
import Link from "next/link";
import React, { AnchorHTMLAttributes, HTMLAttributes } from "react";
import { AbsolutelyPositionedAnchor } from "../commons/AbsolutelyPositionedAnchor";
import { useNavigationContext } from "../navigation-context";
import { onlyText } from "../util/onlyText";

type InlineCodeProps = {
Expand Down Expand Up @@ -93,6 +94,16 @@ const flatten = (
: React.Children.toArray((child as React.ReactElement).props.children).reduce(flatten, text);
};

/**
* By default, next will use /host/current/slug in SSG.
* Because of our custom routing (PathResolver) implementation, we need to override the pathname to be /basePath/current/slug.
* @returns /basepath/current/slug
*/
function useCurrentPathname(): string {
const { basePath, resolvedPath } = useNavigationContext();
return basePath != null ? `/${basePath}/${resolvedPath.fullSlug}` : `/${resolvedPath.fullSlug}`;
}

export const H1: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ...rest }) => {
const children = React.Children.toArray(rest.children);
const text = children.reduce(flatten, "");
Expand All @@ -106,7 +117,10 @@ export const H1: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ..
)}
{...rest}
>
<AbsolutelyPositionedAnchor href={{ hash: slug }} verticalPosition="center" />
<AbsolutelyPositionedAnchor
href={{ hash: slug, pathname: useCurrentPathname() }}
verticalPosition="center"
/>
<span>{children}</span>
</h1>
);
Expand All @@ -125,7 +139,10 @@ export const H2: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ..
)}
{...rest}
>
<AbsolutelyPositionedAnchor href={{ hash: slug }} verticalPosition="center" />
<AbsolutelyPositionedAnchor
href={{ hash: slug, pathname: useCurrentPathname() }}
verticalPosition="center"
/>
{children}
</h2>
);
Expand All @@ -144,7 +161,10 @@ export const H3: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ..
)}
{...rest}
>
<AbsolutelyPositionedAnchor href={{ hash: slug }} verticalPosition="center" />
<AbsolutelyPositionedAnchor
href={{ hash: slug, pathname: useCurrentPathname() }}
verticalPosition="center"
/>
{children}
</h3>
);
Expand All @@ -163,7 +183,10 @@ export const H4: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ..
)}
{...rest}
>
<AbsolutelyPositionedAnchor href={{ hash: slug }} verticalPosition="center" />
<AbsolutelyPositionedAnchor
href={{ hash: slug, pathname: useCurrentPathname() }}
verticalPosition="center"
/>
{children}
</h4>
);
Expand All @@ -182,7 +205,10 @@ export const H5: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ..
)}
{...rest}
>
<AbsolutelyPositionedAnchor href={{ hash: slug }} verticalPosition="center" />
<AbsolutelyPositionedAnchor
href={{ hash: slug, pathname: useCurrentPathname() }}
verticalPosition="center"
/>
{children}
</h5>
);
Expand All @@ -201,7 +227,10 @@ export const H6: React.FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ..
)}
{...rest}
>
<AbsolutelyPositionedAnchor href={{ hash: slug }} verticalPosition="center" />
<AbsolutelyPositionedAnchor
href={{ hash: slug, pathname: useCurrentPathname() }}
verticalPosition="center"
/>
{children}
</h6>
);
Expand Down

1 comment on commit b8b4bce

@vercel
Copy link

@vercel vercel bot commented on b8b4bce Dec 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

fern-dev – ./packages/ui/fe-bundle

devtest.buildwithfern.com
fern-dev-buildwithfern.vercel.app
app-dev.buildwithfern.com
fern-dev-git-main-buildwithfern.vercel.app

Please sign in to comment.