API reference
The exact public surface for embedders. Only the index.ts re-exports
listed here are public; everything else is package-private. Pipeline for an
embed: parse → loadExternalData → resolve → expand → renderHtml.
witlang/parser
Package @witlang/parser.
parse(source: string, file?: string): Document
parseInlineFromText(...): Inline[] // inline sub-parser
tryParseRecordFromText(...): Record | null
tryParseCollectionFromText(...): Collection | null
format(source: string, opts?: FormatOptions): string
FormatOptions { indent: string } // default ' '
class WitError { code; loc; message }parseInlineFromText re-parses captured raw values at substitution
time; the two tryParse… scanners parse late-bound record and
collection text.
Document · Block · Inline · AstNode ·
Paragraph · Comment · NodeUse · NodeDef · DataDef · Record · Collection ·
IfStatement · EachStatement · ScriptBlock · ScriptCall · ReferenceDirective ·
Text · Italic · Bold · Interpolation · BodySlot · DataValue · StringValue ·
NumberValue · BooleanValue · NullValue · Condition · ExistenceCondition ·
ComparisonCondition · Param · AccessPath · Loc · HasLoc.
witlang/runtime
Package @witlang/runtime.
resolve(doc: Document, options?: ResolveOptions): ResolvedDocument
ResolveOptions { rootPath?; fileReader?; onMissingReference? }
expand(resolved: ResolvedDocument): ExpandedDocument
loadExternalData(doc: Document, source: DataSource): Document
DataSource = DataLoader | Record<string, unknown>
toDataValue(value: unknown, loc: Loc): DataValue
class RuntimeError / ResolverError / ExpanderError
RuntimeErrorCode // the code constants
CORE_VOCAB_NAMES / RESERVED_OPAQUE
isCoreVocabName(name) / isReservedNodeName(name)loadExternalData is the load pass; a plain { alias: value } dictionary is the simplest embedded DataSource. resolve throws ResolverError; expand throws ExpanderError.
DataLoader · DataLoadRequest ·
DataSource · ResolvedDocument · ExpandedDocument · ResolveOptions · FileReader ·
RuntimeErrorCodeName.
witlang/render-html
Package @witlang/render-html.
renderHtml(doc: ExpandedDocument, options?: RenderHtmlOptions): string
RenderHtmlOptions {
mode?: 'fragment' | 'document' // default 'fragment'
title?: string // default 'Wit document'
lang?: string // default 'en'
css?: string // default defaultThemeCss; '' = unstyled
}
escapeHtml(s: string): string
defaultThemeCss / rawThemeCss // string constantsrenderHtml defaults to fragment mode — the CLI's --raw / --fragment pathways select mode and CSS on top of
this.
witlang/render-markdown
Package @witlang/render-markdown. One export:
renderMarkdown(doc: ExpandedDocument): string
End-to-end embed
import { parse } from '@witlang/parser';
import { loadExternalData, resolve, expand } from '@witlang/runtime';
import { renderHtml } from '@witlang/render-html';
const doc = parse(source, 'doc.wit');
const loaded = loadExternalData(doc, { sales: [{ region: 'West', total: 100 }] });
const html = renderHtml(expand(resolve(loaded)), { mode: 'document' });See also
CLI· Data model · Errors · Embedding guide.