FrameworkJavaScript
Equation tool: inline LaTeX math
Renders inline math (LaTeX) with KaTeX. Activated with Cmd/Ctrl+Shift+E — wraps the selected text, or a formula typed into the popover input, in a `<span data-latex="...">`. The `data-latex` attribute is the formula: the KaTeX markup is derived from it, is stripped on save, and is regenerated whenever the block renders (load, paste, undo). Read-only surfaces that never mount an editor — `blocksToHtml` / `<BlokView>` — display the source instead; pass an `inlineRenderers` entry to render the math there too.
Import
TypeScript
import { Equation } from '@bloklabs/core/tools';Save Data
TypeScript
// Stored as HTML inside the block's text field. The data-latex attribute is
// the formula; the span's text is normalized to that same source on save, so
// nothing derived from KaTeX's rendering is persisted (which is why plain-text
// previews and search indexes read the formula, not its rendered fragments).
// '<span data-latex="E = mc^2">E = mc^2</span>'JSON
// Render the math on a DOM-free view surface by plugging KaTeX in:
// blocksToHtml(data, {
// inlineRenderers: {
// span: ({ attrs }) => attrs['data-latex'] === undefined
// ? undefined
// : katex.renderToString(attrs['data-latex'], { throwOnError: false }),
// },
// })
{
"type": "paragraph",
"data": {
"text": "Einstein wrote <span data-latex=\"E = mc^2\">E = mc^2</span>"
}
}Usage Example
TypeScript
import { Blok } from '@bloklabs/core';
import { Equation, Paragraph } from '@bloklabs/core/tools';
const editor = new Blok({
holder: 'editor',
tools: {
// Paragraph is the default block and is not bundled with the core —
// without it the editor starts with an "unsupported block" stub.
paragraph: Paragraph,
equation: Equation,
},
});