Skip to content

Rule API Reference ​

This section documents the supported extension surface assembled through src/rules.ts. Use it as a lookup after choosing an extension point; the Rendering Rules Tutorial is the guided starting point.

These are contributor APIs compiled into SnapTeX, not commands imported by a .tex project and not a published npm plugin surface. Read Source API Scope first for exact code location, import paths, callback ownership, output safety, and the minimum test harness. Then use Call Relationships to see where an API runs.

Find the API by task ​

GoalStart here
Understand where API code belongs and who supplies callback valuesSource API Scope
Assemble a complete custom registrydefineRuleRegistry
Add a source-text rendering ruleLegacy rule contract
Add a structural AST rendering ruleAST rule contract
Handle a selected command nested inside AST mathdefineAstMathRule
Read balanced LaTeX commands or groupsreplaceLatexCommandCalls or readLatexCommandAt
Produce safe inline HTMLrenderInlineLatexHtml
Extract custom preamble metadataMetadata contract
Re-render a block when external state changesBlockDependencyRule
Change block splitting behaviorSplitter contract
Test a registry through the real document pipelinePreviewUpdateService

Recognize who owns a function ​

The spelling of a signature tells you how it reaches your code:

Signature shapeWhat you doWho supplies the arguments
function escapeHtml(text)Import and call the helperYour code supplies every argument
apply(text, renderer)Implement this property on a PreprocessRuleSmartRenderer calls it
(input, context) => resultPass this function to defineAstRenderRuleThe AST walker calls it
apply(input, context) on AstMathRulePass the object to defineAstMathRuleThe AST math renderer calls it for matching commands
renderer.protectHtml(...)Call a method on the received legacy contextSmartRenderer created renderer
input.renderChildren(...)Call a method on the received AST inputThe AST walker created input
context.renderMath(...)Call a method on the received AST contextThe AST renderer created context

A declaration helper such as defineAstRenderRule(...) provides typing but does not register or execute the rule. Execution starts only after the returned callback appears in the matching SNAP_TEX_RULES array.

Follow values through the pipeline ​

text
src/rules.ts declaration
  -> SNAP_TEX_RULES registration
  -> parser/renderer calls your callback
  -> callback uses imported helpers and injected context methods
  -> callback returns transformed source, HTML, metadata, or dependencies
  -> the owning service consumes that return value

Each API page identifies the caller and the next consumer under Call relationships. This distinction matters: returning HTML from an AST rule is final, while returning HTML-looking text from a legacy rule is unsafe unless it has first passed through renderer.protectHtml.

API categories ​

  • Registry functions assemble and type-check extension definitions.
  • Legacy context exposes output protection, inline rendering, and citation state to PreprocessRule.
  • Source readers safely read balanced LaTeX syntax without ad hoc regular expressions.
  • Rendering functions escape output and render inline LaTeX or KaTeX.
  • AST functions inspect structural nodes and render their source or children.
  • Metadata and dependency functions extract document state and declare block invalidation inputs.
  • Testing API exercises a registry through parsing, diffing, scanning, and rendering.

Function index ​

CategoryFunctions and methods
RegistrydefineRuleRegistry, defineAstRenderRule, defineAstMathRule, defineBlockDependencyRule
Legacyapply, protectHtml, renderInline, resolveCitation, getCitedKeys
SourcereplaceLatexCommandCalls, readLatexGroup, readLatexCommandAt, skipLatexWhitespace, stripLatexComments
RenderingescapeHtml, renderMath, renderInlineLatexHtml
ASTrender, readAstCommandArguments, node readers, context methods
Metadata and dependenciesextract, readMetadataCommand, collect, deps.metadata, deps.citedKeys
TestingPreviewUpdateService, render, renderBlockByIndex, query methods

Supported boundary ​

The reference intentionally covers the APIs used to extend SNAP_TEX_RULES. Internal exports used only by SnapTeX implementation modules are not presented as stable extension contracts.

The legacy and AST backends are independent execution paths. A custom rule may target either backend; developers are not required to implement the same feature twice.

If an exported helper is absent from this reference, treat it as internal implementation rather than a supported extension contract. Internal APIs may still be reused by built-in modules, but extension documentation and compatibility promises do not follow them automatically.

Definition locations ​

AreaSource
Registry and default rulessrc/rules.ts
Shared rule typessrc/types.ts
Balanced source readerssrc/utils.ts
Rendering helperssrc/rule-helpers.ts
AST rule contractssrc/ast/rules/index.ts
AST node readerssrc/ast/visit-utils.ts
Metadata extractionsrc/metadata.ts
End-to-end update servicesrc/preview-update-service.ts

Continue with the Rendering Rules Tutorial for a guided implementation.

A fast structural preview, not a replacement for a full TeX compiler.