Text & inline›Code

core

Code

Inline code span for dynamic content, where backticks cannot reach

Source

import Markdoc from '@markdoc/markdoc';
import type { Config, Node, RenderableTreeNode, Schema } from '@markdoc/markdoc';
import { declareUniversalPosture } from '../lib/index.js';
const { Tag } = Markdoc;

export const code: Schema = {
	inline: true,
	attributes: {},
	transform(node: Node, config: Config) {
		const children = node.transformChildren(config) as RenderableTreeNode[];
		return new Tag('code', { 'data-rune': 'code' }, children);
	},
};

declareUniversalPosture(code, 'inline');

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

An inline code span you can put a variable inside. Here each attribute name of badge comes from a data file, one table row per attribute:

{% data src="site/content/_data/rune-attributes.json" root="own" where="rune:badge" headers="Attribute, Description" %}
{% code %}{% $row.name %}{% /code %}
---
{% $row.description %}
{% /data %}
<div class="rf-table-wrapper">
  <table>
    <thead>
      <tr>
        <th>Attribute</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <code data-rune="code">sentiment</code>
        </td>
        <td>Sentiment dimension: positive, negative, caution, neutral. Default: neutral.</td>
      </tr>
      <tr>
        <td>
          <code data-rune="code">type</code>
        </td>
        <td>Meta-type dimension: status, category, quantity, temporal, tag, id. Default: tag.</td>
      </tr>
    </tbody>
  </table>
</div>
AttributeDescription
sentimentSentiment dimension: positive, negative, caution, neutral. Default: neutral.
typeMeta-type dimension: status, category, quantity, temporal, tag, id. Default: tag.
<div class="rf-table-wrapper">
  <table>
    <thead>
      <tr>
        <th>Attribute</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <code class="rf-code" data-rune="code" data-density="full">sentiment</code>
        </td>
        <td>Sentiment dimension: positive, negative, caution, neutral. Default: neutral.</td>
      </tr>
      <tr>
        <td>
          <code class="rf-code" data-rune="code" data-density="full">type</code>
        </td>
        <td>Meta-type dimension: status, category, quantity, temporal, tag, id. Default: tag.</td>
      </tr>
    </tbody>
  </table>
</div>

That is the case it was built for: a generated reference table whose first column is a name. See data for the headers attribute.

Use backticks unless the content is dynamic

For static code, backticks are still correct and always will be: write `href`, not {% code %}href{% /code %}. Reach for this rune only when what goes inside is a variable — a {% data %} row value, a binding passed to an {% include %}. Exactly as **bold** stays correct for static emphasis.

Why backticks can't do it

A code span is literal by definition. Everything between the backticks is text, so Markdoc syntax inside one is never parsed. The same row value, in backticks and in the rune:

{% data src="site/content/_data/rune-attributes.json" root="own" where="rune:badge" headers="Backticks, The code rune" %}
`{% $row.name %}`
---
{% code %}{% $row.name %}{% /code %}
{% /data %}
<div class="rf-table-wrapper">
  <table>
    <thead>
      <tr>
        <th>Backticks</th>
        <th>The code rune</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <code>{% $row.name %}</code>
        </td>
        <td>
          <code data-rune="code">sentiment</code>
        </td>
      </tr>
      <tr>
        <td>
          <code>{% $row.name %}</code>
        </td>
        <td>
          <code data-rune="code">type</code>
        </td>
      </tr>
    </tbody>
  </table>
</div>
BackticksThe code rune
{% $row.name %}sentiment
{% $row.name %}type
<div class="rf-table-wrapper">
  <table>
    <thead>
      <tr>
        <th>Backticks</th>
        <th>The code rune</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <code>{% $row.name %}</code>
        </td>
        <td>
          <code class="rf-code" data-rune="code" data-density="full">sentiment</code>
        </td>
      </tr>
      <tr>
        <td>
          <code>{% $row.name %}</code>
        </td>
        <td>
          <code class="rf-code" data-rune="code" data-density="full">type</code>
        </td>
      </tr>
    </tbody>
  </table>
</div>

That is standard Markdown, not a refrakt quirk — and it is the only inline construct with this property. Everything else passes a variable through intact:

AuthoredRenders
[{% $row.name %}](/runes/card)a link labelled href
[**{% $row.name %}**](…)bold inside a link
**_{% $row.name %}_**nested emphasis
{% badge %}{% $row.name %}{% /badge %}a badge reading href
`{% $row.name %}`the literal text {% $row.name %}

So this rune exists to close one specific gap, not to offer a second spelling for something Markdown already does well.

Attributes

Not availableWhy
bg, dropcap, elevation, frame, inset, motion (reveal, stagger), prominence, reading, spacing, substrate, tint, widththis rune is inline — the block-level universal axes have nothing to act on

See also

  • data — the per-row body this was built for
  • include — the other place a variable reaches content
  • snippet — for a block of code read from a file