# The layout language

> The exact grammar of a layout template, what each part draws, the errors the parser gives, the five starting layouts, and how to use the parser from code.

Source: https://crawl-docs.pdhq.win/advanced/layout-language/
Last updated: 2026-12-02

A layout is a template that says where the parts of a saved web **article** go. It is a small BBCode-like language, parsed and drawn by `packages/render/src/layout.ts`. It changes how an article is shown, never what was saved. For the user view see [Layouts](https://crawl-docs.pdhq.win/guide/layouts.md).

## Grammar

Everything between `[` and `]` is a tag. Everything else is text. Blank lines separate paragraphs of text.

| Tag | Meaning | Closes with |
| --- | --- | --- |
| `[title]` `[author]` `[avatar]` `[date]` `[site]` `[reading]` `[original]` `[saved]` | One fact of the post. Drawn only when the post has the data (`[saved]` is always drawn). | none |
| `[body]` | Every block of the article that no `[part]` placed. | none |
| `[part "Heading"]` | The heading with that name and the blocks under it, up to the next heading of the same or a higher level. The name is in double quotes, 1 to 80 characters, matched without regard to letter case. | none |
| `[rule]` | A divider line. | none |
| `[columns N]` | `N` is 2 or 3. Columns stack on a phone. | `[/columns]` |
| `[next]` | Moves to the next column. Only inside `[columns]`. | none |
| `[box tone=T]` | A framed note. `T` is `info`, `warn` or `note` (the default). | `[/box]` |
| plain text | A paragraph. | none |

Tag names are not case sensitive. A layout is at most **20,000** characters (`LAYOUT_SOURCE_MAX`). Columns and boxes can sit inside each other up to **4** levels (`MAX_DEPTH`). A part that the post does not have draws nothing, and a column or box with nothing in it is left out.

## What each fact draws

| Tag | Output |
| --- | --- |
| `[title]` | The title as a first-level heading. |
| `[author]` | The author's name. |
| `[avatar]` | The author's picture. |
| `[date]`, `[site]`, `[reading]` | The date, the site's name, the reading time such as "4 min read". |
| `[original]` | A link to the original page, shown as its address (only `http` and `https`). |
| `[saved]` | When it was saved. |

All text is escaped. The only output is a fixed set of classes (`lay-text`, `lay-title`, `lay-field`, `lay-body`, `lay-part`, `lay-rule`, `lay-columns`, `lay-col`, `lay-box`) drawn by the stylesheet `LAYOUT_CSS`, which is part of the page, so a saved page needs no build step.

## Errors

The parser collects every error with its line number and refuses the template (a saved page whose layout cannot be read falls back to the normal page):

| Message | Cause |
| --- | --- |
| `A [ has no closing ].` | A tag was not closed. |
| `A part needs the heading name in quotes: [part "Ingredients"].` | `[part]` without a quoted name. |
| `Columns are 2 or 3: [columns 2].` | A wrong number. |
| `[next] only works inside [columns].` and `There are more [next] than columns.` | A misplaced `[next]`. |
| `A box tone is info, warn, note: [box tone=info].` | An unknown tone. |
| `[/columns] has no matching [columns N].` and `[/box] has no matching [box].` | A closing tag with no opening one. |
| `[columns] is never closed with [/columns].` and `[box] is never closed with [/box].` | An opening tag with no closing one. |
| `Too many columns and boxes inside each other.` | More than 4 levels. |
| `I do not know [x].` | An unknown tag. |
| `A layout can be 20000 characters at most.` | Too long. |

## Which layout is used

A layout belongs to a **site** (a lower-case host without `www.`). For a post, `pickLayout(url, layouts)` takes the layout whose site equals the post's host or is its parent domain, and the **longest** such site wins. A layout for `docs.example.com` beats one for `example.com`. The API allows 100 layouts and one per site.

## The starting layouts

`STARTER_LAYOUTS` holds five: **Article**, **Docs**, **Product**, **Recipe** and **Thread**. They are plain templates you can read in `layout.ts` and copy from Settings → Layouts.

## From code

```ts
import { parseLayout, renderLayout, pickLayout } from "@social-crawl/render";

const parsed = parseLayout(source);
if (!parsed.ok) throw new Error(parsed.errors.map((e) => `line ${e.line}: ${e.message}`).join("\n"));
const html = renderLayout(parsed.nodes, post.article.blocks, parts); // `parts` is what the page renderer supplies
```

`renderPostHtml(post, { layout: source })` does all of it for a saved article, and ignores the layout for any other kind of post.

## Where the API uses it

- `GET /layouts`, `POST /layouts`, `PUT /layouts/:id` and `DELETE /layouts/:id` manage them, and the API checks each template with `parseLayout` before saving.
- `bookmarks/:id/files/layout.html` draws a post's page with its layout, on demand.
- The saved `index.html` and every ZIP of an article use it. The stored `index.html` of an older post is only redrawn when the post is saved again.
