Data

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).',
		);
	},
});

View packages/runes/src/tags/data.ts on GitHub

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>
productrevenueregionunits
Widget$1,200EMEA340
Gadget$900AMER210
Gizmo$1,500EMEA120
Sprocket$1,050APAC260
<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 writeIt becomes
No bodyA table of the shaped rows
A bodyA template rendered once per row, with $row bound (Render each row with a template)
A --- in the body, when headers is setA 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>
ProductUnits
Widget340
Gizmo120
<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>
  • where filters with the field:value form collection uses: exact, glob*, or /regex/. Repeat a field for OR; combine fields for AND.
  • sort orders by a column; a - prefix sorts descending.
  • columns selects, reorders, and renames. Quote an alias with spaces or punctuation: "revenue as 'Revenue ($)'".
  • limit and offset take 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="{&quot;type&quot;:&quot;bar&quot;,&quot;stacked&quot;:&quot;false&quot;}">
  <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>
Revenue by product
productrevenue
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="{&quot;sortable&quot;:&quot;all&quot;,&quot;searchable&quot;:&quot;true&quot;,&quot;pageSize&quot;:&quot;0&quot;,&quot;defaultSort&quot;:&quot;&quot;}">
  <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>
productrevenueregionunits
Widget$1,200EMEA340
Gadget$900AMER210
Gizmo$1,500EMEA120
Sprocket$1,050APAC260
<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 by key-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 headers must match the number of cells, or the build fails.
  • headers needs 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.
  • $row is not $item: a collection's $item is an entity with data, a row is flat, so $item here is an error.
  • A body inside chart or datatable is an error; they need the table.
  • numeric and text warn 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.

AttributeTypeRequiredDescription
srcstring✓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.
delimiterstring—Override the field separator (CSV/TSV).
headerboolean—CSV/TSV: whether the first row is the header (default true). false synthesizes col1…
rootstring—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-columnstring—JSON: when orient=index, the header for the synthesized key column.
columnsstring—Select + order + rename: "name as Product, revenue as 'Revenue ($)'".
wherestring—Filter rows with `field:value` clauses.
sortstring—Sort by a column; "-" prefix for descending.
limitnumber—Maximum number of rows.
offsetnumber—Skip this many rows before limiting.
headersstring—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.
numericstring—Comma-separated columns to force to numeric typing (emits data-value).
textstring—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.

AttributeTypeRequiredDescription
bgstring—Background preset applied to this block
bg-fromstring—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-tostring—Gradient end colour — a semantic token name
bg-viastring—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.

AttributeTypeRequiredDescription
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.

AttributeTypeRequiredDescription
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.

AttributeTypeRequiredDescription
reveal"none" | "fade" | "slide" | "scale" | "blur"—Scroll-reveal entrance character (none|fade|slide|scale|blur); the theme owns the choreography
staggerboolean—Cascade this block's items in as it reveals (no-op on single-child runes)
spacing

Block-level rhythm override.

AttributeTypeRequiredDescription
spacing"flush" | "tight" | "default" | "loose" | "breathe"—Vertical spacing above and below this block
substrate

Generated pattern fills behind the content, drawn by the theme.

AttributeTypeRequiredDescription
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.

AttributeTypeRequiredDescription
tintstring—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.

AttributeTypeRequiredDescription
width"compact" | "narrow" | "content" | "wide" | "full"—Maximum width constraint for this block
Not availableWhy
dropcap, readingthis rune declares no prose body
framethis rune declares neither a `frameTarget` nor a media section
prominencethis 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 src is read when a build has no local filesystem.