Skip to content

renderer.protectHtml ​

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.

Stores trusted generated HTML behind a temporary token while Markdown renders surrounding text. Call it only from the legacy path when your rule has already generated trusted HTML.

Signature ​

ts
renderer.protectHtml(
    namespace: string,
    html: string,
    mode?: 'block' | 'inline'
): string

Parameters ​

ParameterDescription
namespaceShort diagnostic token category such as badge or math
htmlTrusted HTML generated by the rule
modeRestoration behavior; defaults to block

Returns ​

A temporary text token. Return or embed the token in transformed rule text; do not expose it to users directly.

The returned token belongs to the current block's protection manager. Do not cache it across blocks or preview updates.

Call relationships ​

  • Called by: legacy PreprocessRule.apply callbacks and helpers such as renderMath.
  • Calls: ProtectionManager.protect.
  • Resolved after: Markdown-it finishes rendering the block.
text
escaped/generated HTML -> protectHtml -> temporary token -> Markdown -> restored HTML

Choosing a mode ​

  • Use inline for a fragment that belongs inside a paragraph.
  • Use the default block mode for complete block-level HTML such as a table or figure.
ts
return renderer.protectHtml(
    'badge',
    `<span class="badge">${escapeHtml(content)}</span>`,
    'inline'
);

Escape user-controlled values before interpolating them. protectHtml prevents Markdown transformation; it is not an HTML sanitizer.

See also ​

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