Files
supabase/apps/learn/app/(app)/[...slug]/page.tsx
Anthony Lio 8bcbe3dcc7 feat(docs): extracts code block component (#51661)
## Problem

new surfaces display code blocks their own way while docs has the full
implementation but it lives inside `apps/docs`


## Solution

- moves the docs code block parts to `ui-patterns/CodeBlock` as separate
imports
- kb code blocks now show line numbers, which matches docs

| state | preview |
| -------|------|
| before | <img width="913" height="509" alt="image"
src="https://github.com/user-attachments/assets/76ff0096-433a-4b06-9eca-1d79c4940e24"
/> |
| after | <img width="913" height="509" alt="image"
src="https://github.com/user-attachments/assets/4f5d91d9-13df-44a0-9e65-edf605773c5f"
/> |

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

1. visit /[docs/guides/database/tables#enabling-row-level-security
](https://docs-git-feat-shared-code-block-parts-supabase.vercel.app/docs/guides/database/tables#enabling-row-level-security)code
blocks, line numbers, colors in light and dark mode, copy, and word wrap
match
2. visit
[/docs/guides/auth/passwords](https://docs-git-feat-shared-code-block-parts-supabase.vercel.app/docs/guides/auth/passwords)
hover over a typed identifier tooltip should still show
3. visit a kb guide with code, for example
[/kb/guides/migrate-from-mysql](https://kb-git-feat-shared-code-block-parts-supabase.vercel.app/kb/guides/migrate-from-mysql)
4. visit the [design system code block
page](https://design-system-git-feat-shared-code-block-parts-supabase.vercel.app/design-system/docs/fragments/code-block)

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-22 11:46:38 +03:00

158 lines
5.1 KiB
TypeScript

import { metadata as mainMetadata } from '@/app/layout'
import { ChapterCompletion } from '@/components/chapter-completion'
import { CourseHero } from '@/components/course-hero'
import { ExploreMore } from '@/components/explore-more'
import { Mdx } from '@/components/mdx-components'
import { NextUp } from '@/components/next-up'
import { DashboardTableOfContents } from '@/components/toc'
import { getCurrentChapter } from '@/lib/get-current-chapter'
import { getNextPage } from '@/lib/get-next-page'
import { getTableOfContents } from '@/lib/toc'
import { absoluteUrl, cn } from '@/lib/utils'
import 'config/code-block-variables.css'
import '@/styles/mdx.css'
import { ChevronRight } from 'lucide-react'
import type { Metadata } from 'next'
import { notFound } from 'next/navigation'
import Balancer from 'react-wrap-balancer'
import { ScrollArea } from 'ui'
import { allDocs } from '@/.velite'
interface DocPageProps {
params: Promise<{
slug: string[]
}>
}
async function getDocFromParams({ params }: { params: { slug: string[] } }) {
const slug = params.slug?.join('/') || ''
const doc = allDocs.find((doc) => doc.slugAsParams === slug)
if (!doc) {
return null
}
return doc
}
export async function generateMetadata(props: DocPageProps): Promise<Metadata> {
const params = await props.params
const doc = await getDocFromParams({ params })
// get page params so we can check if it's the introduction page
const slugSegments = doc?.slugAsParams.split('/')
const isIntroductionPage = slugSegments?.[slugSegments.length - 1] === 'introduction'
if (!doc) {
return {}
}
const metadata: Metadata = {
...mainMetadata,
title: doc.title,
description: doc.description,
openGraph: {
...(mainMetadata.openGraph || {}),
title: doc.title,
description: doc.description,
type: 'article',
url: absoluteUrl(doc.slug),
},
}
return metadata
}
export async function generateStaticParams(): Promise<{ slug: string[] }[]> {
return allDocs.map((doc) => ({
slug: doc.slugAsParams.split('/'),
}))
}
export default async function DocPage(props: DocPageProps) {
const params = await props.params
const doc = await getDocFromParams({ params })
if (!doc) {
notFound()
}
const toc = await getTableOfContents(doc.raw)
const nextPage = getNextPage(doc.slugAsParams)
const currentChapter = getCurrentChapter(doc.slugAsParams)
const slugSegments = doc.slugAsParams.split('/')
const isIntroductionPage = slugSegments[slugSegments.length - 1] === 'introduction'
const exploreItems = doc.explore
return (
<div className={cn('relative')}>
{isIntroductionPage && doc.courseHero && (
<CourseHero
title={doc.courseHero.title}
subtitle={doc.courseHero.subtitle}
description={doc.courseHero.description}
//instructors={doc.courseHero.instructors}
/>
)}
<div
className={cn(
'relative lg:gap-10 xl:grid xl:grid-cols-[1fr_200px] py-20',
isIntroductionPage ? 'px-0' : 'px-8 md:px-16'
)}
>
<div className="mx-auto w-full min-w-0 max-w-4xl">
<div className="mb-4 flex items-center space-x-1 text-sm text-foreground-muted">
<div className="overflow-hidden text-ellipsis whitespace-nowrap">Learn</div>
<ChevronRight className="h-4 w-4 text-foreground-muted" />
<div className="text-foreground-lighter">{doc.title}</div>
</div>
<div className="flex flex-col lg:flex-row lg:items-end justify-between mb-5">
<div className="space-y-2">
<h1 className={cn('scroll-m-20 text-2xl lg:text-4xl tracking-tight')}>{doc.title}</h1>
{doc.description && (
<p className="text-base lg:text-lg text-foreground-light">
<Balancer>{doc.description}</Balancer>
</p>
)}
</div>
</div>
<div className="pb-12">
<Mdx code={doc.code} />
</div>
{exploreItems && exploreItems.length > 0 && <ExploreMore items={exploreItems} />}
{currentChapter && nextPage && (
<ChapterCompletion
chapterNumber={currentChapter.chapterNumber!}
completionMessage={currentChapter.completionMessage}
/>
)}
{nextPage && (
<div className="flex w-2xl grow">
<NextUp
title={nextPage.title}
description={nextPage.description || ''}
href={nextPage.href}
chapterNumber={nextPage.chapterNumber}
/>
</div>
)}
</div>
{doc.toc && (
<div className="hidden text-sm xl:block">
<div className="sticky top-16 -mt-10 pt-4">
<ScrollArea className="pb-10">
<div className="sticky top-16 -mt-10 h-[calc(100vh-3.5rem)] py-12">
<DashboardTableOfContents toc={toc} />
</div>
</ScrollArea>
</div>
</div>
)}
</div>
</div>
)
}