agentleFS
yangjingoyangjingo/Synapse/skills/synapse-pretext/SKILL.md

synapse-pretext

Skill4 starsChanged 3 months agoMITView on GitHub

Zero-DOM text measurement and manual line layout for Synapse outputs. Other skills delegate here when they need text geometry — height prediction, obstacle-aware reflow, shrinkwrap width, justified layout, or proportional character sizing. Use when animated text fields, geometry-sensitive titles, editorial flow, or ASCII-art rendering require precise layout without touching the DOM.

The file

Show its rules and text

We split this skill into the rules it gives an agent

17 rules. About 0 are common, 8 are distinctive, and 9 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 9

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    All files are self-contained single HTML.

    line 56
  2. Distinctive:

    Every pretext-powered blog shares these patterns.

    line 140
  3. Very specific:

    Full implementation: search for ⑥ 火柴人 in any blog reference.

    line 185
  4. Distinctive:

    When rendering blog HTML with figures, always read the closest reference template first and copy its complete figure pattern.

    line 195
  5. Very specific:

    When s.image points to a non-existent file, img.onerror shows a "待生成插图" placeholder while the prompt remains in section metadata for …

    line 215
  6. Very specific:

    Native vector diagram: use synapse-excalidraw for flow diagrams, architecture diagrams, and concept maps that should remain SVG/editable.

    line 220
  7. Very specific:

    Animation: use synapse-animation only when explicitly requested.

    line 221
  8. Very specific:

    Never route raster generation through Gemini, Imagen APIs, or Nano Banana.

    line 222
  9. Very specific:

    When a raster figure entry has no image file, invoke the Codex $imagegen skill in its default built-in image_gen tool mode and record …

    line 242
  10. Very specific:

    If built-in image_gen is unavailable or fails, report it instead of switching providers silently.

    line 243
  11. Distinctive:

    When a Remotion composition includes text that must reflow during animation, use pretext for per-frame text measurement instead of DOM …

    line 249
  12. Very specific:

    DO NOT run prettier — it breaks the inlined ~2000-line pretext library The full rendering orchestration (copy template → fill variables) is …

    line 292
  13. Very specific:

    Avoid system-ui font on macOS — use a named font.

    line 299
  14. Distinctive:

    Do not claim pretext usage unless the page is actually using pretext APIs.

    line 301
  15. Distinctive:

    All output using pretext must be self-contained — inline the library, do not reference external files.

    line 302
  16. Distinctive:

    Never trigger DOM reflow for text measurement.

    line 304
  17. Distinctive:

    If this fails → there are curly-quote delimiters or other syntax errors.

    line 319

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Synapse Pretext

The text geometry engine for Synapse. Other skills delegate here when they need to know where text will land — without triggering a reflow.

Pretext measures and positions multiline text through pure arithmetic. No getBoundingClientRect. No layout thrashing. No DOM dependency.

When to Use

  • Animated text that must know its height before the first paint
  • Title routing around obstacles (images, callouts, pull quotes)
  • Multi-column editorial flow with cursor handoff between columns
  • Shrinkwrap: the tightest width that preserves line count
  • Justified layout with Knuth-Plass optimization and river detection
  • Proportional-font ASCII art where character width varies per weight/style
  • Canvas/SVG text rendering where DOM measurement is impossible
  • Virtualized lists that need exact item heights upfront
  • Click-spawn emphasis characters (stick figures) on pretext-powered pages

When NOT to Use

  • Static single-line text — CSS handles this fine
  • Layout that doesn't depend on text geometry
  • Any page where DOM measurement is acceptable and text never moves

Read

Core References

  1. reference/the-editorial-engine.html — the definitive reference. Multi-column flow, obstacle-aware reflow, drop caps, pull quotes, adaptive headline sizing, 60fps animated text.
  2. reference/shrinkwrap-showdown.html — binary search for tightest width.
  3. reference/justification-comparison.html — Knuth-Plass optimal line breaking with hyphenation and river detection.
  4. reference/fluid-smoke.html — full-screen fluid ASCII animation with per-character width measurement.
  5. reference/variable-typographic-ascii.html — particle system mapped to characters by brightness and width.
  6. reference/index.html — landing page linking to all demos.

Blog Integration References

The blog template demonstrates the complete pretext integration pattern with all sub-features (meta, figures, references, stick figure).

  1. examples/dsv4-blog-layout.html — blog rendering template. Self-contained HTML with inlined pretext engine, adaptive title fitting, stick figure overlay, figure zoom, SECTIONS renderer. Cream background, orange accent, serif body.
  2. examples/dsv4-blog-opd-en.html — figure-heavy English example with six generated SVG charts. Both examples resolve assets from examples/figures/; refresh them with node skills/synapse-pretext/examples/generate-figures.js.

Main examples/ directory contains 4 fully rendered blog outputs built from this template:

  • cli-revolution/cli-revolution-blog.html — CLI Revolution (7 sections, 2 callouts)
  • deepseek-v4/deepseek-v4-blog.html — DeepSeek-V4 (12 sections, 2 figures, 3 callouts)
  • nano-cc/nano-cc-blog.html — Nano Claude Code (8 sections, 3 callouts)
  • opd/opd-blog.html — OPD On-Policy Distillation (12 sections, 7 editable SVG figures, 3 callouts)

All files are self-contained single HTML. Open directly in browser, no web server needed.

Feature → Reference Map

FeatureReferenceKey APIs
Obstacle-aware reflow around moving shapesthe-editorial-engine.htmlprepareWithSegments layoutNextLine cursor loop
Multi-column flow with cursor handoffthe-editorial-engine.htmllayoutNextLine cursor passing between columns
Adaptive headline sizing (binary search font size)the-editorial-engine.htmlprepareWithSegments + walkLineRanges in size loop
Drop caps and pull quotesthe-editorial-engine.htmlprepareWithSegments layoutWithLines rect obstacles
Shrinkwrap (tightest width, same line count)shrinkwrap-showdown.htmlprepareWithSegments walkLineRanges binary search
Knuth-Plass optimal justificationjustification-comparison.htmlprepareWithSegments walkLineRanges DP optimization
Full-screen fluid ASCII animationfluid-smoke.htmlprepareWithSegments per-character width
Particle system with brightness/width selectionvariable-typographic-ascii.htmlprepareWithSegments proportional measurement

Blog Integration Features

FeatureTemplateDetails
Adaptive title fittingdsv4-blog-layoutfitTitle() binary search, prepareWithSegments + layoutWithLines
Author info + tag badgedsv4-blog-layout.meta-tag accent badge + .meta-date author line
Reference section with hyperlinksdsv4-blog-layoutbodyHTML with <div><a> per reference
Click-spawn stick figuredsv4-blog-layoutCanvas overlay requestAnimationFrame bounce + wave
Figure integration + click-to-zoomdsv4-blog-layout.figure-block + max-height: 70vh + zoom overlay, prompt revealed only in zoom state
Missing image placeholderdsv4-blog-layoutimg.onerror → "待生成插图"; prompt text kept in section metadata and only shown in zoom state
SVG figure (Excalidraw)opd-blog7 SVG figures via synapse-excalidraw

Core API

Height prediction — prepare + layout

const prepared = prepare('AGI 春天到了. بدأت الرحلة 🚀', '16px Inter')
const { height, lineCount } = layout(prepared, 320, 20)

prepare() normalizes, segments, and measures with canvas. Returns an opaque handle. layout() is the hot path — pure arithmetic over cached widths. On resize, only rerun layout().

Manual line layout — prepareWithSegments + layoutWithLines

const prepared = prepareWithSegments(text, '18px "Helvetica Neue"')
const { lines } = layoutWithLines(prepared, 320, 26)
for (const line of lines) ctx.fillText(line.text, line.x, line.y)

Obstacle-aware flow — layoutNextLine cursor loop

let cursor = { segmentIndex: 0, graphemeIndex: 0 }
while (true) {
  const width = getWidthForY(cursor.y, obstacles)
  const line = layoutNextLine(prepared, cursor, width)
  if (line === null) break
  placeLine(line)
  cursor = line.end
}

Shrinkwrap — walkLineRanges binary search

let targetLines = 0
walkLineRanges(prepared, maxWidth, () => targetLines++)
let lo = 1, hi = widestLine
while (lo < hi) {
  const mid = (lo + hi) >>> 1
  let count = 0
  walkLineRanges(prepared, mid, () => count++)
  if (count <= targetLines) hi = mid; else lo = mid + 1
}
// lo is the tightest width

Character measurement — prepareWithSegments for single chars

const p = prepareWithSegments(ch, font)
const width = p.widths.length > 0 ? p.widths[0] : 0

Blog Integration Patterns

Every pretext-powered blog shares these patterns. Read any of the three blog references for a complete working implementation.

1. Page structure

<canvas id="stick-canvas"></canvas>     <!-- stick figure overlay -->
<div class="smoke-wrap">                <!-- smoke particles wrapper (optional) -->
  <canvas id="smoke-canvas"></canvas>
  <div class="page">
    <div class="meta-tag">TAG</div>
    <div class="meta-date">YYYY/MM/DD · author</div>
    <div id="title-wrap"></div>          <!-- pretext fitTitle() fills this -->
    <div id="lead-wrap"></div>           <!-- pretext measures, CSS renders -->
    <div class="hr"></div>
    <div id="sections-wrap"></div>       <!-- pretext measures, CSS renders -->
    <div id="pulse-wrap" class="pulse"></div>
  </div>
</div>

2. SECTIONS array

var SECTIONS = [
  { heading: "...", body: "..." },                                    // text section
  { heading: "...", body: "...", callout: "..." },                    // text + callout
  { image: "figures/file.svg", caption: "Fig N: ...", prompt: "..." }, // figure
  { heading: "Reference", body: "[1] ...", bodyHTML: '<div>...</div>' } // refs
];

3. Render loop

for (var si = 0; si < SECTIONS.length; si++) {
  var s = SECTIONS[si];
  if (s.image) { /* create figure block + img onerror placeholder + zoom click */ continue; }
  // heading + callout + body (with bodyHTML support for Reference section)
}

4. Stick figure

Canvas overlay, click-spawn at e.clientX, e.clientY, bounce physics, waving arm, blinking eyes, no auto-dismiss. All colors from --accent. Scale 0.65.

Full implementation: search for ⑥ 火柴人 in any blog reference.

5. Smoke particles (optional)

Mouse-following radial gradient particles on a fixed canvas. Emits from cursor + ambient from bottom edge. Accent color with low alpha for subtle atmosphere.

Implementation pattern: Fixed canvas overlay, requestAnimationFrame loop, emit particles at mouse position + bottom edge.

6. Figure implementation (copy from reference first)

When rendering blog HTML with figures, always read the closest reference template first and copy its complete figure pattern. Do not improvise.

Reference templates (in order of completeness):

  • examples/dsv4-blog-opd-en.html — most complete figure implementation
  • examples/dsv4-blog-layout.html — base template with figure support

The complete pattern has five pieces — all must exist:

#PieceLocationWhat to verify
1DSL Type: figure section.dsl.mdIndependent section with Image, Caption, Prompt fields (NOT inline inside Type: text)
2HTML figure section objectSECTIONS array{ image: \...`, caption: `...`, prompt: `...` }— noheadingorbody`
3Prompt sourceslides DSLExtract verbatim from slides DSL ::visual prompt text (Codex $imagegen workflow), never hand-write; prompts must include strict visual controls (#FAFAF7 paper, #4A4338 ink, sparse labels, one #C45A20 accent, no gradients/shadows/3D/decorative clutter) and align with current example DESIGN.md.
4CSS figure-prompt-vis class<style> blockKeep prompt element in DOM, hidden by default, visible in zoom
5Rendering code<script>promptEl created and appended to figBlock so prompt remains available in zoom interaction

What goes wrong when you skip this: figures end up as inline fields inside text sections, prompts are missing or hand-written, or prompts exist as data but are never rendered. Each has caused multi-round user corrections.

7. Figure → skill delegation

When s.image points to a non-existent file, img.onerror shows a "待生成插图" placeholder while the prompt remains in section metadata for traceability and is shown only when clicked/zoomed.

  1. Check s.prompt — describes what the image should contain
  2. Delegate:
    • Raster illustration(默认): invoke Codex $imagegen and use its built-in image_gen tool mode; keep the prompt as the single source of truth.
    • Native vector diagram: use synapse-excalidraw for flow diagrams, architecture diagrams, and concept maps that should remain SVG/editable. This is a deliberate output-type choice, not an image-provider fallback.
    • Animation: use synapse-animation only when explicitly requested.
    • Never route raster generation through Gemini, Imagen APIs, or Nano Banana. If built-in image_gen fails, tell the user; use $imagegen CLI/API mode only with explicit approval.
  3. Save generated image into examples/<project>/figures/ (or figures/ during intermediate steps), and placeholder auto-resolves on reload. Final image 字段必须是 ./figures/... 相对路径。
  4. Traceability: record fallback reason and generator path in examples/<project>/figures/index.md for re-generation.
  5. Prompt visibility rule: prompt stays hidden in normal card state and is only revealed when figure is zoomed.

Workflow

  1. Decide if pretext is needed — does the text move, resize, flow around obstacles, or need exact geometry before render?
  2. Prepare once, layout many times — prepareWithSegments once, layout/layoutNextLine on every resize
  3. Match font strings exactly — pretext font string must be identical to CSS
  4. Inline the library — all output must be self-contained

Skill Composition

With synapse-design (blog)

synapse-design delegates to synapse-pretext when the HTML blog needs animated or geometry-sensitive text. Apply prepareWithSegments at minimum to animated text fields; extend to title, lead, and caption as needed.

With synapse-excalidraw (diagrams)

When a raster figure entry has no image file, invoke the Codex $imagegen skill in its default built-in image_gen tool mode and record codex-imagegen as the call path. If built-in image_gen is unavailable or fails, report it instead of switching providers silently. The $imagegen CLI/API fallback may be used only after explicit user approval. Use synapse-excalidraw directly when the intended artifact is an editable vector diagram. Generated images must be written into the current example’s ./figures/ directory (or figures/ during intermediate steps), and HTML image fields should reference ./figures/.... This includes examples generated under examples/<project>/, i.e. examples/heterogeneous-inference-progress/figures/....

With synapse-animation (animation)

When a Remotion composition includes text that must reflow during animation, use pretext for per-frame text measurement instead of DOM reads.

Ordering with other skills

  1. figure prompt/spec definitions drive what to communicate
  2. synapse-pretext refines typography and line layout
  3. synapse-design defines palette and visual tokens
  4. synapse-design implements the final web artifact
  5. synapse-animation adds motion when needed

HTML Build Protocol

The blog HTML template (examples/dsv4-blog-layout.html) is the reference implementation of pretext-powered blog rendering. synapse-forge uses this template for the Content→Blog mapping.

Template contents

The reference template contains:

  • Complete inlined pretext library (~2000 lines: bidi, segment, measure, layout)
  • fitTitle() binary search for adaptive headline sizing
  • Stick figure canvas overlay with click-spawn animation
  • Figure zoom overlay with img.onerror placeholder
  • SECTIONS array renderer with callout, body, and bodyHTML support
  • Stats bar showing layout metrics

Content variable contract

The template exposes these variables for downstream skills to fill:

VariableTypeDescription
TITLEstringArticle headline, fitted via fitTitle()
LEADstringOpening paragraph
SECTIONSarray{ heading, body, callout, image, caption, prompt, bodyHTML } entries
PULSEstringFinal takeaway paragraph
.meta-tagHTMLCategory badge
.meta-dateHTMLAuthor + date line
<title>HTMLPage title

Rendering rules

  • Content strings: backtick template literals for body/heading/callout/caption/prompt. See Curly Quote Rule below for why.
  • bodyHTML fields (Reference section) use single quotes since they contain HTML tags
  • Image paths: use relative paths from output directory
  • DO NOT run prettier — it breaks the inlined ~2000-line pretext library The full rendering orchestration (copy template → fill variables) is defined in synapse-forge SKILL.md.

Rules

  • Font string must match CSS exactly.
  • lineHeight in layout() must match CSS line-height.
  • Avoid system-ui font on macOS — use a named font.
  • Runtime requires Intl.Segmenter and Canvas 2D text measurement.
  • Do not claim pretext usage unless the page is actually using pretext APIs.
  • All output using pretext must be self-contained — inline the library, do not reference external files.
  • Prepare once per text+font combination. Layout is the hot path.
  • Never trigger DOM reflow for text measurement. That is the entire point of this skill.

Curly Quote Rule (CRITICAL — verify after EVERY .html edit)

Chinese curly quotes “” (U+201C/U+201D) are never valid JS string delimiters. They look like quotes but JavaScript treats them as regular characters, causing SyntaxError: Invalid or unexpected token that silently kills the entire <script type="module">. This has caused 3+ white-screen incidents.

Root cause: node scripts or Edit tool modifying HTML files that contain Chinese curly quotes as text content may accidentally:

  1. Replace a backtick/straight-quote delimiter with a curly quote
  2. Replace HTML attribute straight double quotes "href=..." with curly quotes href="..."

Allowed: curly quotes as string content (“质量保持”) — never as delimiters

Mandatory verification after ANY edit to .html template files:

node -e "new Function('async function _m(){' + require('fs').readFileSync('FILE','utf8').match(/<script type=\"module\">([\\s\\S]*)<\\/script>/)[1] + '}'); console.log('OK')"

If this fails → there are curly-quote delimiters or other syntax errors. Fix before reporting done.

  • DO NOT run prettier on pretext-powered HTML — it expands the inlined library's compact arrays and can introduce quote escaping issues.

Reference

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.