Files
supabase/apps/learn/lib/get-next-page.ts
T
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

82 lines
2.4 KiB
TypeScript

import { allDocs } from '@/.velite'
import { courses } from '@/config/docs'
import { SidebarNavItem } from '@/types/nav'
/**
* Flattens the navigation structure to get all pages in order
* Only includes items that have an href (leaf nodes)
*/
function flattenNavItems(items: SidebarNavItem[]): SidebarNavItem[] {
const result: SidebarNavItem[] = []
for (const item of items) {
if (item.items && item.items.length > 0) {
// Recursively process children first
result.push(...flattenNavItems(item.items))
} else if (item.href) {
// Only add items with href (leaf nodes)
result.push(item)
}
}
return result
}
/**
* Normalizes a slug/href for comparison
*/
export function normalizeSlug(slug: string): string {
return slug.replace(/^\//, '').toLowerCase()
}
/**
* Gets the next page in the navigation structure based on the current page slug
*/
export function getNextPage(
currentSlug: string
): { title: string; href: string; description?: string; chapterNumber?: number } | null {
// Flatten all navigation items
const allPages = flattenNavItems(courses.items)
// Normalize the current slug for comparison
const normalizedCurrentSlug = normalizeSlug(currentSlug)
// Find the current page index
const currentIndex = allPages.findIndex((page) => {
if (!page.href) return false
const pageSlug = normalizeSlug(page.href)
return pageSlug === normalizedCurrentSlug
})
// If current page not found or it's the last page, return null
if (currentIndex === -1 || currentIndex === allPages.length - 1) {
return null
}
// Get the next page
const nextPage = allPages[currentIndex + 1]
if (!nextPage || !nextPage.href) {
return null
}
// Try to get description and chapterNumber from the doc
const nextPageSlug = normalizeSlug(nextPage.href)
const doc = allDocs.find((doc) => normalizeSlug(doc.slugAsParams) === nextPageSlug)
const description = doc?.description || nextPage.title
// Get chapterNumber from frontmatter first, then fallback to parsing from title
let chapterNumber: number | undefined = doc?.chapterNumber
if (!chapterNumber) {
// Try to extract chapter number from title (e.g., "2: CSS Styling" -> 2)
const chapterMatch = nextPage.title.match(/^(\d+):/)
chapterNumber = chapterMatch ? parseInt(chapterMatch[1], 10) : undefined
}
return {
title: nextPage.title,
href: nextPage.href,
description,
chapterNumber,
}
}