Skip to content

API Reference

The <reast-engine> custom element drives the full pipeline and exposes its state and controls as attributes, properties, methods and events.

Attributes

AttributeTypeDescription
srcstringURL to a .reast archive — fetched, extracted and rendered.
contentstringRaw .rea text — parsed and rendered inline.
speednone | fast | medium | slowTypewriter speed. Default medium.
story-idstringStable story identifier used in exported state. Falls back to src.
localestringBCP-47 locale for i18n/date built-ins.
media-controlsnative | noneWhether <video>/<audio> render native controls (native, default) or become host-handled activation targets (none).

src, content, locale and media-controls are observed and re-render on change; speed and story-id are read on demand. Locale resolution order is: the locale attribute → the story's metadata language → the host page's <html lang>en.

Properties

PropertyTypeAccessDescription
documentReaDocument | nullget/setThe parsed story. Setting an already-parsed document renders it without re-parsing — the load path for a host that parses first (consent gates, SEO, offline caches). Set extensions and mediaMap beforehand for archive stories. Setting null clears the element.
manifestReastManifest | nullget/setManifest for a bundle the host loaded itself, so chapters/metadata report as they do for a src load. Set before document.
extensionsExtensionModules | nullsetCompiled .rext modules for a host-loaded bundle, so {use "extensions/…"} resolves. Set before document.
mediaMapMap<string,string> | nullget/setArchive-relative path → blob/object URL. Wins over the element's own loader map. The host owns the URLs — the element never revokes them.
runtimeStoryEngine | nullgetThe story runtime — an advanced read surface for host chrome (variables, cards, condition evaluation). Do not cache: it is swapped on revert/restart/reload.
contentRootHTMLElementgetThe shadow container holding rendered blocks, in flow order (scroll math).
metadataStoryMetadata | nullgetTitle, authors, genre, language, chapters, reading-time estimate, choice count.
chaptersreadonly ChapterInfo[]getChapters in play order. Empty for flat/chapterless stories.
currentChapterChapterInfo | nullgetThe chapter currently rendered.
waitingFor'choice' | 'external-event' | nullgetWhat the story is blocked on.
completedbooleangetWhether the story reached its end.
renderedParagraph{ chapter, paragraph } | nullgetLast block the reader has seen rendered.
revealedNodesreadonly RevealedNode[]getThe visible flow: each node's identity plus whether its block is revealed yet. Read surface for tables of contents and progress UIs.
choicesreadonly PendingChoice[]getThe pending choice group with its visible options. Option indices match selectChoice.
highlightsHighlightManagergetSearch/annotation mark surface, re-anchored automatically on re-render.
unlockCodesRecord<string,string>getUnlock codes held for protected content (defensive copy).
localestringget/setEffective locale (see above). Setting rebuilds the runtime.

Methods

MethodDescription
load(url)Load a story from a .reast archive URL.
render(text)Parse and render raw .rea text.
importVariables(vars)Merge variables into the runtime and re-sync conditional content.
updateLocation(position)Feed a GeoPosition (or null) back after a location-start event; drives world.location.*.
selectChoice(nodeId, optionIndex)Select an option programmatically. optionIndex addresses the visible options — the same numbering choices reports, so a hidden conditional option never shifts it. Returns false if the group is gone, already selected, or the index is out of range.
blockAt(index)The rendered block element at a container index (the numbering rea-progress uses), or null.
revertChoice(nodeId)Revert a choice group: clears its selection and everything after, replays the kept selections, rewinds the flow.
restart()Restart from the beginning, clearing all state.
exportState()Portable, JSON-serializable reading state (selections, variables, last rendered block, unlock codes), or null.
importState(state)Restore exported state, rendering read content instantly and resuming the reveal beyond it. May be called before a story loads.
setUnlockCodes(codes)Provide unlock codes for protected content, keyed by content id.
use(ext)Register a host extension on this instance (see Extending).
unuse(name)Remove a host extension by name. Returns whether one was registered.

Events

All events are CustomEvent instances that bubble.

EventDetailWhen
rea-loadedStory loaded and rendered.
rea-metadataStoryMetadataStory metadata resolved.
rea-chapterChapterInfoCurrent chapter changed.
rea-waiting{ waiting }Waiting state changed.
rea-choice{ nodeId, index }Reader selected a choice.
rea-undo{ nodeId }Reader reverted a choice.
rea-progress{ chapter, paragraph }A block finished revealing.
rea-completeStory reached its end.
rea-error{ message }A load/parse/runtime error occurred.
rea-media-activate{ kind, src, path, alt }Reader activated a story media element.

rea-media-activate

Cancelable and composed. kind is 'image' | 'video' | 'audio'; src is a renderable URL (a blob: URL for archive media); path is the original archive-relative path (name a download, or look the entry up in the manifest); alt is the author's alternative text. Images always fire; video/audio only under media-controls="none". The engine renders no overlay — it only reports the activation. Only the standalone CDN build ships a default lightbox (registered through this same event); an embedding host opens its own viewer and calls preventDefault().

Slots

Host chrome is injected through named slots: before-identity, after-identity, before-chapter, after-chapter, story-end.

Module exports worth naming

From @reast/engine (and, where noted, @reast/engine/player):

ExportDescription
collectMedia(doc)Every media reference in document order, deduplicated by path — including media nested in choice branches, state machines and card hooks. Also on /player.
resolveMediaPath(mediaMap, path)Resolve one media path through a media map, without mutating the AST.
compileExtensions(files, manifest)Compile and validate the .rext modules of an archive (plus std/*). Also on @reast/engine/loader.
haversineDistance, bearing, isWithinRadiusGreat-circle geo helpers.
isInExclusionZone, isInAnyExclusionZone, isGeoTriggerAllowedExclusion-zone gating for location triggers.

Subpath exports

ExportDescription
@reast/engineMain barrel — Node-safe (parser, loader, runtime, errors, types, collectMedia, geo).
@reast/engine/parserRea lexer + parser.
@reast/engine/loader.reast archive loader (extraction, decryption, manifest, compileExtensions).
@reast/engine/runtimeStoryEngine, expression evaluator, state manager.
@reast/engine/player<reast-engine> web component and host-extension types (browser-only).
@reast/engine/geoGeo-position utilities.
@reast/engine/errorsError classes and codes.
@reast/engine/typesTypeScript type definitions.
@reast/engine/validatorvalidateStory.
@reast/engine/debugDebugStepper.

Note: @reast/engine/player uses browser APIs (CSSStyleSheet, HTMLElement). Import it only in browser environments.