d2/d2js/js/README.md

172 lines
5.6 KiB
Markdown
Raw Normal View History

2025-01-12 14:41:58 +00:00
# D2.js
[![npm version](https://badge.fury.io/js/%40terrastruct%2Fd2.svg)](https://www.npmjs.com/package/@terrastruct/d2)
[![License: MPL-2.0](https://img.shields.io/badge/License-MPL_2.0-brightgreen.svg)](https://mozilla.org/MPL/2.0/)
D2.js is a JavaScript wrapper around D2, the modern diagram scripting language. It enables running D2 directly in browsers and Node environments through WebAssembly.
## Features
- 🌐 **Universal** - Works in both browser and Node environments
- 🚀 **Modern** - Built with ESM modules, with CJS fallback
- 🔄 **Isomorphic** - Same API everywhere
-**Fast** - Powered by WebAssembly for near-native performance
- 📦 **Lightweight** - Minimal wrapper around the core D2 engine
## Installation
```bash
# npm
npm install @terrastruct/d2
# yarn
yarn add @terrastruct/d2
# pnpm
pnpm add @terrastruct/d2
# bun
bun add @terrastruct/d2
```
## Usage
2025-01-14 06:15:26 +00:00
D2.js uses webworkers to call a WASM file.
2025-01-12 14:41:58 +00:00
2025-02-25 21:30:51 +00:00
### Basic Usage
2025-01-12 14:41:58 +00:00
```javascript
2025-01-14 06:15:26 +00:00
// Same for Node or browser
2025-01-12 14:41:58 +00:00
import { D2 } from '@terrastruct/d2';
2025-01-14 06:15:26 +00:00
// Or using a CDN
// import { D2 } from 'https://esm.sh/@terrastruct/d2';
2025-01-12 14:41:58 +00:00
const d2 = new D2();
const result = await d2.compile('x -> y');
2025-02-25 21:30:51 +00:00
const svg = await d2.render(result.diagram, result.renderOptions);
2025-02-14 22:47:32 +00:00
```
Configuring render options (see [CompileOptions](#compileoptions) for all available options):
2025-02-14 22:47:32 +00:00
```javascript
import { D2 } from '@terrastruct/d2';
const d2 = new D2();
const result = await d2.compile('x -> y', {
sketch: true,
2025-02-14 22:47:32 +00:00
});
const svg = await d2.render(result.diagram, result.renderOptions);
2025-01-12 14:41:58 +00:00
```
2025-02-27 18:38:44 +00:00
### Imports
2025-02-25 21:30:51 +00:00
2025-02-27 18:38:44 +00:00
In order to support [imports](https://d2lang.com/tour/imports), a mapping of D2 file paths to their content can be passed to the compiler.
2025-02-25 21:30:51 +00:00
```javascript
import { D2 } from '@terrastruct/d2';
const d2 = new D2();
const fs = {
"project.d2": "a: @import",
"import.d2": "x: {shape: circle}",
}
const result = await d2.compile({
fs,
inputPath: "project.d2",
options: {
sketch: true
}
});
const svg = await d2.render(result.diagram, result.renderOptions);
```
2025-01-12 14:41:58 +00:00
## API Reference
### `new D2()`
2025-02-14 22:47:32 +00:00
2025-01-12 14:41:58 +00:00
Creates a new D2 instance.
2025-02-25 21:30:51 +00:00
### `compile(input: string | CompileRequest, options?: CompileOptions): Promise<CompileResult>`
2025-02-14 22:47:32 +00:00
2025-02-25 21:30:51 +00:00
Compiles D2 markup into an intermediate representation. It compile options are provided in both `input` and `options`, the latter will take precedence.
2025-01-12 14:41:58 +00:00
2025-02-14 22:47:32 +00:00
### `render(diagram: Diagram, options?: RenderOptions): Promise<string>`
Renders a compiled diagram to SVG.
### `CompileOptions`
All [RenderOptions](#renderoptions) properties in addition to:
2025-02-14 22:47:32 +00:00
2025-01-12 14:41:58 +00:00
- `layout`: Layout engine to use ('dagre' | 'elk') [default: 'dagre']
2025-02-25 23:38:27 +00:00
- `fontRegular` A byte array containing .ttf file to use for the regular font. If none provided, Source Sans Pro Regular is used.
- `fontItalic` A byte array containing .ttf file to use for the italic font. If none provided, Source Sans Pro Italic is used.
- `fontBold` A byte array containing .ttf file to use for the bold font. If none provided, Source Sans Pro Bold is used.
- `fontSemibold` A byte array containing .ttf file to use for the semibold font. If none provided, Source Sans Pro Semibold is used.
2025-02-14 22:47:32 +00:00
### `RenderOptions`
2025-01-12 14:41:58 +00:00
- `sketch`: Enable sketch mode [default: false]
2025-02-13 12:20:38 +00:00
- `themeID`: Theme ID to use [default: 0]
- `darkThemeID`: Theme ID to use when client is in dark mode
2025-02-11 04:10:27 +00:00
- `center`: Center the SVG in the containing viewbox [default: false]
- `pad`: Pixels padded around the rendered diagram [default: 100]
- `scale`: Scale the output. E.g., 0.5 to halve the default size. The default will render SVG's that will fit to screen. Setting to 1 turns off SVG fitting to screen.
2025-02-13 21:59:29 +00:00
- `forceAppendix`: Adds an appendix for tooltips and links [default: false]
2025-02-24 23:32:19 +00:00
- `target`: Target board/s to render. If target ends with '*', it will be rendered with all of its scenarios, steps, and layers. Otherwise, only the target board will be rendered. E.g. `target: 'layers.x.*'` to render layer 'x' with all of its children. Pass '*' to render all scenarios, steps, and layers. By default, only the root board is rendered. Multi-board outputs are currently only supported for animated SVGs and so `animateInterval` must be set to a value greater than 0 when targeting multiple boards.
- `animateInterval`: If given, multiple boards are packaged as 1 SVG which transitions through each board at the interval (in milliseconds).
- `salt`: Add a salt value to ensure the output uses unique IDs. This is useful when generating multiple identical diagrams to be included in the same HTML doc, so that duplicate IDs do not cause invalid HTML. The salt value is a string that will be appended to IDs in the output.
- `noXMLTag`: Omit XML tag `(<?xml ...?>)` from output SVG files. Useful when generating SVGs for direct HTML embedding.
2025-01-12 14:41:58 +00:00
2025-02-25 21:30:51 +00:00
### `CompileRequest`
2025-02-27 18:38:44 +00:00
- `fs`: A mapping of D2 file paths to their content
- `inputPath`: The path of the entry D2 file [default: index]
2025-02-25 21:30:51 +00:00
- `options`: The [CompileOptions](#compileoptions) to pass to the compiler
2025-02-14 22:47:32 +00:00
### `CompileResult`
- `diagram`: `Diagram`: Compiled D2 diagram
- `options`: `RenderOptions`: Render options merged with configuration set in diagram
- `fs`
- `graph`
2025-01-12 14:41:58 +00:00
## Development
D2.js uses Bun, so install this first.
### Building from source
```bash
git clone https://github.com/terrastruct/d2.git
cd d2/d2js/js
2025-01-14 03:21:18 +00:00
./make.sh all
2025-01-12 14:41:58 +00:00
```
If you change the main D2 source code, you should regenerate the WASM file:
```bash
./make.sh build
```
2025-01-15 18:37:04 +00:00
### Running the dev server
2025-01-12 14:41:58 +00:00
2025-01-15 18:37:04 +00:00
You can browse the examples by running the dev server:
2025-01-14 03:21:18 +00:00
2025-01-12 14:41:58 +00:00
```bash
2025-01-15 18:37:04 +00:00
./make.sh dev
2025-01-12 14:41:58 +00:00
```
Visit `http://localhost:3000` to see the example page.
## Contributing
Contributions are welcome!
## License
This project is licensed under the Mozilla Public License Version 2.0.