File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change 11import { useRouter } from 'next/router' ;
22import { ExternalLink } from './ExternalLink' ;
33import { IconGitHub } from './Icon/IconGitHub' ;
4+ import { ReactElement } from 'react' ;
45
56const githubBranch = 'main' ;
67const 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 >
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments