# Visuals in markdown fields

Source: https://codeherder.com/docs/visuals/

Pick a diagram or block for a plan, design or comment, and keep it within its limits.

A markdown field can carry a Mermaid diagram and seven CodeHerder blocks. The web app draws each one. `ch`, the TUI and git show the plain source, and it stays readable.

## Pick a visual

| You want to show | Use |
| --- | --- |
| A data model | Mermaid `erDiagram` |
| A record’s lifecycle | Mermaid `stateDiagram-v2` |
| Components and flow | Mermaid `flowchart` |
| Who calls whom | Mermaid `sequenceDiagram` |
| A hierarchy | `tree` |
| A plan of parallel work | `steps` |
| A schema change | `table-change` |
| A payload change | `data-shape` |
| A few numbers | `bars` |
| An example rule | `rule` |
| A screen change | `sketch` |

## Shape a plan

Lead with two or three sentences that give the result. Then add one or two visuals. Put the long detail below them.

- Keep each visual under its limits. The table at the end of this page lists them.
- Give each visual a title. A Mermaid diagram uses an `accTitle:` line. Every other block uses a `title:` first line.
- `ch` and the API check each visual on write. A bad visual fails with `invalid_input`. The message names the block, the line and the limit.
- To show Mermaid source as code, use a `text` fence. A `text` fence is not a visual.

## Mermaid

Use Mermaid for flows, calls, data models and lifecycles. Use one of four diagram types: `flowchart` (or `graph`), `sequenceDiagram`, `erDiagram` and `stateDiagram-v2` (or `stateDiagram`). Do not use directives, `click` lines or HTML.

```
flowchart LR
  accTitle: Where a write is checked
  Agent["Agent"] --> API["API write"]
  API --> Check["Check visuals"]
  Check --> DB[(Stored markdown)]
```

```
sequenceDiagram
  accTitle: How ch saves a plan
  participant CH as ch
  participant API as API
  CH->>API: Send the plan
  API-->>CH: Accept or refuse
```

```
erDiagram
  accTitle: A task and its comments
  TASK ||--o{ COMMENT : has
  TASK {
    uuid id
    text title
  }
```

```
stateDiagram-v2
  accTitle: A task's lifecycle
  [*] --> Todo
  Todo --> Doing
  Doing --> Done
  Done --> [*]
```

Limits: a diagram has at most the number of boxes, arrows and lines in the table below. A box is a node, entity, state or participant.

## Tree

Use `tree` to show a hierarchy, such as packages or a folder layout. Write one node per line. Indent each level with two spaces.

```
title: Where the code lives
internal
  visualblock
    check.go
    limits.go
support
  visuals.md
```

Limits: a tree has a node limit and a depth limit.

## Steps

Use `steps` to show a plan of parallel work. Each numbered line is a column. Each `-` line below it is an item in that column.

```
title: Order of the work
1. Support doc
- Write the pick table
- Generate the limits table
2. Pointers
- Brief line
- Planning prompts
```

Limits: `steps` has a column limit and an item limit for each column.

## Table change

Use `table-change` to show a schema change. Name the table. Then list each column change: `+` adds, `~` changes and `-` drops.

```
title: Add a visual count to comments
table: comments
+ visual_count int: Number of visuals in the body
~ body text -> text: Now checked for visuals
- legacy_flag: No longer read
```

Limits: a `table-change` has a row limit.

## Data shape

Use `data-shape` to show a payload change. Give `before` (optional) and `after` as single-line JSON. Then list a note for each field that needs one.

```
title: A comment payload gains a count
before: {"body": "text"}
after: {"body": "text", "visuals": 2}
fields:
visuals: How many visual blocks the body holds
```

Limits: `data-shape` has a payload line limit and a field note limit.

## Bars

Use `bars` to compare a few numbers. Each line is a label and a number. A number is 0 or more. The `unit:` line is optional.

```
title: Visuals by kind
unit: blocks
Mermaid: 12
Steps: 8
Tree: 3
```

Limits: `bars` has a limit on the number of bars.

## Rule

Use `rule` to show an example of a rule. Use `Given`, `When` and `Then` in that order. You can add `And` and `But` lines.

```
title: A bad visual is refused
Given a comment body with a Mermaid diagram
And the diagram has no title
When ch saves the comment
Then the API returns invalid_input
```

Limits: a `rule` has a line limit.

## Sketch

Use `sketch` to show a screen change. Give a `before:` section and an `after:` section. Each line is `part: text`. Indent a line by two spaces to nest it in the part above.

```
title: A plan field gains a summary
before:
  header: Plan
  text: Long detail only
after:
  header: Plan
  note: Short summary first
  text: Long detail below
```

The parts are `header`, `text`, `button`, `input`, `select`, `list`, `table`, `tabs`, `card`, `note`, `image` and `row`. Limits: a `sketch` has a limit on the number of parts.

## Limits

Each label, item or caption holds a limited number of words. The table lists every limit.

| Limit | Value |
| --- | --- |
| Words in a title | 12 |
| Words in a label, item or caption | 10 |
| Words in a table-change meaning or data-shape field note | 20 |
| Mermaid boxes (nodes, entities, states or participants) | 20 |
| Mermaid arrows | 30 |
| Mermaid lines | 80 |
| Tree nodes | 30 |
| Tree depth | 5 |
| Steps columns | 6 |
| Steps items in a column | 6 |
| Table-change rows | 15 |
| Data-shape payload lines | 40 |
| Data-shape field notes | 15 |
| Bars | 12 |
| Words in a bar label | 5 |
| Rule lines | 10 |
| Words in a rule line | 20 |
| Sketch parts | 15 |
