Mark Madej
Mark Madej

Reputation: 1922

Mermaid CLI - how do you escape characters?

I'm using the Mermaid CLI to generate a flowchart (http://knsv.github.io/mermaid/flowchart.html). It works great, but I can't figure out how to get special characters (percent signs, parenthesis, etc) working as text within a node.

For illustration purposes here is a sample flowchart definition for Mermaid (filename is example.mermaid):

graph TD
question1{Gas tank less than 1/8?}
action1[Fill tank to 100%]
question1-- Yes -->action1

When I run mermaid on that file, I get this error (it blows up on the percent sign):

My-MacBook-Pro:mermaid mark$ mermaid example.mermaid 
Error: Parse error on line 3:
...on1[Fill tank to 100%]question1-- Yes -
-----------------------^
Expecting 'QUOTE', 'TAG_END', 'TAG_START', 'MULT', 'EQUALS', 'PLUS', 'DOT', 'BRKT', 'COLON', 'ALPHA', 'COMMA', 'NUM', 'CLICK', 'CLASS', 'CLASSDEF', 'LINKSTYLE', 'STYLE', 'PIPE', 'THICK_ARROW_OPEN', 'THICK_ARROW_CROSS', 'THICK_ARROW_CIRCLE', 'THICK_ARROW_POINT', 'DOTTED_ARROW_OPEN', 'DOTTED_ARROW_CROSS', 'DOTTED_ARROW_CIRCLE', 'DOTTED_ARROW_POINT', 'ARROW_OPEN', 'ARROW_CROSS', 'ARROW_CIRCLE', 'ARROW_POINT', '==', '-.', '--', 'MINUS', 'DIAMOND_STOP', 'DIAMOND_START', 'PE', 'PS', 'SQE', 'SQS', 'end', 'subgraph', 'NEWLINE', 'TAGSTART', 'TAGEND', 'DIR', 'SPACE', 'GRAPH', 'EOF', 'SEMI', got 'PCT'

  ../dist/mermaid.full.js:14712 in parseError
  ../dist/mermaid.full.js:14782 in parse
  ../dist/mermaid.full.js:13260
  ../dist/mermaid.full.js:16846
  ../dist/mermaid.full.js:16889
  phantomjs://webpage.evaluate():23 in executeInPage
  phantomjs://webpage.evaluate():29
  phantomjs://webpage.evaluate():29
PHANTOM ERROR: TypeError: 'null' is not an object (evaluating 'element.setAttribute')
TRACE:
 -> /usr/local/lib/node_modules/mermaid/lib/phantomscript.js: 149 (in function resolveSVGElement)
 -> /usr/local/lib/node_modules/mermaid/lib/phantomscript.js: 69

I tried escaping the percent sign, like this:

action1[Fill tank to 100&]

But then I get the same error on the semicolon. Any thoughts on how I can escape those characters to make it work? Thanks!

Upvotes: 62

Views: 51246

Answers (4)

Aziz Alto
Aziz Alto

Reputation: 20331

Use quotation marks "" to enclose your text and escape special characters, e.g. in your example:

graph TD

    question1{"Gas tank less than 1/8?"}
    action1["Fill tank to 100%"]

    question1-- Yes -->action1

will produce this diagram:

enter image description here

This is now documented in the official documentation.

Upvotes: 73

Haili Sun
Haili Sun

Reputation: 849

sequenceDiagram syntax are different. Refer to official document: https://mermaid.js.org/syntax/sequenceDiagram.html#entity-codes-to-escape-characters

Entity codes to escape characters It is possible to escape characters using the syntax exemplified here.

Code: mermaid sequenceDiagram
    A->>B: I #9829; you!
    B->>A: I #9829; you #infin; times more! A B I ♥ you! I ♥ you ∞ times more! A B Numbers given are base 10, so # can be encoded as
#35;. It is also supported to use HTML character names.

Because semicolons can be used instead of line breaks to define the markup, you need to use #59; to include a semicolon in message text.

Upvotes: 2

fiktor
fiktor

Reputation: 1413

Here are more examples of escaping / quoting special characters. The code

flowchart LR
  A["For most symbols double quotes are enough: ```~`!@#$%^*()[]{}|\/:;'?<>,.+=-_"]
  B["[&quot;&amp;quot;&amp;lt;&lt;br&gt;&amp;gt;&amp;amp;&amp;frac12;#35;189;&quot;]"]
  B --> C["&quot;&lt;<br>&gt;&amp;&frac12;#189;"]

produces Mermaid flowchart

Using this method most unicode characters (including non-ASCII) can be included using either unicode entity or markdown #code; syntax (both ways are illustrated using the character ½ in the diagram above). See also the official documentation.

Disclaimer: I didn't test it in Mermaid CLI. Tested in markdown-viewer and Mermaid live editor.

Upvotes: 14

Knut Sveidqvist
Knut Sveidqvist

Reputation: 231

That was an issue with early mermaid versions. I tried your example code with mermaid 0.4.0 where it renders fine. I would recommend upgrading.

Upvotes: 1

Related Questions