Convert an 8-channel hex color (#RRGGBBAA) to a 6-channel hex color
(#RRGGBB), discarding the alpha channel. Throws if the input is not
a 9-character #-prefixed string.
Graphviz dot does not support alpha; this is a lossy projection.
Read the image filename for a state through jssm.Machine.style_for,
so theme-supplied images are honoured along with per-state declarations.
Returns undefined if neither a theme nor a state declaration supplies an
image.
Build a graphviz-safe node identifier for a state. Accepts either a
string[] (legacy test-only path; returns an index-based n0/n1
identifier via indexOf), or a precomputed Map<state, slug> produced
by slug_states (used by all rendering hot paths).
When a slug map is supplied, the identifier is the slug wrapped in
double quotes — dot allows quoted identifiers, and the slug alphabet
(lowercase alphanumerics + -) requires quoting because bare dot IDs
may not contain -. Graphviz round-trips the quoted form through to
the SVG <title> element and uses the slug as a stable basis for the
generated SVG element id attribute.
node_of('Red Light', new Map([['Red Light', 'red-light']]));
// '"red-light"'
Read the graphviz shape for a state through jssm.Machine.style_for,
so theme-supplied shapes are honoured along with per-state declarations.
Returns undefined if neither a theme nor a state declaration supplies a
shape.
Convert a state name into a URL-friendly slug suitable for use as the body of a dot/SVG node identifier. The transformation is:
[a-z0-9] (after lowercasing) becomes
a single -- are trimmed If the result is empty (e.g. for a state named "!!!"), the empty
string is returned — callers are expected to fall back to an indexed
placeholder like node-N. See slug_states for the collision-
resolving wrapper that consumes this helper.
slug_for('Green Light'); // 'green-light'
slug_for('!!!'); // ''
slug_for(' Foo Bar '); // 'foo-bar'
The state name to slugify.
The lowercase hyphen-separated slug, or empty string if none of the characters were retainable.
Build a Map<state, slug> assigning every state in states a unique,
deterministic, URL-safe slug used as its dot/SVG node identifier.
Algorithm:
node-N, where N is the state's declaration
index (1-based, to match user-visible numbering).-2, -3, … suffixes.
If the proposed suffixed slug itself collides with a base slug
used later, the counter advances until a free slot is found.This yields a deterministic mapping given the state-declaration order, so output is stable across runs.
slug_states(['Red Light', 'red-light']);
// Map { 'Red Light' => 'red-light', 'red-light' => 'red-light-2' }
slug_states(['!!!', '???']);
// Map { '!!!' => 'node-1', '???' => 'node-2' }
States in declaration order.
A Map from each state name to its unique slug.
Compose a graphviz style string for a state by looking up its merged
style via jssm.Machine.style_for, then delegating to
{@link compose_style_string}. Theme-supplied corners and lineStyle
are honoured along with per-state declarations.
Variant of color8to6 that passes undefined through.
Look up a color from the default viz palette by key, returning empty string if the key is unknown (so it disappears in feature concatenation).
Inject runtime configuration for jssm/viz. Currently only accepts a
custom DOMParser constructor for use by *_svg_element functions in
environments that do not provide one globally (e.g. Node + jsdom).
Idempotent — last call wins. No-op if called with no recognized keys.
// Node, with jsdom:
import { JSDOM } from 'jsdom';
import { configure, fsl_to_svg_element } from 'jssm/viz';
configure({ DOMParser: new JSDOM().window.DOMParser });
const el = await fsl_to_svg_element('a -> b;');
Configuration overrides.
Constructor compatible with the WHATWG DOMParser
interface. Used as a fallback when globalThis.DOMParser is undefined.
Compatibility wrapper for machine_to_dot, retained from jssm-viz. Will be removed in the next major.
Render a graphviz dot source string to SVG using @viz-js/viz. The
underlying viz instance is lazy-initialized on first call and cached for
the lifetime of the module.
const svg = await dot_to_svg('digraph G { a -> b }');
const svg_neato = await dot_to_svg('digraph G { a -> b }', { engine: 'neato' });
Graphviz dot source.
Optional renderer overrides.
Graphviz layout engine to use (e.g. 'dot',
'neato', 'circo'). Unrecognized engine names cause @viz-js/viz
to throw at render time.
A promise resolving to an SVG XML string.
Render an FSL string directly to graphviz dot source.
import { fsl_to_dot } from 'jssm/viz';
const dot = fsl_to_dot('a -> b;');
// suppress state-name labels
const dot2 = fsl_to_dot('a -> b;', { hide_state_labels: true });
const dot_with_footer = fsl_to_dot('a -> b;', { footer: 'label="caption";' });
// 'digraph G { ... label="caption"; }'
The FSL source.
Optional render flags. See VizRenderOpts.
A complete graphviz dot source string.
Render an FSL string directly to a parsed SVGSVGElement.
The FSL source.
Optional render flags. See VizRenderOpts.
A promise resolving to a parsed SVGSVGElement.
Render an FSL string directly to SVG.
const svg = await fsl_to_svg_string('a -> b;');
const svg_neato = await fsl_to_svg_string('a -> b;', { engine: 'neato' });
The FSL source.
Optional render flags. See VizRenderOpts.
A promise resolving to an SVG XML string.
Render a jssm.Machine as a graphviz dot string.
An optional footer may be supplied via opts.footer; it is emitted
verbatim just before the closing } of the dot source, after all
arrange declarations. This is a function-argument-only feature for
the moment — a machine-attribute equivalent is planned as a follow-up.
import { sm } from 'jssm';
import { machine_to_dot } from 'jssm/viz';
const dot = machine_to_dot(sm`a -> b;`);
// 'digraph G { ... }'
// suppress state-name labels (boxes only, no text inside)
const dot2 = machine_to_dot(sm`a -> b;`, { hide_state_labels: true });
const dot_with_footer = machine_to_dot(sm`a -> b;`, { footer: 'labelloc="b"; label="caption";' });
// 'digraph G { ... labelloc="b"; label="caption"; }'
The machine to render.
Optional render flags. See VizRenderOpts.
A complete graphviz dot source string.
Render a jssm.Machine to a parsed SVGSVGElement.
The machine to render.
Optional render flags. See VizRenderOpts.
A promise resolving to a parsed SVGSVGElement.
Render a jssm.Machine to SVG.
The machine to render.
Optional render flags. See VizRenderOpts.
A promise resolving to an SVG XML string.
Generated using TypeDoc
Options for the dot/SVG render entry points.
hide_state_labels(defaultfalse) — whentrue, the rendered dot output omits thelabel=attribute on every state's node line. Graphviz then draws the box without any text inside. Useful for diagrams where shape, color, or layout alone carry the meaning (icon-only diagrams, tutorial graphics, presentation slides).footer— verbatim dot source inserted just before the closing}of the generated dot source (e.g.labelloc="b"; label="caption";).engine— graphviz layout engine for the SVG render path (e.g.dot,neato,circo); honored byfsl_to_svg_string.