Wit

Derived content

Some content is computed from the document itself — a word count, a reading time, findings sorted by strength, a "3 of 7 done" badge. Wit's script bridge, <% %>, post-processes a rendered document through an lh object. This page is a cookbook; the full lh API lives in the Build track.

The surface to rely on

These members are stable enough to build on:

  • lh.data — the document's data definitions.
  • lh.query(name) — nodes of a given kind.
  • lh.sort(name, fn) — reorder a collection.
  • lh.inject(id, src) — insert rendered content at a marker.
  • lh.prose().wordCount() — a word count of the prose.
  • A node exposes params and content.

Recipes

Word count and reading time:
<%
  const words = lh.prose().wordCount();
  lh.inject(&#39;stats&#39;, `${words} words · ${Math.ceil(words / 200)} min read`);
%>
Sort findings by strength, then a "X of Y" badge:
<%
  lh.sort(&#39;findings&#39;, (a, b) => b.strength - a.strength);
  const done = lh.data.findings.filter(f => f.supported === &#39;true&#39;).length;
  lh.inject(&#39;badge&#39;, `${done} of ${lh.data.findings.length} supported`);
%>

Note the boolean-as-string quirk: a value typed as a boolean compares as the string 'true' here, so write f.supported === 'true'. Other recipes follow the same shape — group items by an owner field, or build a table of contents from lh.query('h2').

Honesty about the surface

Some members are renderer-specific or unverified — check them against your renderer before relying on them: lh.target (per-target dispatch), node.params.injectId and backrefsInjectId, and a heading's kind. Treat these as "verify in your build".

The injection seam that errors

A script that injects rendered content must supply balanced nodes. A half-open node — a bare opener with no matching closer — collides with the surrounding body's close and raises E_MISMATCHED_CLOSE.

Common mistakes

  • Reaching for a script when (each) over data would do.
  • Relying on unverified lh members without checking your renderer.
  • Injecting a half-open node (balance the open and close).

See also

Composing constructs

· Literal & raw nodes · Self-organising documents · Faceted content