railroad-diagrams

Railroad-Diagram Generator, JS Version

This is a small library for generating railroad diagrams (like what JSON.org uses) using SVG or Unicode/ASCII text, with both JS and Python ports. Here’s an online dingus for you to play with and get SVG code from!

(This is the README for the JS port; see the main README for other ports, and for more non-JS-specific information.)

Installation

Install from npm (ES module, Node 18+ or browsers, includes TypeScript declarations):

npm install railroad-diagrams

You can also import the default stylesheet in bundlers via railroad-diagrams/railroad.css, or copy railroad.css into your page.

Diagrams

To use the library, include railroad.css in your page, and import the railroad-diagrams module (or railroad.js directly) in your script, then call Diagram(...). Its arguments are the components of the diagram (Diagram is a special form of Sequence).

The constructors for each node are named exports in the module; the default export is an object of same-named functions that just call the constructors, so you can construct diagrams without having to spam new all over the place:

// Use the constructors
import { Diagram, Choice } from "railroad-diagrams";
const d = new Diagram("foo", new Choice(0, "bar", "baz"));

// Or use the functions that call the constructors for you
import rr from "railroad-diagrams";
const d = rr.Diagram("foo", rr.Choice(0, "bar", "baz"));

// Output as inline SVG DOM element (in a browser)...
container.appendChild(d.toSVG());
// Or output SVG source code...
container.textContent = "" + d;
// Or write out a standalone SVG as source...
console.log(d.toStandalone());
// Or instead render it as Unicode/ASCII text...
container.textContent = d.toText();

Alternately, you can construct a ComplexDiagram(); it’s identical to Diagram(), but has slightly different start/end shapes, same as what JSON.org does to distinguish between “leaf” types like number (ordinary Diagram()) and “container” types like Array (ComplexDiagram()).

The Diagram class also has a few methods:

CLI

You can also render diagrams from the command line via npx railroad-diagrams (or node cli.js), accepting JavaScript expressions (default), Bikeshed’s indentation DSL (-s dsl), KDL 2.0 (-s kdl), or auto-detected DSL/KDL (-s auto):

npx railroad-diagrams svg -e 'Diagram("foo", Choice(0, "bar", "baz"))'
npx railroad-diagrams unicode -s kdl -e 'choice 0 { t bar; t baz }'

Parsing Text Syntaxes (DSL & KDL 2.0)

In addition to constructing diagrams programmatically, you can parse either Bikeshed’s indentation DSL or KDL 2.0. See the Input Syntaxes section of the main README for the full grammar, supported commands/nodes, properties, and auto-detection rules.

import { parse, parseDsl, parseKdl, detectSyntax, ParseError } from "railroad-diagrams";

// Auto-detect Bikeshed DSL vs KDL 2.0:
const d1 = parse(`
Stack:
	Or: 1
		T: --
		Seq:
			Opt: skip
				T: -
			N: ident
`);

const d2 = parse(`
stack {
	choice 1 {
		t "--"
		seq {
			opt skip { t "-" }
			n ident
		}
	}
}
`);

Components

Components are either leaves or containers.

The leaves:

The containers:

Options

There are a few options you can tweak, in an Options object exported from the module. Just tweak either until the diagram looks like what you want. You can also change the CSS file - feel free to tweak to your heart’s content. Note, though, that if you change the text sizes in the CSS, you’ll have to go adjust the options specifying the text metrics as well.