Media›Track

media

Track

Standalone track or recording with metadata

Install

  1. Add the package: npm install @refrakt-md/media
  2. Add it to the plugins array in refrakt.config.json: "plugins": ["@refrakt-md/media"]

About the plugin and its runes

Source

import Markdoc from '@markdoc/markdoc';
import type { RenderableTreeNodes } from '@markdoc/markdoc';
const { Tag } = Markdoc;
import {
	createContentModelSchema,
	createComponentRenderable,
	renderNodes,
	SCHEMA_TYPE_EXPLICIT,
	textContent,
} from '@refrakt-md/runes';
import { parseDuration } from '../duration.js';

const trackType = ['song', 'episode', 'chapter', 'talk', 'video'] as const;

export const TYPE_IMPLICIT = SCHEMA_TYPE_EXPLICIT;

const trackCommon = {
	name: 'name',
	duration: 'duration',
	url: 'url',
	position: 'position',
	date: 'datePublished',
} as const;

export const trackSchema = {
	by: 'type',
	rows: {
		song: { type: 'MusicRecording', properties: { ...trackCommon, artist: 'byArtist' } },
		episode: { type: 'PodcastEpisode', properties: trackCommon },
		chapter: { type: 'Chapter', properties: trackCommon },
		talk: { type: 'CreativeWork', properties: trackCommon },
		video: { type: 'VideoObject', properties: trackCommon },
	},
	fallback: { type: 'MusicRecording', properties: { ...trackCommon, artist: 'byArtist' } },
};

export const track = createContentModelSchema({
	schema: trackSchema,
	attributes: {
		src: {
			type: String,
			required: false,
			description: 'URL of the audio or video file for this track.',
		},
		artist: {
			type: String,
			required: false,
			description: 'Performer or creator name for this track.',
		},
		duration: {
			type: String,
			required: false,
			description: 'Track length in m:ss, h:mm:ss, or ISO 8601 duration format.',
		},
		number: {
			type: Number,
			required: false,
			description: 'Position number of the track in its parent listing.',
		},
		date: {
			type: String,
			required: false,
			description: 'Publication or release date for the track.',
		},
		url: {
			type: String,
			required: false,
			description: 'External link to the track on a streaming platform or website.',
		},
		type: {
			type: String,
			required: false,
			matches: trackType.slice(),
			description: 'Media category: song, episode, chapter, talk, or video.',
		},
		meta: {
			type: String,
			required: false,
			matches: ['row', 'byline'],
			description:
				'Where the artist, date and running time sit: in the row beside the name (`row`), or beneath it (`byline`). A playlist passes its own to its tracks.',
		},
	},
	contentModel: {
		type: 'sequence',
		fields: [
			{ name: 'title', match: 'heading|paragraph', optional: true },
			{ name: 'body', match: 'any', optional: true, greedy: true },
		],
	},
	transform(resolved, attrs, config) {
		const src = (attrs.src as string) ?? '';
		const artist = (attrs.artist as string) ?? '';
		const duration = (attrs.duration as string) ?? '';
		const number = attrs.number as number | undefined;
		const date = (attrs.date as string) ?? '';
		const url = (attrs.url as string) ?? '';
		// An absent `type` stays absent, so a nested track can take its playlist's.
		const explicitType = attrs.type as string | undefined;
		const typeValue = explicitType ?? 'song';

		const titleNodes = renderNodes(resolved.title, config);

		let nameText = '';
		const headingCursor = titleNodes.headings().limit(1);
		if (headingCursor.count() > 0) {
			nameText = textContent(headingCursor.next());
		} else {
			const pCursor = titleNodes.tag('p').limit(1);
			if (pCursor.count() > 0) nameText = textContent(pCursor.next());
		}

		const bodyNodes = renderNodes(resolved.body, config);

		const nameTag = new Tag('span', { 'data-name': 'track-name' }, [nameText]);
		const children: any[] = [nameTag];

		const typeMeta = new Tag('meta', { content: typeValue });

		if (bodyNodes.count() > 0) {
			children.push(bodyNodes.wrap('div', { 'data-name': 'track-description' }).next());
		}

		const rootAttrs: Record<string, any> = {};
		if (src) rootAttrs['data-src'] = src;

		const durationMeta = duration
			? new Tag('meta', {
					content: duration.startsWith('PT') ? duration : `PT${parseDuration(duration)}S`,
				})
			: undefined;
		const artistMeta = artist ? new Tag('meta', { content: artist }) : undefined;
		const urlMeta = url ? new Tag('meta', { content: url }) : undefined;
		const numberMeta =
			number !== undefined ? new Tag('meta', { content: String(number) }) : undefined;
		const dateMeta = date ? new Tag('meta', { content: date }) : undefined;
		const metaMeta = attrs.meta ? new Tag('meta', { content: attrs.meta as string }) : undefined;

		const renderable = createComponentRenderable({
			rune: 'track',
			tag: 'li',
			properties: {
				name: nameTag,
				artist: artistMeta,
				duration: durationMeta,
				url: urlMeta,
				position: numberMeta,
				date: dateMeta,
				type: typeMeta,
				meta: metaMeta,
			},
			children,
		});

		Object.assign((renderable as any).attributes, rootAttrs);
		if (explicitType !== undefined) (renderable as any)[TYPE_IMPLICIT] = true;
		return renderable;
	},
});

View plugins/media/src/tags/track.ts on GitHub

A single song or podcast episode, with its artist, running time and date, on its own or inside a playlist. For a list of tracks, use playlist; to play a file, audio.

Basic usage

A single track with artist and duration.

{% track artist="Radiohead" duration="4:01" type="song" %}
# Everything in Its Right Place
{% /track %}
<li data-rune="track" data-rune-fields="{&quot;artist&quot;:&quot;Radiohead&quot;,&quot;duration&quot;:&quot;PT241S&quot;,&quot;type&quot;:&quot;song&quot;}" typeof="MusicRecording">
  <span data-name="track-name" data-field="name" property="name">Everything in Its Right Place</span>
  <meta property="duration" content="PT241S">
  <meta property="byArtist" content="Radiohead">
</li>
  • Everything in Its Right Place
    Radiohead4:01
  • <li class="rf-track" typeof="MusicRecording" data-meta="row" data-artist="Radiohead" data-duration="PT241S" data-elevation="flat" data-rune="track" data-density="full">
      <span data-name="track-name" data-field="name" property="name" class="rf-track__track-name">Everything in Its Right Place</span>
      <div data-name="details" data-zone="details" data-zone-layout="bar" class="rf-track__details">
        <span data-field="artist">Radiohead</span>
        <span data-meta-type="quantity" data-align="end" data-field="duration">4:01</span>
      </div>
      <meta property="duration" content="PT241S" />
      <meta property="byArtist" content="Radiohead" />
    </li>

    How Markdown is read

    You writeIt becomes
    A headingThe track's name
    Everything after itThe track's description or show notes

    Byline

    The artist, date and running time sit along the track's row by default. meta="byline" sets them beneath the name instead.

    {% track artist="Radiohead" duration="4:01" date="2000" meta="byline" %}
    # Everything in Its Right Place
    {% /track %}
    <li data-rune="track" data-rune-fields="{&quot;artist&quot;:&quot;Radiohead&quot;,&quot;duration&quot;:&quot;PT241S&quot;,&quot;date&quot;:&quot;2000&quot;,&quot;type&quot;:&quot;song&quot;,&quot;meta&quot;:&quot;byline&quot;}" typeof="MusicRecording">
      <span data-name="track-name" data-field="name" property="name">Everything in Its Right Place</span>
      <meta property="duration" content="PT241S">
      <meta property="datePublished" content="2000">
      <meta property="byArtist" content="Radiohead">
    </li>
  • Everything in Its Right Place
    Radiohead20004:01
  • <li class="rf-track" typeof="MusicRecording" data-meta="byline" data-artist="Radiohead" data-duration="PT241S" data-date="2000" data-elevation="flat" data-rune="track" data-density="full">
      <div data-name="heading" class="rf-track__heading">
        <span data-name="track-name" data-field="name" property="name" class="rf-track__track-name">Everything in Its Right Place</span>
        <div data-name="details" data-zone="details" data-zone-layout="bar" class="rf-track__details">
          <span data-field="artist">Radiohead</span>
          <span data-meta-type="temporal" data-field="date">2000</span>
          <span data-meta-type="quantity" data-field="duration">4:01</span>
        </div>
      </div>
      <meta property="duration" content="PT241S" />
      <meta property="datePublished" content="2000" />
      <meta property="byArtist" content="Radiohead" />
    </li>

    Podcast episode

    Use type="episode" for podcast episodes with a date.

    {% track type="episode" artist="Tech Weekly" duration="42:30" date="March 2025" %}
    # The Future of Web Standards
    {% /track %}
    <li data-rune="track" data-rune-fields="{&quot;artist&quot;:&quot;Tech Weekly&quot;,&quot;duration&quot;:&quot;PT2550S&quot;,&quot;date&quot;:&quot;March 2025&quot;,&quot;type&quot;:&quot;episode&quot;}" typeof="PodcastEpisode">
      <span data-name="track-name" data-field="name" property="name">The Future of Web Standards</span>
      <meta property="duration" content="PT2550S">
      <meta property="datePublished" content="March 2025">
    </li>
  • The Future of Web Standards
    Tech Weekly42:30March 2025
  • <li class="rf-track" typeof="PodcastEpisode" data-meta="row" data-artist="Tech Weekly" data-duration="PT2550S" data-date="March 2025" data-elevation="flat" data-rune="track" data-density="full">
      <span data-name="track-name" data-field="name" property="name" class="rf-track__track-name">The Future of Web Standards</span>
      <div data-name="details" data-zone="details" data-zone-layout="bar" class="rf-track__details">
        <span data-field="artist">Tech Weekly</span>
        <span data-meta-type="quantity" data-align="end" data-field="duration">42:30</span>
        <span data-meta-type="temporal" data-field="date">March 2025</span>
      </div>
      <meta property="duration" content="PT2550S" />
      <meta property="datePublished" content="March 2025" />
    </li>

    With body content

    Tracks can include additional body content like descriptions or show notes.

    {% track artist="Miles Davis" duration="9:22" type="song" %}
    # So What
    
    The opening track from *Kind of Blue*, featuring the iconic two-chord modal structure that changed jazz forever.
    {% /track %}
    <li data-rune="track" data-rune-fields="{&quot;artist&quot;:&quot;Miles Davis&quot;,&quot;duration&quot;:&quot;PT562S&quot;,&quot;type&quot;:&quot;song&quot;}" typeof="MusicRecording">
      <span data-name="track-name" data-field="name" property="name">So What</span>
      <div data-name="track-description">
        <p>
          The opening track from
          <em marker="*">Kind of Blue</em>
          , featuring the iconic two-chord modal structure that changed jazz forever.
        </p>
      </div>
      <meta property="duration" content="PT562S">
      <meta property="byArtist" content="Miles Davis">
    </li>
  • So What
    Miles Davis9:22

    The opening track from Kind of Blue, featuring the iconic two-chord modal structure that changed jazz forever.

  • <li class="rf-track" typeof="MusicRecording" data-meta="row" data-artist="Miles Davis" data-duration="PT562S" data-elevation="flat" data-rune="track" data-density="full">
      <span data-name="track-name" data-field="name" property="name" class="rf-track__track-name">So What</span>
      <div data-name="details" data-zone="details" data-zone-layout="bar" class="rf-track__details">
        <span data-field="artist">Miles Davis</span>
        <span data-meta-type="quantity" data-align="end" data-field="duration">9:22</span>
      </div>
      <div data-name="track-description" class="rf-track__track-description">
        <p>
          The opening track from 
          <em marker="*">Kind of Blue</em>
          , featuring the iconic two-chord modal structure that changed jazz forever.
        </p>
      </div>
      <meta property="duration" content="PT562S" />
      <meta property="byArtist" content="Miles Davis" />
    </li>

    Inside a playlist

    A {% track %} written inside a {% playlist %} becomes one of that playlist's tracks — in the listing, in the HTML, and in the structured data. Use it when the list format is too limited: body content, per-track links, or cue points with prose.

    It costs nothing to switch. A nested track that states no type takes the playlist's kind, and inherits the playlist's artist the way a list item does, so the same track written either way produces the same structured data. The longer form may carry more; it never carries less.

    {% playlist type="podcast" artist="Tech Weekly" %}
    	# Design Systems Weekly
    
    	- **Token Architecture** (38:15) — February 2025
    
    	{% track duration="45:30" date="March 2025" url="https://example.com/ep-12" %}
    	# Component Libraries at Scale
    
    	Why component libraries stall at the second team, and what to do about it.
    	{% /track %}
    {% /playlist %}
    <section data-rune="playlist" data-rune-fields="{&quot;type&quot;:&quot;podcast&quot;,&quot;media-position&quot;:&quot;top&quot;,&quot;artist&quot;:&quot;Tech Weekly&quot;}" typeof="PodcastSeries" data-schema-lists="[&quot;hasPart&quot;]">
      <h1 id="design-systems-weekly" data-name="headline" property="name">Design Systems Weekly</h1>
      <ol data-name="tracks">
        <li data-rune="track" data-rune-fields="{&quot;artist&quot;:&quot;Tech Weekly&quot;,&quot;duration&quot;:&quot;PT2295S&quot;,&quot;date&quot;:&quot;February 2025&quot;}" elevation="flush" data-field="track" typeof="PodcastEpisode" property="hasPart">
          <span data-name="track-name" property="name">Token Architecture</span>
          <meta data-name="artist" content="Tech Weekly">
          <meta data-name="duration" content="PT2295S" property="duration">
          <meta data-name="date" content="February 2025" property="datePublished">
        </li>
        <li data-rune="track" data-rune-fields="{&quot;duration&quot;:&quot;PT2730S&quot;,&quot;url&quot;:&quot;https://example.com/ep-12&quot;,&quot;date&quot;:&quot;March 2025&quot;,&quot;type&quot;:&quot;episode&quot;,&quot;artist&quot;:&quot;Tech Weekly&quot;}" typeof="PodcastEpisode" elevation="flush" data-field="track" property="hasPart">
          <span data-name="track-name" data-field="name" property="name">Component Libraries at Scale</span>
          <div data-name="track-description">
            <p>Why component libraries stall at the second team, and what to do about it.</p>
          </div>
          <meta content="Tech Weekly" data-name="artist">
          <meta property="duration" content="PT2730S">
          <meta property="datePublished" content="March 2025">
          <meta property="url" content="https://example.com/ep-12">
        </li>
      </ol>
    </section>
    podcast

    Design Systems Weekly

    1. 1Token Architecture
      Tech Weekly38:15February 2025
    2. 2Component Libraries at Scale
      Tech Weekly45:30March 2025

      Why component libraries stall at the second team, and what to do about it.

    <section class="rf-playlist rf-playlist--podcast" typeof="PodcastSeries" data-schema-lists="[&quot;hasPart&quot;]" data-type="podcast" data-media-position="top" data-artist="Tech Weekly" data-elevation="flat" data-rune="playlist" data-density="full">
      <div data-name="content" class="rf-playlist__content">
        <div data-name="eyebrow" data-zone="eyebrow" data-zone-layout="bar" class="rf-playlist__eyebrow">
          <span data-meta-type="category" data-field="type">podcast</span>
        </div>
        <header data-name="preamble" class="rf-playlist__preamble" data-section="preamble">
          <h1 id="design-systems-weekly" data-name="headline" property="name" class="rf-playlist__headline" data-section="title">Design Systems Weekly</h1>
        </header>
        <ol data-name="tracks" data-sequence="numbered" class="rf-playlist__tracks">
          <li data-field="track" typeof="PodcastEpisode" property="hasPart" data-meta="row" data-artist="Tech Weekly" data-duration="PT2295S" data-date="February 2025" data-elevation="flush" class="rf-track" data-rune="track" data-density="full">
            <span data-name="ordinal" data-meta-type="quantity" class="rf-track__ordinal">1</span>
            <span data-name="track-name" property="name" class="rf-track__track-name">Token Architecture</span>
            <div data-name="details" data-zone="details" data-zone-layout="bar" class="rf-track__details">
              <span data-field="artist">Tech Weekly</span>
              <span data-meta-type="quantity" data-align="end" data-field="duration">38:15</span>
              <span data-meta-type="temporal" data-field="date">February 2025</span>
            </div>
            <meta data-name="artist" content="Tech Weekly" class="rf-track__artist" />
            <meta data-name="duration" content="PT2295S" property="duration" class="rf-track__duration" />
            <meta data-name="date" content="February 2025" property="datePublished" class="rf-track__date" />
          </li>
          <li class="rf-track" typeof="PodcastEpisode" data-field="track" property="hasPart" data-meta="row" data-artist="Tech Weekly" data-duration="PT2730S" data-date="March 2025" data-elevation="flush" data-rune="track" data-density="full">
            <span data-name="ordinal" data-meta-type="quantity" class="rf-track__ordinal">2</span>
            <span data-name="track-name" data-field="name" property="name" class="rf-track__track-name">Component Libraries at Scale</span>
            <div data-name="details" data-zone="details" data-zone-layout="bar" class="rf-track__details">
              <span data-field="artist">Tech Weekly</span>
              <span data-meta-type="quantity" data-align="end" data-field="duration">45:30</span>
              <span data-meta-type="temporal" data-field="date">March 2025</span>
            </div>
            <div data-name="track-description" class="rf-track__track-description">
              <p>Why component libraries stall at the second team, and what to do about it.</p>
            </div>
            <meta content="Tech Weekly" data-name="artist" class="rf-track__artist" />
            <meta property="duration" content="PT2730S" />
            <meta property="datePublished" content="March 2025" />
            <meta property="url" content="https://example.com/ep-12" />
          </li>
        </ol>
      </div>
    </section>

    Both forms can be mixed, and document order is preserved. Two things worth knowing:

    • Tracks must be consecutive. The track listing is collected as one run, so prose written between two tracks ends it — anything after that becomes the playlist's body rather than another track.
    • An explicit type always wins. A {% track type="song" %} inside a podcast stays a song. The attribute means what it says; the playlist only supplies a kind when the track states none.

    Attributes

    AttributeTypeRequiredDescription
    srcstring—URL of the audio or video file for this track.
    artiststring—Performer or creator name for this track.
    durationstring—Track length in m:ss, h:mm:ss, or ISO 8601 duration format.
    numbernumber—Position number of the track in its parent listing.
    datestring—Publication or release date for the track.
    urlstring—External link to the track on a streaming platform or website.
    type"song" | "episode" | "chapter" | "talk" | "video"—Media category: song, episode, chapter, talk, or video.
    meta"row" | "byline"—Where the artist, date and running time sit: in the row beside the name (`row`), or beneath it (`byline`). A playlist passes its own to its tracks.

    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