core
Data
Ingest an external tabular file (CSV, TSV, JSON, NDJSON) and render it as a table, chart, or datatable — shaped at build time
Source
import { createContentModelSchema } from '../lib/index.js';
import { preprocessDataTag } from '../data-pipeline.js';
export const data = createContentModelSchema({
attributes: {
src: {
type: String,
required: true,
description: 'Path to the source file, relative to the project root (sandboxed).',
},
format: {
type: String,
required: false,
matches: ['csv', 'tsv', 'json', 'ndjson'],
description: 'Source format. Inferred from the file extension; override for ambiguity.',
},
delimiter: {
type: String,
required: false,
description: 'Override the field separator (CSV/TSV).',
},
header: {
type: Boolean,
required: false,
description:
'CSV/TSV: whether the first row is the header (default true). false synthesizes col1…',
},
root: {
type: String,
required: false,
description: 'JSON: dotted path / JSON Pointer to the array or map within the document.',
},
orient: {
type: String,
required: false,
matches: ['records', 'values', 'index'],
description:
'JSON: how each element maps to a row. records/values auto-detected; index is explicit.',
},
'key-column': {
type: String,
required: false,
description: 'JSON: when orient=index, the header for the synthesized key column.',
},
columns: {
type: String,
required: false,
description: 'Select + order + rename: "name as Product, revenue as \'Revenue ($)\'".',
},
where: {
type: String,
required: false,
description: 'Filter rows with `field:value` clauses.',
},
sort: {
type: String,
required: false,
description: 'Sort by a column; "-" prefix for descending.',
},
limit: { type: Number, required: false, description: 'Maximum number of rows.' },
offset: { type: Number, required: false, description: 'Skip this many rows before limiting.' },
headers: {
type: String,
required: false,
description:
'Header labels for a `---`-delimited body, e.g. "Attribute, Type". Setting it makes the body one table ROW — cells separated by `---` — instead of a run of blocks, so cells can carry emphasis, links, runes and {% if %}. Requires a body.',
},
numeric: {
type: String,
required: false,
description: 'Comma-separated columns to force to numeric typing (emits data-value).',
},
text: {
type: String,
required: false,
description: 'Comma-separated columns to force to text typing.',
},
},
// Empty but required: `preprocess` replaces the body, and Markdoc rejects a tag with none.
contentModel: { type: 'sequence', fields: [] },
preprocess: preprocessDataTag,
transform(_resolved, _attrs) {
throw new Error(
'{% data %} reached the transform phase unresolved.\n\n' +
'If this file is pulled in with {% partial %}: use {% include %} instead. ' +
'Markdoc expands partials during the transform, which is after the preprocess ' +
'phase that resolves `data` — so a `data` tag inside a partial never gets read. ' +
'{% include file="..." /%} pastes the file before preprocess, so `data` and ' +
'`snippet` inside it resolve. Both runes read the same `_partials/` directory ' +
'and file roots, so only the call site changes.\n\n' +
'If you are building a custom pipeline: registered `preprocess` hooks must run ' +
'before `Markdoc.transform` (data pre-resolves to a Markdoc `table` node).',
);
},
});
Feed tables, charts and sortable tables from real data files instead of hand-typed Markdown tables. data reads a file from your project at build time, filters, sorts and selects its rows, and renders a plain table. That table works without JavaScript, and {% chart %} and {% datatable %} take it like one you typed.
Basic usage
src is relative to the project root. The format comes from the extension: .csv, .tsv, .json, and .ndjson or .jsonl; for anything else, set format.
{% data src="site/examples/revenue.csv" /%}<div class="rf-table-wrapper">
<table>
<thead>
<tr>
<th>product</th>
<th>revenue</th>
<th>region</th>
<th>units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="1200">$1,200</td>
<td>EMEA</td>
<td data-value="340">340</td>
</tr>
<tr>
<td>Gadget</td>
<td data-value="900">$900</td>
<td>AMER</td>
<td data-value="210">210</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="1500">$1,500</td>
<td>EMEA</td>
<td data-value="120">120</td>
</tr>
<tr>
<td>Sprocket</td>
<td data-value="1050">$1,050</td>
<td>APAC</td>
<td data-value="260">260</td>
</tr>
</tbody>
</table>
</div>| product | revenue | region | units |
|---|---|---|---|
| Widget | $1,200 | EMEA | 340 |
| Gadget | $900 | AMER | 210 |
| Gizmo | $1,500 | EMEA | 120 |
| Sprocket | $1,050 | APAC | 260 |
<div class="rf-table-wrapper">
<table>
<thead>
<tr>
<th>product</th>
<th>revenue</th>
<th>region</th>
<th>units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="1200">$1,200</td>
<td>EMEA</td>
<td data-value="340">340</td>
</tr>
<tr>
<td>Gadget</td>
<td data-value="900">$900</td>
<td>AMER</td>
<td data-value="210">210</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="1500">$1,500</td>
<td>EMEA</td>
<td data-value="120">120</td>
</tr>
<tr>
<td>Sprocket</td>
<td data-value="1050">$1,050</td>
<td>APAC</td>
<td data-value="260">260</td>
</tr>
</tbody>
</table>
</div>Change the file and the table updates on the next build.
How Markdown is read
| You write | It becomes |
|---|---|
| No body | A table of the shaped rows |
| A body | A template rendered once per row, with $row bound (Render each row with a template) |
A --- in the body, when headers is set | A cell boundary: the body becomes one table row, a cell per zone (A table with formatted cells) |
Filter, sort and pick columns
These run at build time, in this order: where, sort, columns, then limit and offset.
{% data src="site/examples/revenue.csv"
where="region:EMEA"
sort="-units"
columns="product as Product, units as Units"
numeric="units" /%}<div class="rf-table-wrapper">
<table>
<thead>
<tr>
<th>Product</th>
<th>Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="340">340</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="120">120</td>
</tr>
</tbody>
</table>
</div>| Product | Units |
|---|---|
| Widget | 340 |
| Gizmo | 120 |
<div class="rf-table-wrapper">
<table>
<thead>
<tr>
<th>Product</th>
<th>Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="340">340</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="120">120</td>
</tr>
</tbody>
</table>
</div>wherefilters with thefield:valueform collection uses: exact,glob*, or/regex/. Repeat a field for OR; combine fields for AND.sortorders by a column; a-prefix sorts descending.columnsselects, reorders, and renames. Quote an alias with spaces or punctuation:"revenue as 'Revenue ($)'".limitandoffsettake a slice of the rows.
Treat a column as numbers
A column whose cells all read as numbers, even formatted ones like $1,200 or 98%, is numeric automatically: it sorts by value, a chart plots it, and the reader still sees the formatted text. Use numeric="col" to force it, for a column with a few blanks or footnote markers, and text="col" to keep a code like 007 as text.
Feed a chart
Put data inside {% chart %}:
{% chart type="bar" title="Revenue by product" %}
{% data src="site/examples/revenue.csv" columns="product, revenue" numeric="revenue" /%}
{% /chart %}<rf-chart data-rune="chart" data-rune-fields="{"type":"bar","stacked":"false"}">
<table data-name="data">
<caption>Revenue by product</caption>
<thead>
<tr>
<th>product</th>
<th>revenue</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="1200">$1,200</td>
</tr>
<tr>
<td>Gadget</td>
<td data-value="900">$900</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="1500">$1,500</td>
</tr>
<tr>
<td>Sprocket</td>
<td data-value="1050">$1,050</td>
</tr>
</tbody>
</table>
</rf-chart>| product | revenue |
|---|---|
| Widget | $1,200 |
| Gadget | $900 |
| Gizmo | $1,500 |
| Sprocket | $1,050 |
<rf-chart class="rf-chart" data-type="bar" data-stacked="false" data-elevation="sunken" data-rune="chart" data-density="compact">
<table data-name="data" class="rf-chart__data">
<caption>Revenue by product</caption>
<thead>
<tr>
<th>product</th>
<th>revenue</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="1200">$1,200</td>
</tr>
<tr>
<td>Gadget</td>
<td data-value="900">$900</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="1500">$1,500</td>
</tr>
<tr>
<td>Sprocket</td>
<td data-value="1050">$1,050</td>
</tr>
</tbody>
</table>
</rf-chart>Let readers search and sort
Put data inside {% datatable %}. Numeric columns sort by value, so $1,200 sorts above $900.
{% datatable sortable="all" searchable=true %}
{% data src="site/examples/revenue.csv" numeric="revenue,units" /%}
{% /datatable %}<div data-rune="data-table" data-rune-fields="{"sortable":"all","searchable":"true","pageSize":"0","defaultSort":""}">
<div data-name="scroll">
<table data-name="table">
<thead>
<tr>
<th>product</th>
<th>revenue</th>
<th>region</th>
<th>units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="1200">$1,200</td>
<td>EMEA</td>
<td data-value="340">340</td>
</tr>
<tr>
<td>Gadget</td>
<td data-value="900">$900</td>
<td>AMER</td>
<td data-value="210">210</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="1500">$1,500</td>
<td>EMEA</td>
<td data-value="120">120</td>
</tr>
<tr>
<td>Sprocket</td>
<td data-value="1050">$1,050</td>
<td>APAC</td>
<td data-value="260">260</td>
</tr>
</tbody>
</table>
</div>
</div>| product | revenue | region | units |
|---|---|---|---|
| Widget | $1,200 | EMEA | 340 |
| Gadget | $900 | AMER | 210 |
| Gizmo | $1,500 | EMEA | 120 |
| Sprocket | $1,050 | APAC | 260 |
<div class="rf-datatable rf-datatable--true rf-datatable--all rf-datatable--0" data-searchable="true" data-sortable="all" data-page-size="0" data-default-sort="" data-elevation="sunken" data-rune="data-table" data-density="compact">
<div data-name="scroll" class="rf-datatable__scroll">
<table data-name="table" class="rf-datatable__table" data-section="body">
<thead>
<tr>
<th>product</th>
<th>revenue</th>
<th>region</th>
<th>units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget</td>
<td data-value="1200">$1,200</td>
<td>EMEA</td>
<td data-value="340">340</td>
</tr>
<tr>
<td>Gadget</td>
<td data-value="900">$900</td>
<td>AMER</td>
<td data-value="210">210</td>
</tr>
<tr>
<td>Gizmo</td>
<td data-value="1500">$1,500</td>
<td>EMEA</td>
<td data-value="120">120</td>
</tr>
<tr>
<td>Sprocket</td>
<td data-value="1050">$1,050</td>
<td>APAC</td>
<td data-value="260">260</td>
</tr>
</tbody>
</table>
</div>
</div>data decides at build time which rows are on the page at all; datatable lets readers hide and reorder those rows in the browser. Filter with where to leave rows out, and with the datatable's search to let readers narrow them.
Read nested JSON
Point root at the array or object inside the document, as a dotted path (data.results) or a JSON Pointer (/data/results). Nested fields become dotted column names, which columns can pick:
{% data src="data/api-dump.json" root="data.results" where="region:EMEA"
columns="product as Product, geo.country as Country, units as Units"
numeric="units" sort="-units" /%}
orient says how each element becomes a row:
records(detected):[{name, revenue}, …]; the keys become columns.values(detected):[["name","revenue"],["a",10]]; the first array is the header row.index:{ "us": {…}, … }; each key becomes a column named bykey-column.
NDJSON reads one record per line, and every key that appears in any record becomes a column.
Render each row with a template
Give data a body and it renders the body once per row instead of a table, with $row bound to that row:
{% data src="team.csv" sort="name" %}
{% card %}
### {% $row.name %}
{% $row.role %}
{% /card %}
{% /data %}
$row.<column> uses the name after columns renames it. Numeric columns bind as numbers, the rest as text. Filtering, sorting and slicing run first, as for the table.
The rendered rows land where hand-written ones would, so runes that build from their children, such as accordion, tabs and bento, work with them:
{% accordion %}
{% data src="faq.csv" %}
{% accordion-item %}
## {% $row.question %}
{% $row.answer %}
{% /accordion-item %}
{% /data %}
{% /accordion %}
A {% snippet %}, {% include %} or nested {% data %} in the body can take its attributes from $row, as in {% snippet path=$row.path /%}.
Markdoc does not fill in variables inside backticks, so `{% $row.name %}` prints the tag itself on every row, with no warning. Use {% code %}{% $row.name %}{% /code %} for a code-styled value.
A table with formatted cells
Set headers and the body becomes one table row, with --- between cells:
{% data src="_data/rune-attributes.json" root="attributes" where="rune:card scope:own"
headers="Attribute, Type, Required, Description" %}
{% code %}{% $row.name %}{% /code %}
---
{% $row.type %}
---
{% if $row.required %}✓{% else /%}—{% /if %}
---
{% $row.description %}
{% /data %}
Use it when cells need markup: the bodyless table shows cell text as-is, so it can print true but never ✓. chart and datatable take this table the same way.
- The number of
headersmust match the number of cells, or the build fails. headersneeds a body; on a self-closing tag it is an error.- A
---inside a cell's text splits it, as in card and grid.
Nested queries
A {% data %} in another's body runs once per outer row:
{% data src="axes.json" where=$r %}
## {% $row.axis %}
{% data src="axis-attributes.json" where=$row.query headers="Attribute, Type" %}
{% code %}{% $row.name %}{% /code %}
---
{% $row.type %}
{% /data %}
{% /data %}
Inside the inner body, $row is the inner row. The outer row reaches the inner query only through its attributes, as in where=$row.query. Nothing builds strings here, so store the ready-made filter ("axis:bg") in a column. An inner query that matches nothing renders nothing, and the outer row still renders.
Limits
{% if %}works ({% else /%}is self-closing); there is no loop inside a row.$rowis not$item: a collection's$itemis an entity withdata, a row is flat, so$itemhere is an error.- A body inside
chartordatatableis an error; they need the table. numericandtextwarn on a body, since there are no table cells to type; numeric columns still bind as numbers.
When something goes wrong
A missing file, a file it cannot parse, a path outside the project, or a file or root with no rows shows an error in place of the table, and the build reports it. A where that matches nothing is not an error: the rune renders nothing, the way a list of open bugs is empty when there are none.
An attribute the rune cannot read is a build error. Attributes are read before functions run, so where=concat(…) fails, as do an undefined variable and an empty where; read as empty, they would show every row.
A column name that the file does not have is a warning that lists the ones it does:
data "revenue.csv": `where` names a column the source does not have: "regio". Available: region, quarter, revenue.
Attributes
Every attribute in one table. Format-specific ones say so in their description — CSV/TSV: or JSON:; the rest apply to every source.
| Attribute | Type | Required | Description |
|---|---|---|---|
src | string | ✓ | Path to the source file, relative to the project root (sandboxed). |
format | "csv" | "tsv" | "json" | "ndjson" | — | Source format. Inferred from the file extension; override for ambiguity. |
delimiter | string | — | Override the field separator (CSV/TSV). |
header | boolean | — | CSV/TSV: whether the first row is the header (default true). false synthesizes col1… |
root | string | — | JSON: dotted path / JSON Pointer to the array or map within the document. |
orient | "records" | "values" | "index" | — | JSON: how each element maps to a row. records/values auto-detected; index is explicit. |
key-column | string | — | JSON: when orient=index, the header for the synthesized key column. |
columns | string | — | Select + order + rename: "name as Product, revenue as 'Revenue ($)'". |
where | string | — | Filter rows with `field:value` clauses. |
sort | string | — | Sort by a column; "-" prefix for descending. |
limit | number | — | Maximum number of rows. |
offset | number | — | Skip this many rows before limiting. |
headers | string | — | Header labels for a `---`-delimited body, e.g. "Attribute, Type". Setting it makes the body one table ROW — cells separated by `---` — instead of a run of blocks, so cells can carry emphasis, links, runes and {% if %}. Requires a body. |
numeric | string | — | Comma-separated columns to force to numeric typing (emits data-value). |
text | string | — | Comma-separated columns to force to text typing. |
Universal attributes
bg
The background layer: image, video, gradient, flat overlay wash and legibility scrim, in a single injected layer behind the rune's content.
| Attribute | Type | Required | Description |
|---|---|---|---|
bg | string | — | Background preset applied to this block |
bg-from | string | — | Gradient start colour — a semantic token name (→ var(--rf-color-*)) |
bg-gradient | "to-t" | "to-b" | "to-l" | "to-r" | "to-tr" | "to-br" | "to-bl" | "to-tl" | — | Gradient direction (bounded named set) |
bg-gradient-type | "linear" | "radial" | "conic" | — | Gradient type |
bg-to | string | — | Gradient end colour — a semantic token name |
bg-via | string | — | Optional middle gradient stop — a semantic token name |
scrim | "top" | "bottom" | "left" | "right" | "none" | — | Scrim direction (heaviest edge); presence turns the scrim on, "none" opts out of the default cover scrim |
scrim-blur | "none" | "sm" | "md" | "lg" | — | Frost scrim blur amount |
scrim-strength | "sm" | "md" | "lg" | — | Gradient scrim strength |
scrim-tone | "dark" | "light" | — | Whether the scrim darkens (for light text) or lightens (for dark text) |
scrim-type | "gradient" | "frost" | — | Scrim treatment: gradient (default) or frost (backdrop blur) |
elevation
How far the rune sits off the page, from sunken to overlay. The theme decides what each step looks like.
| Attribute | Type | Required | Description |
|---|---|---|---|
elevation | "sunken" | "flush" | "flat" | "raised" | "floating" | "overlay" | "none" | "sm" | "md" | "lg" | — | Surface depth, from sunken to overlay. Deprecated: none, sm, md and lg (read as flat, raised, raised and floating) |
inset
Internal padding override.
| Attribute | Type | Required | Description |
|---|---|---|---|
inset | "flush" | "tight" | "default" | "loose" | "breathe" | — | Inner padding of this block |
motion
Scroll-reveal entrance. You choose its character; the theme decides how it moves.
| Attribute | Type | Required | Description |
|---|---|---|---|
reveal | "none" | "fade" | "slide" | "scale" | "blur" | — | Scroll-reveal entrance character (none|fade|slide|scale|blur); the theme owns the choreography |
stagger | boolean | — | Cascade this block's items in as it reveals (no-op on single-child runes) |
spacing
Block-level rhythm override.
| Attribute | Type | Required | Description |
|---|---|---|---|
spacing | "flush" | "tight" | "default" | "loose" | "breathe" | — | Vertical spacing above and below this block |
substrate
Generated pattern fills behind the content, drawn by the theme.
| Attribute | Type | Required | Description |
|---|---|---|---|
substrate | "dots" | "grid" | "lines" | "cross" | "checker" | "none" | — | Generated surface pattern |
substrate-fill | "inherit" | "inset" | — | Surface fill the pattern sits on (full colour stays with tint) |
substrate-opacity | "sm" | "md" | "lg" | — | Pattern ink strength |
substrate-size | "sm" | "md" | "lg" | — | Pattern cell size |
substrate-target | "self" | "media" | — | Which surface the pattern fills (overrides the rune/theme default) |
tint
Per-rune colour override: a named tint from the theme registry, with inline per-token overrides layered on top.
| Attribute | Type | Required | Description |
|---|---|---|---|
tint | string | — | Color tint preset applied to this block |
tint-mode | "auto" | "dark" | "light" | — | Whether the tint adapts to auto, dark, or light mode |
width
The track a block rune occupies.
| Attribute | Type | Required | Description |
|---|---|---|---|
width | "compact" | "narrow" | "content" | "wide" | "full" | — | Maximum width constraint for this block |
| Not available | Why |
|---|---|
| dropcap, reading | this rune declares no prose body |
| frame | this rune declares neither a `frameTarget` nor a media section |
| prominence | this rune has no page-section header or featured quotation |
See also
- Chart — plots the emitted table.
- Datatable — search and sort over the emitted table in the browser.
- Snippet — embeds a project file as a code block.
- Hosted & in-memory builds — how
srcis read when a build has no local filesystem.