Skip to main content
The core package has no dependencies and knows nothing about any particular programming language. It defines the contracts, creates narrators and builds diff reports.

createNarrator

SourceLanguage[]
required
The source languages this narrator can read. For each file, the first language whose handles(path) returns true is used.
Locale
required
The output language, for example en from @usenarrator/locale-en.
DependencyIndex
The repository’s dependency manifests, from createDependencyIndex or loadDependencyIndex in @usenarrator/node. For each file, the narrator passes its language the dependencies of the file’s package in that language’s ecosystems. Without it, plugins only see imports. See Dependency detection.

Narrator

boolean
Whether any configured language handles this path.
Narration
Narrates one file and returns { lines, errors, stats, problems }. errors counts syntax errors, and problems describes them along with failed plugin rules. If the language itself throws, narrate returns no lines and one narrator problem instead of throwing. It throws only when no language handles the path, so check canNarrate first when paths come from users.
FileNarrator
A fresh narrator for one file, with access to units(src, path) for diffing.
FileJson[]
Narrates both sides of each changed file, diffs them by unit, detects moves and renames across files, and returns HTML-ready rows. Files no language handles are skipped, and files whose English didn’t change are left out. opts.resolve turns function and value references into links. See Units and diffs.Generated, vendored, minified and build files are not narrated. They are returned with skipped set to the reason and no units, so a report can still list them. Pass includeGenerated: true to narrate them anyway. A file that can’t be narrated is returned with no units and a narrator problem, and the rest of the change set is unaffected. When the new side has a syntax error, declarations that only seem to be missing past the error are not reported as removed.
FileDiff[]
The same change set as diff, as structured units whose rows hold Line objects rather than HTML, with the same skipped and problems fields. Render it with renderDiffText.

renderDiffText

Prints structured diffs as indented text: a header per file, a line per changed unit with its status, then each sentence marked -, + or left unmarked when unchanged. Skipped files get a single header line such as dist/app.js [modified, skipped: build output], and problems are printed under their file’s header with a leading !. format defaults to plain. Pass toAnsi for colour.

Problems

NarrationProblem

severity is error when part of the file is missing from the output and warning when a fallback reading was used. describeProblem turns a problem into one English sentence, the way the CLI prints it.

Generated files

narrator.diff and narrator.diffFiles use these to decide what to skip. Pass paths relative to the project root, since a folder named build above the project would otherwise count. .gitattributes needs file system access, so it lives in @usenarrator/node as gitAttributeSkips and changedFilesFromGit.

Dependencies

See Dependency detection.

Lines

Line.d is the indent depth. start and end are source offsets, or -1 for lines that don’t map to code. text contains markup. Doc is what rules return: inline text t and the block lines kids it introduces. Create one with D(t, kids?), which also strips a trailing colon from t when there are kids, because the colon is added when the block is emitted.

Markup

Contracts

See Adding a source language and the Locale reference.

Diff primitives

These are what narrator.diff and narrator.diffFiles are built on.
detectMoves updates the unit diffs in place, marking matched pairs as moved, renamed or moved-away.

Lexicon

Lexicon reads identifiers into words. Each engine has one, extended by the vocabulary of every plugin.