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

Class diagrams broken with official Mermaid, and shown fine at GitHub. #6075

Open
FuadEfendi opened this issue Nov 23, 2024 · 11 comments
Open
Assignees
Labels
Graph: Class Status: In progress Type: Bug / Error Something isn't working or is incorrect

Comments

@FuadEfendi
Copy link

Description

This class diagram will be shown fine here at Github, and will fail in minimalistic plain HTML:

classDiagram
    AbstractFactory <|.. ConcreteFactory1
    AbstractFactory <|.. ConcreteFactory2

    AbstractProductA <|-- ProductA1
    AbstractProductA <|-- ProductA2
    AbstractProductB <|-- ProductB1
    AbstractProductB <|-- ProductB2

    Client --> AbstractFactory
    Client --> AbstractProductA
    Client --> AbstractProductB

    class AbstractFactory {
        +CreateProductA() : AbstractProductA
        +CreateProductB() : AbstractProductB
    }

    class ConcreteFactory1 {
        +CreateProductA() : ProductA1
        +CreateProductB() : ProductB1
    }

    class ConcreteFactory2 {
        +CreateProductA() : ProductA2
        +CreateProductB() : ProductB2
    }

    class AbstractProductA {
        <<interface>>
    }

    class AbstractProductB {
        <<interface>>
    }

    class ProductA1
    class ProductA2
    class ProductB1
    class ProductB2
Loading

Steps to reproduce

This is plain minimalistic HTML. Save it in filesystem and open in the browser: you will see error icon.


<!DOCTYPE html>
<html lang="en" charset="utf-8">

</head>
<body>
	
<h2 id="class-diagram-with--createproducta--producta1">Class Diagram with <code> +CreateProductA() : ProductA1</code></h2>

<br/>

<pre class="mermaid">classDiagram
    AbstractFactory <|.. ConcreteFactory1
    AbstractFactory <|.. ConcreteFactory2

    AbstractProductA <|-- ProductA1
    AbstractProductA <|-- ProductA2
    AbstractProductB <|-- ProductB1
    AbstractProductB <|-- ProductB2

    Client --> AbstractFactory
    Client --> AbstractProductA
    Client --> AbstractProductB

    class AbstractFactory {
        +CreateProductA() : AbstractProductA
        +CreateProductB() : AbstractProductB
    }

    class ConcreteFactory1 {
        +CreateProductA() : ProductA1
        +CreateProductB() : ProductB1
    }

    class ConcreteFactory2 {
        +CreateProductA() : ProductA2
        +CreateProductB() : ProductB2
    }

    class AbstractProductA {
        <<interface>>
    }

    class AbstractProductB {
        <<interface>>
    }

    class ProductA1
    class ProductA2
    class ProductB1
    class ProductB2
</pre>
  <script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.esm.min.mjs';
    mermaid.initialize({ startOnLoad: true });
  </script>
</body>
</html>

Screenshots

No response

Code Sample

No response

Setup

  • Mermaid version:
  • Browser and Version: [Chrome, Edge, Firefox]

Suggested Solutions

No response

Additional Context

No response

@FuadEfendi FuadEfendi added Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect labels Nov 23, 2024
@FuadEfendi
Copy link
Author

If I replace ( with #40; and ) with #41; it will work; however, it is just workaround. Why it works at GitHub, do GitHub use different patched version?

@jmooring
Copy link

jmooring commented Nov 23, 2024

The error is triggered by this bit:

class AbstractProductA {
    <<interface>>
}

class AbstractProductB {
    <<interface>>
}

The HTML above is invalid. You can fix it with:

class AbstractProductA {
    &lt;&lt;interface&gt;&gt;
}

class AbstractProductB {
    &lt;&lt;interface&gt;&gt;
}

@FuadEfendi
Copy link
Author

Yes, invalid, but GitHub handles that somehow... I tried to remove <<interface>> in plain HTML file and it works.

This is another "workaround" to make it work:

   class AbstractProductA {
        <~interface>~
    }

    class AbstractProductB {
        <~interface>~
    }

@FuadEfendi
Copy link
Author

Ok, if we open source of this GitHub page, it escapes:


class ConcreteFactory2 {
--
  | +CreateProductA() : ProductA2
  | +CreateProductB() : ProductB2
  | }
  |  
  | class AbstractProductA {
  | &lt;&lt;interface&gt;&gt;
  | }
  |  
  | class AbstractProductB {
  | &lt;&lt;interface&gt;&gt;
  | }

@jgreywolf
Copy link
Contributor

This is odd, since this has "always" worked in the past. The "hack" fix of <~interface>~ is less than ideal given that the ~ character is used for declaring generic types (I am working on fixing this).

@FuadEfendi
Copy link
Author

FuadEfendi commented Nov 23, 2024

This is another "inconvenience":

graph TD;
    A[Gross Domestic Product (GDP)] --> B[Consumer Spending (C)];
    A --> C[Investment (I)];
    A --> D[Government Spending (G)];
    A --> E[Net Exports (NX)];
    E --> F[Exports (X)];
    E --> G[Imports (M)];
Loading

However, if I wrap ["Investment (I)"], or if I use #40; and #41; instead of brackets, it won't crash:

graph TD;
    A["Gross Domestic Product (GDP)"] --> B["Consumer Spending (C)"];
    A --> C["Investment (I)"];
    A --> D["Government Spending (G)"];
    A --> E["Net Exports (NX)"];
    E --> F["Exports (X)"];
    E --> G["Imports (M)"];
Loading
```mermaid
graph TD;
    A["Gross Domestic Product (GDP)"] --> B["Consumer Spending (C)"];
    A --> C["Investment (I)"];
    A --> D["Government Spending (G)"];
    A --> E["Net Exports (NX)"];
    E --> F["Exports (X)"];
    E --> G["Imports (M)"];
```

@jgreywolf
Copy link
Contributor

Both flowcharts and class diagrams do not allow spaces in the "id" portion of the definition. If you need to show spaces in the diagram, you can add a label (like you have above).

https://mermaid.js.org/syntax/flowchart.html#a-node-with-text
https://mermaid.js.org/syntax/classDiagram.html#class-labels

@jgreywolf jgreywolf self-assigned this Nov 23, 2024
@jgreywolf jgreywolf added Graph: Class Status: In progress and removed Status: Triage Needs to be verified, categorized, etc labels Nov 23, 2024
@FuadEfendi
Copy link
Author

FuadEfendi commented Nov 24, 2024

Thanks for working on it; what is "id portion of definition", can you provide example? With class diagram, if I just replace E --> G[Imports (M)]; onto either E --> G[Imports #40;M#41;]; or E --> G["Imports (M)"]; it will fix the issue, although it looks like "workaround".

I believe "id" in C[Investment (I)] is "C", right?

@FuadEfendi
Copy link
Author

FuadEfendi commented Nov 25, 2024

graph TD;
    A["Tuple: {:ok, #34;Success#34;, 200}"]
    A --> B[:ok]
    A --> C["Success"]
    A --> D[200]
Loading

Note: quotes in C["Success"] are not rendered on chart. Maybe we can use some escaping for embedded quotes?

```mermaid
graph TD;
    A["Tuple: {:ok, #34;Success#34;, 200}"]
    A --> B[:ok]
    A --> C["Success"]
    A --> D[200]
```
graph TD;
    A["Map: %{#34;name#34; => #34;Alice#34;, #34;age#34; => 30, #34;city#34; => #34;New York#34;}"]
    A --> B[#34;name#34; => #34;Alice#34;]
    A --> C[#34;age#34; => 30]
    A --> D[#34;city#34; => #34;New York#34;]
Loading
```mermaid
graph TD;
    A["Map: %{#34;name#34; => #34;Alice#34;, #34;age#34; => 30, #34;city#34; => #34;New York#34;}"]
    A --> B[#34;name#34; => #34;Alice#34;]
    A --> C[#34;age#34; => 30]
    A --> D[#34;city#34; => #34;New York#34;]
```

@jgreywolf
Copy link
Contributor

Thanks for working on it; what is "id portion of definition", can you provide example? With class diagram, if I just replace E --> G[Imports (M)]; onto either E --> G[Imports #40;M#41;]; or E --> G["Imports (M)"]; it will fix the issue, although it looks like "workaround".

I believe "id" in C[Investment (I)] is "C", right?

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Graph: Class Status: In progress Type: Bug / Error Something isn't working or is incorrect
Projects
None yet
Development

No branches or pull requests

3 participants