---
title: "defineTree"
description: "Define hierarchical, ordered content backed by nested folders."
canonical: "https://www.qino.works/docs/api/tree"
---

# defineTree

> Define hierarchical, ordered content backed by nested folders.

Defines a tree: a nested directory where every node is a file and sections are a file plus a same-named folder. Returns getters for structure, content, and navigation.

```ts title="qino/trees/docs.ts"
import { z } from "zod";

import qino from "../";

export const docsTree = qino.defineTree({
  directory: "docs",
  extension: ".md",
  titleField: "title",
  schema: z.object({
    title: z.string(),
  }),
});
```

## Signature

```ts title="@qino/cms"
qino.defineTree(params: {
  directory: `/${string}`;
  extension: ".md" | ".mdx" | ".markdown" | ".json";
  schema: StandardSchemaV1<unknown, Record<string, unknown>>;
  titleField: StringKeyOfSchemaOutput;
  orderFileName?: string;
  relations?: Record<RelationPath, Primitive | (() => Primitive)>;
  media?: Record<StringPath, MediaRule>;
  markdown?: {
    stats?: boolean;
    toc?: boolean | { upTo?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; slug?: (text: string) => string };
    media?: MediaRule | Array<MediaRule>;
  };
  views?: (view: ViewHelper) => Record<string, View> & { default: View };
  pages?: { index: `/${string}`; entry: `/${string}{slug}${string}`; title: string; llmsFull?: boolean; markdownComponents?: Record<string, PageMarkdownComponent<Entry>> };
  cloud?: { displayAs?: string; displayEntryAs?: `${string}{${RequiredField}}${string}`; saveEntryAs?: `${string}{${RequiredField}}${string}`; searchableBy?: Array<Field>; hiddenFields?: Array<OptionalField>; media?: { saveFileAs?: string } };
}): Tree;

type MediaRule = { accepted: MimeType; maxSize: Size; parse?: (value: string, ctx: MediaParseContext) => string | undefined };
```

## Parameters

| Name            | Type                                                                                  | Required | Default         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| --------------- | ------------------------------------------------------------------------------------- | -------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `directory`     | `` `/${string}` ``                                                                    | yes      |                 | Directory under `contentFolder`. Walked recursively. Owned exclusively by this tree                                                                                                                                                                                                                                                                                                                                                                                                 |
| `extension`     | `".md" \| ".mdx" \| ".markdown" \| ".json"`                                           | yes      |                 | Only files with this extension are nodes                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `schema`        | Standard Schema object validator                                                      | yes      |                 | Synchronous. May not declare `_meta`, nor `markdown`/`_stats`/`_toc` for Markdown                                                                                                                                                                                                                                                                                                                                                                                                   |
| `titleField`    | key of schema output                                                                  | yes      |                 | Field whose string value titles each node. Non-string keys are a type error                                                                                                                                                                                                                                                                                                                                                                                                         |
| `orderFileName` | `string`                                                                              | no       | `"_order.json"` | Per-folder ordering file                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `relations`     | `Record<path, primitive \| () => primitive>`                                          | no       | `{}`            | Same grammar as collections                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `media`         | `Record<path, MediaRule>`                                                             | no       | `{}`            | Media fields, keyed by path to a string field, with the one MIME type each accepts and its max size. Image fields are read as `QinoImage`, and `parse` reads a value first. See [Media](https://www.qino.works/docs/concepts/media)                                                                                                                                                                                                                                                                       |
| `markdown`      | `{ stats?; toc?; media? }`                                                            | no       | `{}`            | Configures the body. Markdown and MDX only. `stats` adds `_stats`, its word and character counts. See [MarkdownStats](https://www.qino.works/docs/api/types/markdown-stats). `toc` adds `_toc`, its headings with their anchor ids: `true` lists every level, `upTo` stops at one, and `slug` sets how ids are made. See [MarkdownToc](https://www.qino.works/docs/api/types/markdown-toc). `media` lists the rules body images must match, each read with its own `parse`. See [Body images](https://www.qino.works/docs/concepts/media#body-images) |
| `views`         | `(view) => { default: View; [name]: View }`                                           | no       |                 | Helper offers `resolveDepth` and `augment` only. `filter` and `sort` are rejected                                                                                                                                                                                                                                                                                                                                                                                                   |
| `pages`         | `{ index; entry; title; llmsFull?; markdownComponents? }`                             | no       |                 | Where the index page and entry pages live, for page Markdown, Markdown URLs and `llms.txt`, and how MDX components read in the pages' Markdown. A slug keeps its slashes, so `guide/cli` fills `/docs/{slug}` as `/docs/guide/cli`. Requires `siteUrl` on `initQino`. Same fields and rules as [collections](https://www.qino.works/docs/api/collection#pages)                                                                                                                                            |
| `cloud`         | `{ displayAs?; displayEntryAs?; saveEntryAs?; searchableBy?; hiddenFields?; media? }` | no       |                 | Cloud UI names, entry labels (default: `titleField`) and naming, search, hidden fields, and where uploads are saved. Requires `cloud` on `initQino`. See [Cloud](https://www.qino.works/docs/guide/cloud#primitives)                                                                                                                                                                                                                                                                                      |

## Returns

A `Tree` with nine getters.

### Getters

- [`getTree(slug?)`](https://www.qino.works/docs/api/tree/get-tree)
- [`getFlatTree()`](https://www.qino.works/docs/api/tree/get-flat-tree)
- [`getEntries(options?)`](https://www.qino.works/docs/api/tree/get-entries)
- [`getEntry(slug, options?)`](https://www.qino.works/docs/api/tree/get-entry)
- [`getAncestorNodes(slug)`](https://www.qino.works/docs/api/tree/get-ancestor-nodes)
- [`getNextNode(slug)`](https://www.qino.works/docs/api/tree/get-next-node)
- [`getPreviousNode(slug)`](https://www.qino.works/docs/api/tree/get-previous-node)
- [`getPageMarkdown(slug?)`](https://www.qino.works/docs/api/tree/get-page-markdown)
- [`getMarkdownUrl(slug?)`](https://www.qino.works/docs/api/tree/get-markdown-url)

## Throws

At definition time, the shared view errors listed on [defineCollection](https://www.qino.works/docs/api/collection), plus:

- `tree views do not support filter or sort.`
- `tree view "<name>" does not support filter or sort.`
- ``"<id>" sets `cloud`, but initQino doesn't. Add `cloud` to initQino.``
- The `pages` errors listed on [defineCollection](https://www.qino.works/docs/api/collection#throws), naming `Tree "<directory>"`.

Layout errors surface when a getter walks the tree. See [getTree](https://www.qino.works/docs/api/tree/get-tree).

## See also

- [Trees](https://www.qino.works/docs/concepts/trees)
- [Content folder](https://www.qino.works/docs/guide/content-folder)
- [Agent-ready content](https://www.qino.works/docs/guide/agent-ready-content)
