Wit

Derived content

Some content is computed from the document itself — a reading time, a "2 of 3 supported" badge, a list sorted by a key. <% … %> runs JavaScript against the expanded document through an lh object. The full API is in Scripting & the lh bridge.

Inline values

An inline <% expr %> inserts the expression's value, inside node bodies too:

#findings: [
  { claim: Tides follow the moon, supported: true }
  { claim: Gulls predict storms, supported: false }
  { claim: Fog lifts by noon, supported: true }
] !!

<% lh.data.findings.filter((f) => f.supported).length %> of <% lh.data.findings.length %> claims supported.

That renders 2 of 3 claims supported. lh.data gives plain JavaScript values: numbers, real booleans, strings, arrays, and records whose keys match loosely (lh.data.paper.word_target reaches word target).

Word count and reading time

lh.prose().wordCount() counts the document's prose; lh.inject(id, source) fills the node whose id matches with new Wit:

<%
  const words = lh.prose().wordCount();
  lh.inject('stats', `${words} words · ${Math.ceil(words / 200)} min read`);
%>

(p(id: stats) p)

The keeper trimmed the wick at dusk and logged the weather before the tide.
<p id="stats">14 words · 1 min read</p>
<p>The keeper trimmed the wick at dusk and logged the weather before the tide.</p>

Sorting

lh.sort(kind, compare) reorders siblings of one AST kind — for node uses, 'nodeUse' — comparing on their parameters:

<%
  const key = (n) => n.params.find((p) => p.name === 'id').value;
  lh.sort('nodeUse', (a, b) => key(a).localeCompare(key(b)));
%>

@ul
(li(id: c) Fog lifts by noon li)
(li(id: a) Tides follow the moon li)
(li(id: b) Gulls predict storms li)
ul@
<ul><li id="a">Tides follow the moon</li><li id="b">Gulls predict storms</li><li id="c">Fog lifts by noon</li></ul>

Limits

  • Scripts are trusted input: there is no sandbox. Never run them on documents you did not write.
  • A script that throws is E_SCRIPT_ERROR.
  • Script output is plain text or freshly parsed Wit; renderers never see the script itself.

See also

Scripting & the lh bridge · Composing constructs · Self-organising documents · Faceted content