Skip to content

Legacy Rule Contract ​

How to read this API

These are TypeScript APIs compiled into SnapTeX; a .tex project cannot load them at runtime.

  • function name(...) is a helper that your rule imports and calls.
  • apply(...), render(...), extract(...), and collect(...) are callbacks that your rule implements and SnapTeX calls.
  • renderer.name(...), context.name(...), input.name(...), and deps.name(...) are methods SnapTeX supplies to callbacks; do not import or construct them.
  • service.name(...) is an instance method called after your host or test constructs PreviewUpdateService.

Register extension declarations through SNAP_TEX_RULES in src/rules.ts. See Source API Scope for imports, value ownership, output safety, rebuilds, and testing.

A PreprocessRule transforms one block of LaTeX source before Markdown rendering. Use it when the legacy backend can recognize the feature from source text without structural AST traversal.

ts
interface PreprocessRule {
    priority: number;
    apply(text: string, renderer: RenderContext): string;
}

Lifecycle ​

  1. defineRuleRegistry sorts legacy rules by ascending priority.
  2. SmartRenderer passes the current block through every apply callback.
  3. Markdown-it renders the transformed text with raw HTML disabled.
  4. SnapTeX restores HTML registered with renderer.protectHtml.

The callback's return value is therefore usually one of three forms:

Return shapeUse
Unchanged textThe rule does not apply
Transformed LaTeX/Markdown-compatible textA later rule or Markdown should continue processing it
Text containing protected tokensThe rule generated trusted HTML that must survive Markdown
ts
const RULE: PreprocessRule = {
    priority: 200,
    apply: (text, _renderer) => /* transformed text */ text
};

The _renderer parameter is present because SnapTeX always supplies the full apply(text, renderer) interface. Its underscore only says that this minimal example does not use the context.

RenderContext ​

MemberPurpose
currentMacrosCurrent KaTeX macro definitions
metadataParsed document metadata, when available
bibEntriesCurrent bibliography entries
PreprocessRule.applyTransform one block in the legacy rule sequence
protectHtmlHide trusted generated HTML from Markdown
renderInlineRun Markdown inline rendering
resolveCitationAssign or reuse a citation number
getCitedKeysRead citation keys seen so far

Legacy rules operate on source text and therefore commonly use the balanced source readers. They do not receive AST nodes and do not run in AST mode.

priority controls ordering; it does not select commands. Source matching belongs inside apply, normally through replaceLatexCommandCalls or another shared reader.

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