Skip to content

Commit 38672e3

Browse files
committed
Fix index pages and sub pages
1 parent 86bb7ac commit 38672e3

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/components/EditThis.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import {useRouter} from 'next/router';
22
import {ExternalLink} from './ExternalLink';
33
import {IconGitHub} from './Icon/IconGitHub';
4+
import {ReactElement} from 'react';
45

56
const githubBranch = 'main';
67
const baseGithubLink = `https://github.com/reactjs/react.dev/edit/${githubBranch}/`;
78

8-
export const EditThis = () => {
9+
type EditThisProps = {
10+
path: string;
11+
isIndexPage: boolean;
12+
};
13+
14+
export const EditThis = ({path, isIndexPage}: EditThisProps): ReactElement => {
915
const {asPath} = useRouter();
10-
const cleanedPath = asPath.split(/[\?\#]/)[0];
16+
const pathParts = asPath.split(/[\?\#]/)[0].split('/');
17+
const pageToEdit = isIndexPage ? 'index' : pathParts[pathParts.length - 1];
18+
1119
return (
1220
<div className="flex text-link dark:text-link-dark group justify-start align-middle">
1321
<IconGitHub />
14-
<ExternalLink href={`${baseGithubLink}src/content/${cleanedPath}.md`}>
22+
<ExternalLink
23+
href={`${baseGithubLink}src/content${path}/${pageToEdit}.md`}>
1524
<span className="text-lg group-hover:underline">
1625
Edit this page on Github
1726
</span>

src/components/Layout/Page.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ export function Page({
9090
</LanguagesContext.Provider>
9191
</TocContext.Provider>
9292
</div>
93-
{isReferencePage && <EditThis />}
93+
{isReferencePage && route && (
94+
<EditThis
95+
isIndexPage={
96+
routeTree.routes?.find((el: RouteItem): boolean | undefined =>
97+
el.path?.startsWith(route.path!)
98+
)?.path === route.path
99+
}
100+
path={route?.path || routeTree.path || ''}
101+
/>
102+
)}
94103
{!isBlogIndex && (
95104
<DocsPageFooter
96105
route={route}

0 commit comments

Comments
 (0)