Skip to content

Fix layout component logic #15451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions app/[locale]/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import mdComponents from "@/components/MdComponents"

import { dataLoader } from "@/lib/utils/data/dataLoader"
import { dateToString } from "@/lib/utils/date"
import { getLayoutFromSlug } from "@/lib/utils/layout"
import { getPostSlugs } from "@/lib/utils/md"
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"

Expand All @@ -19,18 +20,6 @@ import { getMdMetadata } from "@/lib/md/metadata"

const loadData = dataLoader([["gfissues", fetchGFIs]])

function getLayoutFromSlug(slug: string) {
if (slug.includes("developers/docs")) {
return "docs"
}

if (slug.includes("developers/tutorials")) {
return "tutorial"
}

return "static"
}

export default async function Page({
params,
}: {
Expand Down Expand Up @@ -69,7 +58,8 @@ export default async function Page({
slug,
// TODO: Address component typing error here (flip `FC` types to prop object types)
// @ts-expect-error Incompatible component function signatures
components: { ...mdComponents, ...componentsMapping },
baseComponents: mdComponents,
componentsMapping,
scope: {
gfissues,
},
Expand Down
10 changes: 1 addition & 9 deletions src/components/Codeblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ const TopBarItem = ({

const codeTheme = {
light: {
plain: {
backgroundColor: "#f7f7f7", // background-highlight (gray-50)
color: "#6C24DF", // primary (purple-600)
},
styles: [
{
style: { color: "#6c6783" },
Expand Down Expand Up @@ -113,10 +109,6 @@ const codeTheme = {
},
dark: {
// Pulled from `defaultProps.theme` for potential customization
plain: {
backgroundColor: "#121212", // background-highlight (gray-900)
color: "#B38DF0", // primary (purple-400)
},
styles: [
{
style: { color: "#6c6783" },
Expand Down Expand Up @@ -253,7 +245,7 @@ const Codeblock = ({
/* Context: https://github.com/ethereum/ethereum-org-website/issues/6202 */
<div className={cn("relative", className)} dir="ltr">
<div
className="overflow-scroll rounded"
className="overflow-scroll rounded bg-background-highlight text-primary"
style={{
maxHeight: isCollapsed
? `calc((1.2rem * ${LINES_BEFORE_COLLAPSABLE}) + 4.185rem)`
Expand Down
14 changes: 10 additions & 4 deletions src/layouts/Docs.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MDXRemoteProps } from "next-mdx-remote"
import type { HTMLAttributes } from "react"

import { ChildOnlyProp } from "@/lib/types"
Expand Down Expand Up @@ -34,8 +35,7 @@ import YouTube from "@/components/YouTube"
import { cn } from "@/lib/utils/cn"
import { getEditPath } from "@/lib/utils/editPath"

const baseHeadingClasses =
"font-mono uppercase font-bold scroll-mt-40 break-words"
const baseHeadingClasses = "font-bold scroll-mt-40 break-words"

const H1 = (props: HTMLAttributes<HTMLHeadingElement>) => (
<MdHeading1
Expand Down Expand Up @@ -72,12 +72,18 @@ const BackToTop = (props: ChildOnlyProp) => (
</div>
)

const Pre = (props: React.HTMLAttributes<HTMLDivElement>) => {
const match = props.className?.match(/(language-\S+)/)
const codeLanguage = match ? match[0] : "plain-text"
return <Codeblock codeLanguage={codeLanguage} {...props} />
}

export const docsComponents = {
h1: H1,
h2: H2,
h3: H3,
h4: H4,
pre: Codeblock,
pre: Pre,
...mdxTableComponents,
ButtonLink,
Card,
Expand All @@ -88,7 +94,7 @@ export const docsComponents = {
GlossaryTooltip,
InfoBanner,
YouTube,
}
} as MDXRemoteProps["components"]

type DocsLayoutProps = Pick<
MdPageContent,
Expand Down
22 changes: 12 additions & 10 deletions src/layouts/Tutorial.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MDXRemoteProps } from "next-mdx-remote"
import type { HTMLAttributes } from "react"

import type { ChildOnlyProp } from "@/lib/types"
Expand Down Expand Up @@ -28,17 +29,11 @@ import YouTube from "@/components/YouTube"
import { getEditPath } from "@/lib/utils/editPath"

const Heading1 = (props: HTMLAttributes<HTMLHeadingElement>) => (
<MdHeading1
className="font-monospace uppercase max-lg:text-[1.75rem]"
{...props}
/>
<MdHeading1 className="max-lg:text-[1.75rem]" {...props} />
)

const Heading2 = (props: HTMLAttributes<HTMLHeadingElement>) => (
<MdHeading2
className="mt-12 scroll-mt-40 font-monospace uppercase max-md:text-2xl"
{...props}
/>
<MdHeading2 className="mt-12 scroll-mt-40 max-md:text-2xl" {...props} />
)

const Heading3 = (props: HTMLAttributes<HTMLHeadingElement>) => (
Expand Down Expand Up @@ -66,6 +61,12 @@ const KBD = (props: HTMLAttributes<HTMLElement>) => (
/>
)

const Pre = (props: React.HTMLAttributes<HTMLDivElement>) => {
const match = props.className?.match(/(language-\S+)/)
const codeLanguage = match ? match[0] : "plain-text"
return <Codeblock codeLanguage={codeLanguage} {...props} />
}

export const tutorialsComponents = {
a: TooltipLink,
h1: Heading1,
Expand All @@ -74,7 +75,7 @@ export const tutorialsComponents = {
h4: Heading4,
p: Paragraph,
kbd: KBD,
pre: Codeblock,
pre: Pre,
...mdxTableComponents,
ButtonLink,
CallToContribute,
Expand All @@ -83,7 +84,8 @@ export const tutorialsComponents = {
EnvWarningBanner,
InfoBanner,
YouTube,
}
} as MDXRemoteProps["components"]

type TutorialLayoutProps = ChildOnlyProp &
Pick<
MdPageContent,
Expand Down
24 changes: 14 additions & 10 deletions src/layouts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { MDXRemoteProps } from "next-mdx-remote"

import { Layout } from "@/lib/types"

import { docsComponents, DocsLayout } from "./Docs"
import * as mdLayouts from "./md"
import { staticComponents, StaticLayout } from "./Static"
Expand All @@ -20,13 +24,13 @@ export const layoutMapping = {
tutorial: TutorialLayout,
}

export const componentsMapping = {
...staticComponents,
...mdLayouts.useCasesComponents,
...mdLayouts.stakingComponents,
...mdLayouts.roadmapComponents,
...mdLayouts.upgradeComponents,
...mdLayouts.translatathonComponents,
...docsComponents,
...tutorialsComponents,
} as const
export const componentsMapping: Record<Layout, MDXRemoteProps["components"]> = {
static: staticComponents,
"use-cases": mdLayouts.useCasesComponents,
staking: mdLayouts.stakingComponents,
roadmap: mdLayouts.roadmapComponents,
upgrade: mdLayouts.upgradeComponents,
translatathon: mdLayouts.translatathonComponents,
docs: docsComponents,
tutorial: tutorialsComponents,
}
14 changes: 13 additions & 1 deletion src/lib/md/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import remarkGfm from "remark-gfm"
import remarkHeadingId from "remark-heading-id"

import { CONTENT_DIR, CONTENT_PATH } from "../constants"
import { Frontmatter, TocNodeType } from "../types"
import { Frontmatter, Layout, TocNodeType } from "../types"

import rehypeImg from "@/lib/md/rehypeImg"
import remarkInferToc from "@/lib/md/remarkInferToc"
Expand Down Expand Up @@ -80,3 +80,15 @@ export const compile = async ({
tocNodeItems,
}
}

export const extractLayoutFromMarkdown = async (
markdown: string
): Promise<Layout | undefined> => {
const source = preprocessMarkdown(markdown)

const { frontmatter } = await compileMDX<Frontmatter>({
source,
options: { parseFrontmatter: true },
})
return frontmatter.template
}
23 changes: 18 additions & 5 deletions src/lib/md/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import {
import { getFileContributorInfo } from "@/lib/utils/contributors"
import { getLocaleTimestamp } from "@/lib/utils/time"

import { compile } from "./compile"
import { getLayoutFromSlug } from "../utils/layout"

import { compile, extractLayoutFromMarkdown } from "./compile"
import { importMd } from "./import"

const commitHistoryCache: CommitHistory = {}

interface GetPageDataParams {
locale: string
slug: string
components: MDXRemoteProps["components"]
baseComponents: MDXRemoteProps["components"]
componentsMapping: Record<Layout, MDXRemoteProps["components"]>
layout?: Layout
scope?: Record<string, unknown>
}
Expand All @@ -39,14 +42,26 @@ interface PageData {
export async function getPageData({
locale,
slug,
components,
baseComponents,
componentsMapping,
layout: layoutFromProps,
scope,
}: GetPageDataParams): Promise<PageData> {
const slugArray = slug.split("/")

// Import and compile markdown
const { markdown, isTranslated } = await importMd(locale, slug)
// Determine layout first to finalize list of components
const layout =
layoutFromProps ||
(await extractLayoutFromMarkdown(markdown)) ||
getLayoutFromSlug(slug)

const components: MDXRemoteProps["components"] = {
...baseComponents,
...(layout ? componentsMapping[layout] : {}),
}

const { content, frontmatter, tocNodeItems } = await compile({
markdown,
slugArray,
Expand All @@ -55,8 +70,6 @@ export async function getPageData({
scope,
})

const layout = layoutFromProps || frontmatter.template || "static"

// Process TOC items
const tocItems =
tocNodeItems.length === 1 && "items" in tocNodeItems[0]
Expand Down
5 changes: 5 additions & 0 deletions src/lib/utils/layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getLayoutFromSlug = (slug: string) => {
if (slug.includes("developers/docs")) return "docs"
if (slug.includes("developers/tutorials")) return "tutorial"
return "static"
}