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

fix: clean comments in text in getDiagramFromText API so flowchart works well #5076

6 changes: 5 additions & 1 deletion demos/flowchart.html
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,11 @@ <h3>flowchart</h3>
class I bugged_node
</pre>
<hr />

<pre class="mermaid">
flowchart LR
%% this is a comment A -- text --> B{node}
A -- text --> B -- text2 --> C
</pre>
ad1992 marked this conversation as resolved.
Show resolved Hide resolved
<h1 id="link-clicked">Anchor for "link-clicked" test</h1>

<script type="module">
Expand Down
4 changes: 3 additions & 1 deletion packages/mermaid/src/Diagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { encodeEntities } from './utils.js';
import type { DetailedError } from './utils.js';
import type { DiagramDefinition, DiagramMetadata } from './diagram-api/types.js';

import { cleanupComments } from './diagram-api/comments.js';

export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void;

/**
Expand All @@ -23,7 +25,7 @@ export class Diagram {

private detectError?: UnknownDiagramError;
constructor(public text: string, public metadata: Pick<DiagramMetadata, 'title'> = {}) {
this.text = encodeEntities(text);
this.text = encodeEntities(cleanupComments(text));
ad1992 marked this conversation as resolved.
Show resolved Hide resolved
this.text += '\n';
const cnf = configApi.getConfig();
try {
Expand Down
10 changes: 10 additions & 0 deletions packages/mermaid/src/diagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ Expecting 'TXT', got 'NEWLINE'"
expect(messages[0].message).toBe('I fl°°9829¶ß you!');
expect(messages[1].message).toBe('I fl°°9829¶ß you fl°infin¶ß times more!');
});

test('should clean up comments when present in diagram defination', async () => {
ad1992 marked this conversation as resolved.
Show resolved Hide resolved
const diagram = await getDiagramFromText(
`flowchart LR
%% this is a comment A -- text --> B{node}
A -- text --> B -- text2 --> C`
);
expect(diagram).toBeInstanceOf(Diagram);
expect(diagram.type).toBe('flowchart-v2');
});
});
Loading