Graphviz dot language parser for ts-graphviz.
The module can then be installed using npm:
# yarn
$ yarn add @ts-graphviz/parser
# or npm
$ npm install -S @ts-graphviz/parser
Parse a string written in dot language and convert it to a model.
The return value is a Graph
or Digraph
that inherits from RootCluster
.
import { parse } from '@ts-graphviz/parser';
const G = parse(`
digraph hoge {
a -> b;
}`);
This is equivalent to the code below when using ts-graphviz.
import { digraph } from 'ts-graphviz';
const G = digraph('hoge', (g) => {
g.edge(['a', 'b']);
});
If the given string is invalid, a SyntaxError exception will be thrown.
import { parse, SyntaxError } from '@ts-graphviz/parser';
try {
parse(`invalid`);
} catch (e) {
if (e instanceof SyntaxError) {
console.log(e.message);
}
}
This is an experimental API. Behavior may change in the future.
A tag template version of the parse function.
Returns a Graph or Digraph object based on the parsed result.
If the given string is invalid, a SyntaxError exception will be thrown.
import { dot } from '@ts-graphviz/parser';
const G = dot`
graph {
a -- b
}
`;
The AST
module provides the ability to handle the AST as a result of parsing the dot language
for lower level operations.
The basic usage is the same as the parse
function, except that it returns the dot language AST.
- Parameters
dot
-- string in the dot language to be parsed.options.rule
-- Object type of dot string.- This can be
"node"
,"edge"
,"graph"
,"attributes"
,"attribute", "cluster_statements"
.
- This can be
import { AST } from '@ts-graphviz/parser';
const ast = AST.parse(`
strict digraph example {
subgraph cluster_0 {
label="Subgraph A";
a -> b -> c -> d;
}
subgraph cluster_1 {
label="Subgraph B";
a -> f;
f -> c;
}
}
`);
console.log(ast);
// {
// type: 'graph',
// id: 'example',
// directed: true,
// strict: true,
// body: [
// {
// type: 'subgraph',
// id: 'cluster_0',
// body: [
// { type: 'attribute', key: 'label', value: 'Subgraph A' },
// {
// type: 'edge',
// targets: [ { id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' } ],
// attributes: []
// }
// ]
// },
// {
// type: 'subgraph',
// id: 'cluster_1',
// body: [
// { type: 'attribute', key: 'label', value: 'Subgraph B' },
// {
// type: 'edge',
// targets: [ { id: 'a' }, { id: 'f' } ],
// attributes: []
// },
// {
// type: 'edge',
// targets: [ { id: 'f' }, { id: 'c' } ],
// attributes: []
// }
// ]
// }
// ]
// }
import { AST } from '@ts-graphviz/parser';
const ast = AST.parse(
`test [
style=filled;
color=lightgrey;
label = "example #1";
];`,
{ rule: AST.Types.Node },
);
console.log(ast);
// {
// type: 'node',
// id: 'test',
// attributes: [
// { key: 'style', value: 'filled' },
// { key: 'color', value: 'lightgrey' },
// { key: 'label', value: 'example #1' }
// ]
// }
Graphviz-dot Test and Integration
- ts-graphviz
- Graphviz library for TypeScript.
- @ts-graphviz/react
- Graphviz-dot Renderer using React.
- jest-graphviz
- Jest matchers that supports graphviz integration.
- setup-graphviz
- GitHub Action to set up Graphviz cross-platform(Linux, macOS, Windows).
This software is released under the MIT License, see LICENSE.