Skip to content

AstRenderRule ​

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.

Checks and renders the current AST node. You implement this callback; the AST walker supplies both arguments and consumes its result.

Signature ​

ts
type AstRenderRule = (
    input: AstRenderInput,
    context: AstRenderContext
) => AstRenderResult | undefined

Returns ​

{ html, consumedNodes? } to claim the node, or undefined to let later rules try. consumedNodes defaults to 1.

html is final preview HTML. consumedNodes counts sibling-list entries beginning at input.index, including the current node; it is not the number of child nodes rendered.

Call relationships ​

  • Called by: the AST walker for each rule in registry order until one returns a result.
  • Usually calls: readAstCommandArguments, input.renderChildren, or an AstRenderContext method.
  • Output goes directly to: preview HTML; it does not pass through Markdown.
text
node -> rule(input, context) -> result: append HTML and advance walker
                            -> undefined: try the next rule
ts
const RULE = defineAstRenderRule((input, context) => {
    if (!isMacroNode(input.node, 'badge')) {
        return undefined;
    }
    const args = readAstCommandArguments(input, 1);
    const content = args.requiredArgs[0];
    return content === undefined ? undefined : {
        html: `<span>${context.escapeHtml(content)}</span>`,
        consumedNodes: args.consumedNodes
    };
});

Always escape plain source values or render existing AST children. Do not use legacy protection tokens in AST output.

See also ​

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