Skip to content

Commit 082bb20

Browse files
authored
Fix "Edit page" link pointing to wrong URL in some cases (graphprotocol#52)
1 parent 3d2555b commit 082bb20

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

components/EditPageLink.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HTMLAttributes } from 'react'
1+
import { HTMLAttributes, useContext } from 'react'
22
import {
33
Text,
44
Flex,
@@ -11,6 +11,7 @@ import {
1111
useUniqueId,
1212
} from '@edgeandnode/components'
1313

14+
import { NavContext } from '@/layout'
1415
import { Link } from '@/components'
1516
import { useI18n } from '@/hooks'
1617

@@ -19,15 +20,14 @@ export type EditPageLinkProps = {
1920
} & Omit<HTMLAttributes<HTMLElement>, 'children'>
2021

2122
export const EditPageLink = ({ mobile = false, ...props }: EditPageLinkProps) => {
22-
const { currentLocale, currentPathWithoutLocale, translations } = useI18n()
23+
const { pagePath } = useContext(NavContext)!
24+
const { translations } = useI18n()
2325
const linkClass = useUniqueId('class')
2426

2527
return (
2628
<Link
2729
className={linkClass}
28-
href={`https://github.com/graphprotocol/docs/blob/main/pages/${currentLocale}${currentPathWithoutLocale}${
29-
currentPathWithoutLocale.endsWith('/') ? 'index' : ''
30-
}.mdx`}
30+
href={`https://github.com/graphprotocol/docs/blob/main/pages/${pagePath}`}
3131
target="_blank"
3232
sx={{
3333
display: 'block',

layout/MDXLayout.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const mdxStyles = {
6161
} as ThemeUIStyleObject
6262

6363
export type NavContextProps = {
64+
pagePath: string
6465
navItems: NavItem[]
6566
pageNavItems: NavItemPage[]
6667
previousPage: NavItemPage | null
@@ -90,10 +91,10 @@ export type DocumentContextProps = {
9091
export const DocumentContext = createContext(null) as Context<DocumentContextProps | null>
9192

9293
export type MDXLayoutProps = PropsWithChildren<
93-
Pick<NavContextProps, 'navItems'> & Pick<DocumentContextProps, 'frontmatter' | 'outline'>
94+
Pick<NavContextProps, 'pagePath' | 'navItems'> & Pick<DocumentContextProps, 'frontmatter' | 'outline'>
9495
>
9596

96-
export const MDXLayout = ({ navItems, frontmatter, outline, children }: MDXLayoutProps) => {
97+
export const MDXLayout = ({ pagePath, navItems, frontmatter, outline, children }: MDXLayoutProps) => {
9798
const { currentPathWithoutLocale } = useI18n()
9899

99100
// Compute some values for the `NavContext`
@@ -151,7 +152,7 @@ export const MDXLayout = ({ navItems, frontmatter, outline, children }: MDXLayou
151152
}, [outline, outlineItemIsInOrAboveView])
152153

153154
return (
154-
<NavContext.Provider value={{ navItems, pageNavItems, previousPage, currentPage, nextPage }}>
155+
<NavContext.Provider value={{ pagePath, navItems, pageNavItems, previousPage, currentPage, nextPage }}>
155156
<DocumentContext.Provider value={{ frontmatter, outline, markOutlineItem, highlightedOutlineItemId }}>
156157
<Head>
157158
<title>{frontmatter?.title ? `${frontmatter.title} - ` : ''}The Graph Docs</title>

lib/remarkMdxLayout.mjs

+41-12
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,61 @@ import path from 'path'
99

1010
const JSXParser = Parser.extend(acornJsx())
1111

12-
const getAstToInject = function (locale, outline) {
12+
const getAstToInject = function (pagePath, outline) {
13+
const isDynamic = pagePath.includes('[locale]')
14+
const locale = isDynamic ? null : pagePath.substring(0, 2)
15+
1316
return {
1417
type: 'mdxjsEsm',
1518
value: '',
1619
data: {
1720
estree: JSXParser.parse(
1821
`
1922
import { MDXLayout as _MDXLayout } from '@/layout'
23+
import { Locale } from '@/i18n'
2024
import { getNavItems } from '@/navigation'
2125
22-
export const getStaticProps = async () => {
23-
const navItems = await getNavItems('${locale}')
26+
const outline = ${Serializer.run(outline)}
2427
25-
return {
26-
props: {
27-
navItems,
28-
},
28+
${
29+
isDynamic
30+
? `
31+
export const getStaticPaths = async () => {
32+
return {
33+
paths: Object.values(Locale).map((locale) => ({
34+
params: { locale },
35+
})),
36+
fallback: false,
37+
}
2938
}
39+
40+
export const getStaticProps = async (context) => {
41+
const navItems = await getNavItems(context.params?.locale)
42+
43+
return {
44+
props: {
45+
navItems,
46+
},
47+
}
48+
}
49+
`
50+
: `
51+
export const getStaticProps = async () => {
52+
const navItems = await getNavItems('${locale}')
53+
54+
return {
55+
props: {
56+
navItems,
57+
},
58+
}
59+
}
60+
`
3061
}
3162
3263
export default function ({ navItems, children }) {
33-
const outline = ${Serializer.run(outline)}
34-
3564
return (
3665
<_MDXLayout
66+
pagePath="${pagePath}"
3767
navItems={navItems}
3868
frontmatter={typeof frontmatter !== 'undefined' ? frontmatter : undefined}
3969
outline={outline}
@@ -82,9 +112,8 @@ export const remarkMdxLayout = () => {
82112
})
83113
})
84114

85-
const relativePath = path.relative(path.join(process.cwd(), 'pages'), file.path)
86-
const locale = relativePath.substring(0, 2)
87-
const astToInject = getAstToInject(locale, outline)
115+
const pagePath = path.relative(path.join(process.cwd(), 'pages'), file.path)
116+
const astToInject = getAstToInject(pagePath, outline)
88117

89118
ast.children.push(astToInject)
90119
}

pages/[locale]/index.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ const Index = ({ navItems }: { navItems: NavItem[] }) => {
5252
const { currentLocale, translations } = useI18n()
5353

5454
return (
55-
<MDXLayout navItems={navItems} frontmatter={frontmatter(currentLocale)} outline={outline}>
55+
<MDXLayout
56+
pagePath="[locale]/index.tsx"
57+
navItems={navItems}
58+
frontmatter={frontmatter(currentLocale)}
59+
outline={outline}
60+
>
5661
<Paragraph>{translations.index.intro}</Paragraph>
5762
<ul
5863
sx={{

0 commit comments

Comments
 (0)