### Usage Example
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Demonstrates how to import Viz.js, get an instance, and render an SVG element.
```APIDOC
## Usage Example
Viz.js exports a function, `instance`, which encapsulates decoding the WebAssembly code and instantiating the WebAssembly and Emscripten modules. This returns a promise that resolves to an instance of the `Viz` class, which provides methods for rendering graphs.
### Code Example
```javascript
import * as Viz from "@viz-js/viz";
Viz.instance().then(viz => {
const svg = viz.renderSVGElement("digraph { a -> b }");
document.getElementById("graph").appendChild(svg);
});
```
The instance can be used to render multiple graphs.
```
--------------------------------
### Viz.js 2.x Usage Example
Source: https://github.com/mdaines/viz-js/wiki/Differences-between-Viz.js-2.x-and-3.x
Demonstrates the traditional method of initializing Viz.js in version 2.x using `new Viz()` and handling asynchronous rendering with promises.
```html
```
--------------------------------
### Viz.js 3.x Basic Usage Example
Source: https://github.com/mdaines/viz-js/wiki/Differences-between-Viz.js-2.x-and-3.x
Illustrates the updated initialization pattern for Viz.js 3.x using `Viz.instance()`, which returns a promise. Rendering methods now return results synchronously.
```html
```
--------------------------------
### Initialize Viz.js Instance and Render Graph
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Import Viz.js, get an instance, and then use it to render a graph to an SVG element, appending it to the DOM. This is the primary way to use Viz.js for rendering.
```javascript
import * as Viz from "@viz-js/viz";
Viz.instance().then(viz => {
const svg = viz.renderSVGElement("digraph { a -> b }");
document.getElementById("graph").appendChild(svg);
});
```
--------------------------------
### Checkout Default Branch
Source: https://github.com/mdaines/viz-js/wiki/Release
Switch to the 'v3' branch and pull the latest changes before starting the release process.
```bash
git switch v3
git pull
```
--------------------------------
### Parse Line Comments in DOT
Source: https://github.com/mdaines/viz-js/blob/v3/packages/lang-dot/test/graph.txt
Shows how to use line comments in DOT graphs. Both '//' and '#' can be used to start a line comment, which extends to the end of the line.
```dot
graph {
a // line comment
b # line comment
}
```
--------------------------------
### Viz Instance
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Details on obtaining and using a Viz.js instance.
```APIDOC
## Viz Instance
### `instance()`
- **Returns**: `Promise` - A promise that resolves to an instance of the `Viz` class.
- **Description**: Returns a promise that resolves to an instance of the `Viz` class. This instance is used to render graphs.
### Class `Viz`
- **Description**: The `Viz` class isn't exported directly but is instantiated using the `instance()` function. It provides methods for rendering graphs and accessing Graphviz information at runtime.
```
--------------------------------
### Import and Render Graph with Viz.js
Source: https://github.com/mdaines/viz-js/blob/v3/packages/viz/site/index.md
Import the Viz.js library and use the instance function to render a graph. The rendered SVG element is then appended to the DOM. Ensure the Viz.js library is imported correctly.
```javascript
import * as Viz from "@viz-js/viz";
Viz.instance().then(viz => {
const svg = viz.renderSVGElement("digraph { a -> b }");
document.getElementById("graph").appendChild(svg);
});
```
--------------------------------
### Node with Ports
Source: https://github.com/mdaines/viz-js/blob/v3/packages/lang-dot/test/graph.txt
Demonstrates defining nodes with ports. Ports are specified using colons after the node name.
```dot
graph {
a:b
a:b:c
}
```
--------------------------------
### Create Release Branch
Source: https://github.com/mdaines/viz-js/wiki/Release
Create a new Git branch for the release preparation. Replace X-Y-Z with the version number using hyphens.
```bash
git switch -c viz-prepare-release-X-Y-Z
```
--------------------------------
### String Identifiers and Values
Source: https://github.com/mdaines/viz-js/blob/v3/packages/lang-dot/test/graph.txt
Shows how to use strings as graph names, node names, port names, and attribute values. Strings must be enclosed in double quotes.
```dot
graph "abc" {
"a"="b"
"test" ["x"="y"]
"x" -- "y" -- "z"
subgraph "\"test\"" { }
"a":"b":"c"
}
```
--------------------------------
### Viz.js 3.x Reusable Instance Pattern
Source: https://github.com/mdaines/viz-js/wiki/Differences-between-Viz.js-2.x-and-3.x
Shows a pattern for reusing a Viz.js 3.x instance by deferring the call to `Viz.instance()` until it's actually needed, managing the promise to avoid repeated instantiations.
```html
```
--------------------------------
### Viz Class Getters
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Runtime information about the Graphviz environment available through the Viz instance.
```APIDOC
## Viz Class Getters
### `graphvizVersion`
- **Returns**: `string` - The version of Graphviz at runtime.
- **Description**: Returns the version of Graphviz used for this build.
### `formats`
- **Returns**: `string[]` - An array of supported output format names.
- **Description**: The names of the [Graphviz output formats](https://www.graphviz.org/docs/outputs/) supported at runtime.
### `engines`
- **Returns**: `string[]` - An array of supported layout engine names.
- **Description**: The names of the [Graphviz layout engines](https://www.graphviz.org/docs/layouts/) supported at runtime.
**Note**: These getters return the same values as the constants `graphvizVersion`, `formats`, and `engines`.
```
--------------------------------
### Create Release Commit
Source: https://github.com/mdaines/viz-js/wiki/Release
Stage and commit the updated package-lock.json, package.json, and CHANGELOG.md files for the release. Replace X.Y.Z with the actual version number.
```bash
git add package-lock.json packages/viz/package.json packages/viz/CHANGELOG.md
git commit -m "Release @viz-js/viz X.Y.Z"
```
--------------------------------
### Render Basic SVG Element with Viz.js
Source: https://github.com/mdaines/viz-js/blob/v3/packages/viz/test/browser/index.html
Use this to render a simple SVG element from a DOT language string. Ensure Viz.js instance is initialized.
```javascript
assert.ok(viz.renderSVGElement("digraph { a -> b }"));
```
--------------------------------
### Viz.js - @viz-js/viz
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Provides a WebAssembly build of Graphviz with a simple JavaScript wrapper.
```APIDOC
## GET @viz-js/viz
### Description
Initializes and provides access to the WebAssembly build of Graphviz.
### Method
GET
### Endpoint
/api/@viz-js/viz
### Parameters
#### Query Parameters
None
### Request Example
None
### Response
#### Success Response (200)
- **GraphvizInstance** (object) - An instance of the Graphviz engine.
#### Response Example
```json
{
"graphviz": "..."
}
```
```
--------------------------------
### Node List Definition
Source: https://github.com/mdaines/viz-js/blob/v3/packages/lang-dot/test/graph.txt
Shows how to define multiple nodes in a list, separated by commas. This is useful for defining several nodes at once.
```dot
graph { a, b:x:y; c }
```
--------------------------------
### Viz.js Render Options
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Configuration options for the viz.render function.
```APIDOC
## Viz.js Render Options
### Description
Configuration options for the `viz.render` function, allowing customization of graph attributes, layout engines, and image handling.
### Parameters
#### Request Body
- **engine** (string) - Optional - The Graphviz layout engine to use for graph layout. Defaults to "dot".
- **yInvert** (boolean) - Optional - Invert y coordinates in output. Corresponds to the `-y` Graphviz command-line option. Defaults to `false`.
- **reduce** (boolean) - Optional - Reduce the graph. Corresponds to the `-x` Graphviz command-line option. Defaults to `false`.
- **graphAttributes** (Attributes) - Optional - Sets the default graph attributes. Corresponds to the `-G` Graphviz command-line option.
- **nodeAttributes** (Attributes) - Optional - Sets the default node attributes. Corresponds to the `-N` Graphviz command-line option.
- **edgeAttributes** (Attributes) - Optional - Sets the default edge attributes. Corresponds to the `-E` Graphviz command-line option.
- **images** (ImageSize[]) - Optional - Image sizes to use when rendering nodes with `image` attributes.
- **trustedTypePolicy** (object) - Optional - The TrustedTypePolicy object used to sanitize the rendered SVG string before parsing.
### Request Example
```json
{
"engine": "neato",
"reduce": true,
"graphAttributes": {
"label": "My Graph"
},
"images": [
{ "name": "test.png", "width": 300, "height": 200 }
]
}
```
### Response
#### Success Response (200)
- **status** (string) - Indicates "success".
- **output** (string) - The rendered graph output.
- **errors** (RenderError[]) - A list of warning messages.
#### Failure Response
- **status** (string) - Indicates "failure".
- **output** (undefined) - No output is provided on failure.
- **errors** (RenderError[]) - A list of error messages.
#### Response Example (Success)
```json
{
"status": "success",
"output": "",
"errors": [
{ "level": "warning", "message": "Some warning" }
]
}
```
#### Response Example (Failure)
```json
{
"status": "failure",
"output": undefined,
"errors": [
{ "level": "error", "message": "Graphviz error" }
]
}
```
```
--------------------------------
### Render Options
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Options that can be passed to rendering methods.
```APIDOC
## Render Options
### `RenderOptions`
- **`format`** (`string`) - Optional - Defaults to "dot". The [Graphviz output format](https://www.graphviz.org/docs/outputs/) to render (e.g., "dot" or "svg").
- **`engine`** (`string`) - Optional - Defaults to "dot". The [Graphviz layout engine](https://www.graphviz.org/docs/layouts/) to use for graph layout (e.g., "dot" or "neato").
- **`yInvert`** (`boolean`) - Optional - Defaults to `false`. Invert y coordinates in output. Corresponds to the [`-y` Graphviz command-line option](https://www.graphviz.org/doc/info/command.html#-y).
- **`reduce`** (`boolean`) - Optional - Defaults to `false`.
```
--------------------------------
### Viz.js Render Formats Options
Source: https://github.com/mdaines/viz-js/blob/v3/website/src/api/index.html
Configuration options for the viz.renderFormats function.
```APIDOC
## Viz.js Render Formats Options
### Description
Configuration options for the `viz.renderFormats` function, allowing rendering in multiple formats simultaneously.
### Parameters
#### Request Body
- **formats** (string[]) - Required - An array of desired output formats (e.g., ["svg", "png"]).
- **engine** (string) - Optional - The Graphviz layout engine to use for graph layout. Defaults to "dot".
- **yInvert** (boolean) - Optional - Invert y coordinates in output. Corresponds to the `-y` Graphviz command-line option. Defaults to `false`.
- **reduce** (boolean) - Optional - Reduce the graph. Corresponds to the `-x` Graphviz command-line option. Defaults to `false`.
- **graphAttributes** (Attributes) - Optional - Sets the default graph attributes. Corresponds to the `-G` Graphviz command-line option.
- **nodeAttributes** (Attributes) - Optional - Sets the default node attributes. Corresponds to the `-N` Graphviz command-line option.
- **edgeAttributes** (Attributes) - Optional - Sets the default edge attributes. Corresponds to the `-E` Graphviz command-line option.
- **images** (ImageSize[]) - Optional - Image sizes to use when rendering nodes with `image` attributes.
- **trustedTypePolicy** (object) - Optional - The TrustedTypePolicy object used to sanitize the rendered SVG string before parsing.
### Request Example
```json
{
"formats": ["svg", "png"],
"engine": "dot",
"graphAttributes": {
"label": "Multi-Format Graph"
}
}
```
### Response
#### Success Response (200)
- **status** (string) - Indicates "success".
- **output** (object) - An object where keys are the requested formats and values are the rendered graph output for each format.
- **errors** (RenderError[]) - A list of warning messages.
#### Failure Response
- **status** (string) - Indicates "failure".
- **output** (undefined) - No output is provided on failure.
- **errors** (RenderError[]) - A list of error messages.
#### Response Example (Success)
```json
{
"status": "success",
"output": {
"svg": "",
"png": "base64_encoded_png_data"
},
"errors": [
{ "level": "warning", "message": "Some warning" }
]
}
```
#### Response Example (Failure)
```json
{
"status": "failure",
"output": undefined,
"errors": [
{ "level": "error", "message": "Graphviz error" }
]
}
```
```
--------------------------------
### Verify Locked Dependencies
Source: https://github.com/mdaines/viz-js/wiki/Release
Check the changes in the package-lock.json file to ensure only the viz package version was updated and dependencies remain locked.
```bash
git diff package-lock.json
```
--------------------------------
### Parse HTML-like Strings in DOT
Source: https://github.com/mdaines/viz-js/blob/v3/packages/lang-dot/test/graph.txt
Demonstrates the use of HTML-like strings within DOT graphs for complex node labels. Ensure proper HTML structure within the angle brackets.
```dot
graph <> {
=<