Skip to content

Commit 0fd1c2b

Browse files
authored
Make the current page’s “group” (if there is one) accessible to Algolia’s Crawler (#118)
1 parent a8cfa34 commit 0fd1c2b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

layout/MDXLayout.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
Table,
2929
} from '@/components'
3030
import { useI18n } from '@/i18n'
31+
import { NavItemGroup } from '@/navigation'
3132

3233
const mdxComponents = {
3334
blockquote: Blockquote,
@@ -75,13 +76,21 @@ export const MDXLayout = ({ pagePath, navItems, frontmatter, outline, children }
7576
const { pathWithoutLocale } = useI18n()
7677

7778
// Compute some values for the `NavContext`
78-
const { pageNavItems, previousPage, currentPage, nextPage } = useMemo(() => {
79+
const { previousPage, currentPage, nextPage, currentGroup } = useMemo(() => {
7980
let previousPage = null
8081
let currentPage = null
8182
let nextPage = null
83+
let currentGroup = null
8284
const pageNavItems = navItems.flatMap((navItem) => {
8385
if ('children' in navItem) {
84-
return navItem.children.filter((childNavItem) => 'path' in childNavItem)
86+
return navItem.children.filter((childNavItem) => {
87+
if ('path' in childNavItem) {
88+
if (childNavItem.path === pathWithoutLocale) {
89+
currentGroup = navItem
90+
}
91+
return true
92+
}
93+
})
8594
}
8695
if ('path' in navItem) {
8796
return [navItem]
@@ -97,7 +106,7 @@ export const MDXLayout = ({ pagePath, navItems, frontmatter, outline, children }
97106
}
98107
pageNavItemIndex++
99108
}
100-
return { pageNavItems, previousPage, currentPage, nextPage }
109+
return { previousPage, currentPage, nextPage, currentGroup: currentGroup as NavItemGroup | null }
101110
}, [navItems, pathWithoutLocale])
102111

103112
// Provide `markOutlineItem` to the `DocumentContext` so child `Heading` components can mark outline items as "in or above view" or not
@@ -129,7 +138,7 @@ export const MDXLayout = ({ pagePath, navItems, frontmatter, outline, children }
129138
}, [outline, outlineItemIsInOrAboveView])
130139

131140
return (
132-
<NavContext.Provider value={{ pagePath, navItems, pageNavItems, previousPage, currentPage, nextPage }}>
141+
<NavContext.Provider value={{ pagePath, navItems, previousPage, currentPage, nextPage, currentGroup }}>
133142
<DocumentContext.Provider value={{ frontmatter, outline, markOutlineItem, highlightedOutlineItemId }}>
134143
<NextSeo title={`${frontmatter?.title ? `${frontmatter.title} - ` : ''} The Graph Docs`} />
135144

@@ -155,7 +164,12 @@ export const MDXLayout = ({ pagePath, navItems, frontmatter, outline, children }
155164
</div>
156165

157166
<article className="graph-docs-content" sx={mdxStyles}>
158-
{frontmatter?.title && <Heading.H1>{frontmatter.title}</Heading.H1>}
167+
{currentGroup ? (
168+
<div className="graph-docs-current-group" sx={{ display: 'none' }}>
169+
{currentGroup.title}
170+
</div>
171+
) : null}
172+
{frontmatter?.title ? <Heading.H1>{frontmatter.title}</Heading.H1> : null}
159173
<MDXProvider components={mdxComponents}>{children}</MDXProvider>
160174
</article>
161175

layout/NavContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { createContext, Context } from 'react'
22

3-
import { NavItem, NavItemPage } from '@/navigation'
3+
import { NavItem, NavItemPage, NavItemGroup } from '@/navigation'
44

55
export type NavContextProps = {
66
pagePath: string
77
navItems: NavItem[]
8-
pageNavItems: NavItemPage[]
98
previousPage: NavItemPage | null
109
currentPage: NavItemPage | null
1110
nextPage: NavItemPage | null
11+
currentGroup: NavItemGroup | null
1212
}
1313

1414
export const NavContext = createContext(null) as Context<NavContextProps | null>

0 commit comments

Comments
 (0)