Graphity for TypeScript 3.0.0
    Preparing search index...

    A writer of Mermaid flowcharts that labels and styles a graph with the caller's data by node ID and edge index. Mermaid flowcharts render in Markdown.

    MermaidWriter.of returns a writer for a graph, the methods that configure it return the writer again, and MermaidWriter.write or MermaidWriter.writeToString write the text. The graph holds no data, so labels, shapes, classes and styles come from hooks that receive a node ID or an edge index and read the caller's arrays. Here, the packages of a small program get their names from names. The strongly connected components sccs tell which packages are in a cycle, and these are drawn in red:

    const mermaid = MermaidWriter.of(dependencies)
    .nodeLabel((node) => names[node])
    .classDef("cycle", "stroke:red")
    .nodeClass((node) => (sccs.containsCycle(sccs.sccOf(node)) ? "cycle" : null))
    .writeToString();
    // model and storage, the cycle, in red:
    // classDef cycle stroke:red
    // class n4 cycle
    // class n5 cycle

    Nodes are named by their node IDs after an n, such as n4. The text lists the class definitions, the clusters in group order with their nodes, then the nodes in no cluster, then the edges in edge index order, the classes of the nodes and the edge styles, so the same graph and hooks always give the same text. Mermaid numbers the edges in the order they are written; the writer translates edge indices into these numbers for linkStyle, also when a filter leaves edges out.

    Labels are written as quoted strings: the writer turns quotes, #, %, &, :, <, > and backticks into Mermaid entity codes and line breaks into <br>, so that no label reads as HTML, as a Markdown string or as a directive. For the same reason, whitespace right after the word direction becomes an entity code, and an empty label, which Mermaid rejects, is written as one space.

    Mermaid's renderer still changes some labels. Mermaid 11 applies Markdown emphasis and backslash escapes to every label, so 2*3*4 loses its asterisks; Mermaid 12 shows a backslash before n as a line break. Both read ¶ß and fl°, which Mermaid uses internally for entity codes, as ; and &. Both also turn fa:fa-name into an icon in a node label with a line break. With htmlLabels set to false, Mermaid shows some entity codes as written, such as &#58; for :.

    Class names must be identifiers - an ASCII letter or underscore, then ASCII letters, digits or underscores - other than a Mermaid keyword: style, linkStyle, interpolate, classDef, class, href, click, call, graph, flowchart, subgraph, end, _self, _blank, _parent and _top. Keywords are case-sensitive, so End is a valid class name. Styles are written as given and must not be blank or contain a line break or a semicolon.

    Mermaid renders graphs of a few hundred nodes well; for larger ones, DotWriter in the package @kobayami/graphity-dot writes DOT (the language of Graphviz). A writer keeps its configuration and can write any number of times.

    Instances are mutable.

    Index
    • Defines a class for MermaidWriter.nodeClass to assign, written as classDef name style.

      Time: O(1).

      Parameters

      • name: string

        class name, an identifier other than a Mermaid keyword

      • style: string

        style of the class, such as fill:#f96,stroke:#333

      Returns this

      this writer

      Error if name is not an identifier or is a Mermaid keyword, or style is blank or contains a line break or a semicolon

    • Draws each group of partition as a cluster (a Mermaid subgraph), with a label by group ID.

      Nodes in no group stay outside every cluster, and a group without a node to write has no cluster. A label of null or undefined shows the name of the cluster: c and the group ID, such as c2.

      Time: O(1).

      Parameters

      • partition: NodePartition

        the groups, over the nodes of the graph, such as an Sccs

      • label: (group: number) => string | null | undefined

        called with a group ID, returns the label of its cluster, or null or undefined

      Returns this

      this writer

      Error if partition has a different node count than the graph

    • Sets the label of each edge, by edge index; null or undefined leaves an edge unlabeled.

      Time: O(1).

      Parameters

      • label: (edge: number) => string | null | undefined

        called with an edge index, returns the label of the edge, or null or undefined

      Returns this

      this writer

    • Sets the style of each edge, by edge index, written as a linkStyle statement; null or undefined leaves an edge in the default style.

      Time: O(1).

      Parameters

      • style: (edge: number) => string | null | undefined

        called with an edge index, returns the style of the edge, such as stroke:red,stroke-width:2px, or null or undefined

      Returns this

      this writer

    • Sets the class of each node, by node ID; null or undefined assigns none.

      Time: O(1).

      Parameters

      • className: (node: number) => string | null | undefined

        called with a node ID, returns the name of the class of the node, an identifier other than a Mermaid keyword, or null or undefined

      Returns this

      this writer

    • Restricts the text to the nodes that include accepts and the edges between them.

      Node IDs stay those of the graph, so the hooks keep reading the same arrays.

      Time: O(1).

      Parameters

      • include: (node: number) => boolean

        called with a node ID, returns whether to write the node

      Returns this

      this writer

    • Sets the label of each node, by node ID; null or undefined shows the name of the node, such as n4.

      Time: O(1).

      Parameters

      • label: (node: number) => string | null | undefined

        called with a node ID, returns the label of the node, or null or undefined

      Returns this

      this writer

    • Sets the shape of each node, by node ID; null or undefined draws a rectangle.

      A value that is not one of Shape makes MermaidWriter.write throw an Error.

      Time: O(1).

      Parameters

      • shape: (node: number) => Shape | null | undefined

        called with a node ID, returns the shape of the node, or null or undefined

      Returns this

      this writer

    • Switches to the undirected reading of the graph: edges without arrowheads.

      The edges a -> b and b -> a become one, written as the edge from the lower node ID; the edge hooks receive its edge index.

      Time: O(1).

      Returns this

      this writer

    • Writes the graph to out, as the text of a Mermaid flowchart. out is anything with a method write(text), such as process.stdout or a file stream of Node.js (Java: an Appendable).

      The text goes to out.write in chunks of about 8 KiB characters, not piece by piece.

      Time: O(nodeCount + edgeCount + groupCount), plus the hooks, with groupCount the number of groups of the partition drawn as clusters; in the undirected reading, O(log outDegree(target)) more per edge. Memory: up to twice the size of the linkStyle lines while it runs, as they are written last, and a chunk of text.

      Parameters

      • out: TextSink

        where the text goes; not closed

      Returns void

      Error what out.write throws (Java: IOException), or if a hook returns a class name that is not an identifier or is a Mermaid keyword, an edge style that is blank or contains a line break or a semicolon, or a shape that is not one of Shape; the text written before the error has gone to out. Only a write that throws fails the call. A stream reports its errors through its own event or promise, which the caller must watch (see TextSink)

      RangeError the engine's Invalid string length, if the linkStyle lines, which are collected before they are written, are longer than a JavaScript string can be (2^29 - 24 characters in V8, where Java allows 2^31 - 9; Java: an OutOfMemoryError)