Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API docs to website #206

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/lang-dot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
],
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "types/index.d.ts",
"devDependencies": {
"@lezer/generator": "^1.3.0",
"@rollup/plugin-node-resolve": "^15.1.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/lang-dot/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LanguageSupport } from "@codemirror/language";

export function dot(): LanguageSupport;
32 changes: 1 addition & 31 deletions packages/viz/src/standalone.mjs
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
/**
* @module viz
*/

import Module from "../lib/module.mjs";
import Viz from "./viz.mjs";
import { decode } from "../lib/encoded.mjs";

export {
/**
* A string indicating the version of Graphviz used for this build.
* This records the value of {@link Viz#graphvizVersion} at build time and can be used without creating an instance of the {@link Viz} class.
* @constant {string}
*/
graphvizVersion,

/**
* An array of strings indicating the supported Graphviz output formats in this build.
* This records the value of {@link Viz#formats} at build time and can be used without creating an instance of the {@link Viz} class.
* @constant {string[]}
*/
formats,

/**
* An array of strings indicating the supported Graphviz layout engines in this build.
* This records the value of {@link Viz#engines} at build time and can be used without creating an instance of the {@link Viz} class.
* @constant {string[]}
*/
engines
} from "../lib/metadata.mjs";
export { graphvizVersion, formats, engines } from "../lib/metadata.mjs";

/**
* Returns a promise that resolves to an instance of the {@link Viz} class.
* This function encapsulates instantiating the Emscripten module.
* @returns {Promise<Viz>}
*/
export function instance() {
return Module({ wasm: decode() }).then(m => new Viz(m));
}
179 changes: 0 additions & 179 deletions packages/viz/src/viz.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -241,189 +241,27 @@ function getPluginList(module, kind) {
return list;
}

/**
* An object representing a graph.
* @example
* {
* edges: [
* { tail: "a", head: "b" }
* ]
* }
* @example
* {
* directed: false,
* nodeAttributes: { style: "filled", fontcolor: "white" },
* edges: [
* { tail: "a", head: "b", attributes: { label: "1" } },
* { tail: "b", head: "c", attributes: { label: "2" } },
* { tail: "c", head: "a", attributes: { label: "3" } }
* ],
* nodes: [
* { name: "a", attributes: { color: "red" } },
* { name: "b", attributes: { color: "green" } },
* { name: "c", attributes: { color: "blue" } }
* ]
* }
* @typedef {object} Graph
* @property {string} [name]
* @property {boolean} [strict=false]
* @property {boolean} [directed=true]
* @property {Attributes} [graphAttributes]
* @property {Attributes} [nodeAttributes]
* @property {Attributes} [edgeAttributes]
* @property {Node[]} [nodes]
* @property {Edge[]} [edges]
* @property {Subgraph[]} [subgraphs]
*/

/**
* An object representing the attributes for a graph, node, or edge.
* Used to specify attributes in {@link Graph}, {@link Node}, {@link Edge}, or {@link Subgraph}, and default attributes in {@link RenderOptions}.
* @example
* {
* color: "blue",
* width: 1,
* label: { html: "<i>Viz.js</i>" }
* }
* @typedef {Object.<string, AttributeValue>} Attributes
*/

/**
* Values that can be specified for {@link Attributes}.
* @typedef {string | number | boolean | HTMLString} AttributeValue
*/

/**
* An HTML string attribute value.
* This can be used to create <a href="https://www.graphviz.org/doc/info/shapes.html#html">HTML-like labels</a>.
* @example
* { html: "<i>Hello!</i>" }
* @typedef {object} HTMLString
* @property {string} html
*/

/**
* An object representing a node in a {@link Graph}.
* @typedef {object} Node
* @property {string} name
* @property {Attributes} [attributes]
*/

/**
* An object representing a edge in a {@link Graph}.
* @typedef {object} Edge
* @property {string} tail
* @property {string} head
* @property {Attributes} [attributes]
*/

/**
* An object representing a subgraph in a {@link Graph}.
* @typedef {object} Subgraph
* @property {string} [name]
* @property {Attributes} [graphAttributes]
* @property {Attributes} [nodeAttributes]
* @property {Attributes} [edgeAttributes]
* @property {Node[]} [nodes]
* @property {Edge[]} [edges]
* @property {Subgraph[]} [subgraphs]
*/

/**
* Options for {@link Viz#render} and other render methods.
* @typedef {object} RenderOptions
* @property {string} [format=dot]
* @property {string} [engine=dot]
* @property {boolean} [yInvert=false]
* @property {boolean} [reduce=false]
* @property {Attributes} [graphAttributes]
* @property {Attributes} [nodeAttributes]
* @property {Attributes} [edgeAttributes]
*/

/**
* An object representing the result of rendering. See {@link Viz#render}.
* @typedef {SuccessResult | FailureResult} RenderResult
*/

/**
* Returned by {@link Viz#render} when rendering is successful.
* @typedef {object} SuccessResult
* @property {"success"} status
* @property {string} output
* @property {RenderError[]} errors
*/

/**
* Returned by {@link Viz#render} when rendering is unsuccessful.
* @typedef {object} FailureResult
* @property {"failure"} status
* @property {undefined} output
* @property {RenderError[]} errors
*/

/**
* A warning or error message emitted by Graphviz during rendering.
* @typedef {object} RenderError
* @property {"error" | "warning"} [level]
* @property {string} message
*/

/**
* Wraps an instance of the Emscripten module used to call Graphviz.
* Use {@link module:viz.instance} to create a new instance of this class.
*/
class Viz {
/** @package */
constructor(module) {
this.module = module;
}

/**
* Returns a string indicating the version of Graphviz available at runtime.
* The constant{@link module:viz.graphvizVersion} records the value of this method at build time.
* @returns {string}
*/
get graphvizVersion() {
return getGraphvizVersion(this.module);
}

/**
* Returns an array of strings indicating the supported Graphviz output formats at runtime.
* The constant {@link module:viz.formats} records the value of this method at build time.
* @returns {string[]}
*/
get formats() {
return getPluginList(this.module, "device");
}

/**
* Returns an array of strings indicating the supported Graphviz layout engines at runtime.
* The constant {@link module:viz.engines} records the value of this method at build time.
* @returns {string[]}
*/
get engines() {
return getPluginList(this.module, "layout");
}

/**
* Renders the graph described by the input and returns an object representing the result of rendering.
* @param {string | Graph} input
* @param {RenderOptions} [options]
* @returns {RenderResult}
*/
render(input, options = {}) {
return renderInput(this.module, input, { format: "dot", engine: "dot", ...options });
}

/**
* Renders the input and returns the result as a string.
* This method accepts the same options as {@link Viz#render}.
* @param {string | Graph} input
* @param {RenderOptions} [options]
* @returns {string}
* @throws Throws an error if rendering failed.
*/
renderString(src, options = {}) {
const result = this.render(src, options);

Expand All @@ -434,29 +272,12 @@ class Viz {
return result.output;
}

/**
* Convenience method that parses the output and returns an SVG element that can be inserted into the document.
* This method accepts the same options as {@link Viz#render}, except the format option is always "svg".
* @param {string | Graph} input
* @param {RenderOptions} [options]
* @returns {SVGSVGElement}
* @throws Throws an error if rendering failed.
*/
renderSVGElement(src, options = {}) {
const str = this.renderString(src, { ...options, format: "svg" });
const parser = new DOMParser();
return parser.parseFromString(str, "image/svg+xml").documentElement;
}

/**
* Convenience method that renders the input, parses the output, and returns a JSON object.
* If rendering failed, it throws an error.
* This method accepts the same options as {@link Viz#render}, except that the format option is always "json".
* @param {string | Graph} input
* @param {RenderOptions} [options]
* @returns {object}
* @throws Throws an error if rendering failed.
*/
renderJSON(src, options = {}) {
const str = this.renderString(src, { ...options, format: "json" });
return JSON.parse(str);
Expand Down
Loading