Files
Ivan Vasilov c6cdf4bd53 Migrate off contentlayer2 to Velite (design-system, ui-library, learn) (#48546)
## Summary
- `contentlayer2@0.4.6` is unmaintained and drags in a heavy, stale
dependency graph (esbuild pinned to 0.17–0.20, mdx-bundler, old
`@opentelemetry/core`) that was the recurring source of vuln bumps.
- Migrates all three apps that used it — `design-system`, `ui-library`,
`learn` — to [Velite](https://velite.js.org), preserving the generated
typed `allDocs`/`Doc` collection and the `body.code` + `useMDXComponent`
runtime via a small shared local hook.
- Same MDX pipeline (remark-gfm, remark-code-import, rehype-slug,
rehype-pretty-code w/ Shiki compat + local theme,
rehype-autolink-headings, custom
`__rawString__`/`__src__`/`__event__`/`__style__` visitors) ported 1:1
into each app's `velite.config.js`.
- `learn`'s extra frontmatter fields (`chapterNumber`, `explore`,
`courseHero`) are now backed by real Velite/Zod schema types, so the
`(doc as any)` casts in `get-next-page.ts` / `get-current-chapter.ts` /
the doc page could be dropped.
- `next.config.mjs` no longer wraps with `withContentlayer`; since
Velite has no Next.js webpack-plugin equivalent, each app's `dev` script
now runs `velite dev` and `next dev` in parallel via `npm-run-all`.

Ref:
[FE-3861](https://linear.app/supabase/issue/FE-3861/migrate-off-contentlayer2-learn-ui-library-design-system-to-shed)

## Test plan
- [x] `pnpm build:content` (Velite build) succeeds for all three apps
- [x] `pnpm typecheck` passes for all three apps
- [ ] Manual smoke test of `pnpm dev` for each app in a browser (docs
pages render, TOC, copy-button, code highlighting)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Improvements**
* Improved content generation across documentation, learning materials,
and the UI library for more consistent pages.
* Preserved MDX rendering, navigation, table of contents, course
metadata, source previews, and component examples.
* Improved consistency when displaying documentation and interactive
examples.
* Improved application loading by optimizing how interface components
are delivered.
* **Chores**
* Streamlined content compilation and development workflows across the
design system, learning area, and UI library.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-04 15:49:44 +02:00

98 lines
3.1 KiB
TypeScript

import { ChevronLeft, ChevronRight } from 'lucide-react'
import Link from 'next/link'
import { Doc } from '@/.velite'
import { docsConfig } from '@/config/docs'
import { cn } from '@/lib/utils'
import { NavItem, NavItemWithChildren } from '@/types/nav'
interface DocsPagerProps {
doc: Doc
}
export function DocsPager({ doc }: DocsPagerProps) {
const pager = getPagerForDoc(doc)
if (!pager) {
return null
}
const hasBoth = pager?.prev?.href && pager?.next?.href
return (
<div className={cn('flex flex-row items-center', hasBoth ? 'justify-between' : 'justify-end')}>
{pager?.prev?.href && (
<Link
href={pager.prev.href}
className="flex gap-3 text-foreground-light hover:text-foreground group"
>
<ChevronLeft className="mr-2 h-4 w-4 self-end mb-1 text-foreground-muted group-hover:text-foreground-lighter" />
<div className="flex flex-col gap-1">
<span className="text-xs font-mono uppercase tracking-wider text-foreground-muted group-hover:text-foreground-lighter">
Previous
</span>
{pager.prev.title}
</div>
</Link>
)}
{pager?.next?.href && (
<Link
href={pager.next.href}
className="flex gap-3 text-foreground-light hover:text-foreground group"
>
<div className="flex flex-col gap-1">
<span className="text-xs font-mono uppercase tracking-wider text-foreground-muted group-hover:text-foreground-lighter">
Next
</span>
{pager.next.title}
</div>
<ChevronRight className="ml-2 h-4 w-4 self-end mb-1 text-foreground-muted group-hover:text-foreground-lighter" />
</Link>
)}
</div>
)
}
export function getPagerForDoc(doc: Doc) {
const flattenedLinks = [null, ...flatten(docsConfig.sidebarNav), null]
const activeIndex = flattenedLinks.findIndex((link) => doc.slug === link?.href)
const prev = activeIndex !== 0 ? flattenedLinks[activeIndex - 1] : null
const next = activeIndex !== flattenedLinks.length - 1 ? flattenedLinks[activeIndex + 1] : null
return {
prev,
next,
}
}
export function flatten(links: NavItemWithChildren[]): NavItem[] {
return links
.reduce<NavItem[]>((flat, link) => {
return flat.concat(link.items?.length ? flatten(link.items) : link)
}, [])
.filter((link) => !link?.disabled)
}
export interface BreadcrumbSegment {
title: string
href?: string
}
export function getBreadcrumbSegments(doc: Doc): BreadcrumbSegment[] {
const segments: BreadcrumbSegment[] = [{ title: 'Docs', href: '/docs' }]
for (const section of docsConfig.sidebarNav) {
const flatItems = flatten(section.items ?? [])
const inSection = flatItems.some((item) => item.href === doc.slug)
if (!inSection || !section.title) continue
const sectionLanding = flatItems.find((item) => item.priority) ?? flatItems[0]
const isOnLanding = sectionLanding?.href === doc.slug
if (sectionLanding?.href && !isOnLanding) {
segments.push({ title: section.title, href: sectionLanding.href })
}
break
}
return segments
}