0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Enhance code elements with precompiled MathML rendering. Add data-math to any code element for automatic MathML injection with numbering, copy, and Markdown integration.
Overview
The data-mathattribute enhances <code> elements with precompiled MathML. The original LaTeX source stays readable without JavaScript, and when JS runs, it is replaced by rendered MathML.
Block math with precompiled MathML
1234
<codedata-mathdata-mathml="<math display='block'><mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></math>"> x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}</code>
How It Works
Add data-math to a <code> element with LaTeX source as its text content
Add data-mathml with the precompiled MathML string
On page load, the script reads data-mathml, injects a <math> element, and hides the original <code>
Without data-mathml, or in a browser that cannot render MathML, the LaTeX source displays as-is — readable fallback with no JS dependency
The rendered <math> gets alttext set to the LaTeX source (unless your MathML already has one)
The data-mathml string is sanitized before insertion: only MathML elements survive, and event handlers, href, src, style, id, name and xlink:/xml: attributes are removed. Treat it as author-controlled markup all the same — generate it at build time, not from user input.
Space-separated tokens: inline, numbered, copyable. An empty value defaults to block rendering.
data-mathml
string
Precompiled MathML string to inject. Without this, the element shows the raw LaTeX source.
data-label-copy
string
Copy button text for copyable. Default "Copy LaTeX". Read from the element or its nearest ancestor that sets it, so one attribute on <article> localises every equation.
data-label-copied
string
Announced (and shown on the button for two seconds) after a successful copy. Default "Copied to clipboard".
data-label-copy-failed
string
Announced (and shown on the button) when the clipboard refuses. Default "Copy failed".
data-math-state
string
Set automatically: "rendered" when MathML injected, "source" when showing raw LaTeX.
data-math-init
boolean
State hook, set once the element is enhanced. Do not set it. Enhancement is tracked internally, so a cloned equation is adopted (not rendered twice) and its copy button works.
Tokens
Token
Description
inline
Renders inline math, wrapped in a <span> instead of a block wrapper.
numbered
Displays an equation number. Numbers follow document order and are renumbered when equations are added or removed. Equations with an id are listed in window.VBMath.equations ({ id, number, latex }, rebuilt in document order).
copyable
Adds a Copy LaTeX<button> after the rendered math. Activating it copies the original LaTeX source and announces the result.
Inline Math
Use the inline token for math that flows within paragraph text.
Inline math
123456
<p>The area is<codedata-math="inline"data-mathml="<math><mi>π</mi><msup><mi>r</mi><mn>2</mn></msup></math>"> \pi r^2</code>.</p>
Numbered Equations
Combine numbered with an id for linkable, numbered equations.
Numbered equation
123456
<codedata-math="numbered"id="eq-energy"data-mathml="<math display='block'><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></math>"> E = mc^2</code><p>See <ahref="#eq-energy">Equation 1</a>.</p>
Copyable Math
The copyable token adds a Copy LaTeX button after the rendered math. Activating it (click, Enter or Space) copies the LaTeX source to the clipboard, announces the result through VB's shared live region and shows it on the button for two seconds. A refused copy (no clipboard permission, insecure context) is announced too. Localise the texts with data-label-copy, data-label-copied and data-label-copy-failed on the element or an ancestor.
Markdown processors emit code.language-math for fenced math blocks. The enhancement script auto-detects these — no extra configuration needed.
Markdown source
123
```mathx = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}```
This produces the standard HTML output:
Emitted HTML
123
<pre><codeclass="language-math"> x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}</code></pre>
The code.language-math selector is matched automatically alongside [data-math]. For precompiled MathML, add data-mathml to the element at build time via a Remark or Markdown-it plugin.
Styling
Hook
Description
[data-math-rendered]
The wrapper holding the rendered <math> (a block wrapper, or a <span> for inline). It takes the source's id, so :target highlights a linked equation.
[data-math-numbered]
Numbered wrapper; a grid with the number in ::after.
[data-equation-number]
The equation's number on the wrapper (read by attr() in ::after).
[data-math-copyable], button[data-math-copy]
Copyable wrapper and its button. The button carries data-state="copied" or "failed" for two seconds after use.
[data-math-state="source"]
The fallback: source shown as code.
Events
Event
Detail
Description
math:rendered
{ latex, inline, number }
Fired on the source element after MathML is injected. latex is the source text, inline is a boolean, number is the equation number at that moment (or null); a later insert above it renumbers the wrapper.
Listening for math:rendered
1234567
const el = document.querySelector('[data-math]');el.addEventListener('math:rendered', (e) => {console.log('LaTeX:', e.detail.latex);console.log('Inline:', e.detail.inline);console.log('Number:', e.detail.number);});
Progressive Enhancement
Without data-mathml, the LaTeX source displays as a readable <code> block. This is the no-JS fallback:
No-JS fallback
12
<codedata-math>E = mc^2</code><!-- Without data-mathml, the LaTeX source displays as readable code -->
The element receives data-math-state="source" so you can style the fallback state differently if needed.
Dynamic Content
Elements added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization needed.
The injected <math> is read as math by screen readers with MathML support (VoiceOver; NVDA and JAWS with a math add-on such as MathCAT). Support elsewhere, notably TalkBack, is limited.
For those, the <math> carries alttext with the LaTeX source. Where LaTeX is a poor spoken form, put a plain-language alttext in your MathML (it is kept), or describe the equation in the surrounding text.
The original LaTeX source stays in the DOM, hidden
Copy results are announced through VB's shared role="status" live region (a refused copy through the assertive one), placed outside the button so the button's name does not change for assistive tech mid-announcement
Numbered equations are linkable via id and standard <a href>; the number is drawn in CSS, so write "Equation 1" in your link text
Related
<math> — MathML itself, its styling and the math font.
<code> — the element that carries the LaTeX source.
data-copy — copy buttons for any text. copyable is the math-specific version that copies the LaTeX behind a rendered equation.