feat(farmjs): render themes like the Next.js layout (shell layout/page, generated titles, hidden folders)

KI
1mo ago
+343-69

Problem

a theme that renders the same on next.js can't be reproduced on farm. trying the assistant-ui docs theme (assistant-ui/assistant-ui#9741, built for @farming-labs/next) on a 376-page farm port showed four gaps, each a place where the farm adapter does something different from the next.js layout:

  1. no layout composition. on next, a theme wraps createDocsLayout with its own header and passes page controls as the layout's children. farm renders the layout inside its own renderer, so there was nowhere to put either inside the providers (the existing shell wraps outside them, so search/tree/toc contexts aren't available).
  2. no generated page title. next renders the frontmatter title (and the title-row controls themes attach to .fd-generated-page-header) for shadcn-based themes when the markdown has no h1. farm never did, so pages without # title had no heading at all.
  3. hidden folder index. next names a folder from a hidden: true index without linking it, and drops a hidden page with no children. farm linked to the hidden page.
  4. "Introduction" folders. with a shadcn-based theme next names a folder whose index is titled "Introduction" after its slug (e.g. "React Native"); farm showed "Introduction".

Change

  • the docs shell module (withDocs(config, { shell })) may also export layout and page. the renderer wraps the docs layout with layout inside the providers, and each page's content with page inside the layout, the same places next puts a theme's header and page controls. without them nothing changes.
  • load() returns generatedTitle with next's rule (hasAuthoredPageTitle, copied rather than imported so older theme/core versions keep working); the renderer passes it to the layout, and DocsPageClient/TanstackDocsLayout accept it per page.
  • both nav builders (filesystem and bundled) honor hidden: true like next.
  • the loaded tree names "Introduction" folders like next for shadcn-based themes (scanned trees only, as on next).

Safety and compatibility

  • all opt-in or matching existing next behavior; apps without a shell layout/page, without shadcn-based themes, or without hidden pages render as before.
  • generatedTitle only applies when the markdown has no title, so pages with # title are unchanged.
  • next and tanstack adapters are untouched apart from the new optional generatedTitle prop on the shared layout.

Verification

  • packages/farmjs 141 tests (new: wrapper placement and absence, generated title rules incl. fenced # and setext, hidden index in both builders, introduction folder naming), packages/fumadocs 286, packages/next 181, packages/tanstack-start 4; typecheck clean for farmjs and theme; oxlint/oxfmt clean.
  • assistant-ui docs (376 pages) with the shared theme on farm (this branch, plus #621 and #625) vs the same content, config and theme on next 16.4 + @farming-labs/next 0.2.127: screenshots at 1440x900 light/dark and 390x844 for three pages, viewport and full page, are pixel-identical for architecture (0.00% in all six) and thread (<=0.01%, same height); installation differs only inside an animated gif frame. sidebars match entry for entry (377/377) after these fixes; before them farm had 379 entries in a different order and no title row controls.
  • no console errors or hydration warnings on either.

Documentation and release

the shell option's docs comment covers layout/page. ships with the next release.


Summary by cubic

Makes farm render themes the way the Next.js docs layout does, so a theme built for @farming-labs/next reproduces identically on farm without theme changes.

Changes

  • The docs shell may now export layout and page; the renderer wraps the docs layout and each page's content with them inside the providers, where Next.js places a theme's header and page controls.
  • load() returns generatedTitle for shadcn-based themes when the Markdown authors no title (#, setext, or <h1> outside code fences), and the renderer passes it to the layout.
  • Both nav builders honor hidden: true like Next.js: a hidden index names its folder without linking to it, and hidden pages are dropped.
  • For shadcn-based themes, folders whose index is titled "Introduction" are named after their slug, as on Next.js.

Compatibility

  • All changes are opt-in or match existing Next.js behavior; apps without a shell layout/page, without shadcn-based themes, or without hidden pages render as before.
  • Next.js and TanStack adapters gain only the optional generatedTitle prop.