mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-26 10:49:38 +02:00
update mermaidAPI to cleanup the text before passing to getDiagramFromText
This commit is contained in:
@@ -8,8 +8,6 @@ import { encodeEntities } from './utils.js';
|
|||||||
import type { DetailedError } from './utils.js';
|
import type { DetailedError } from './utils.js';
|
||||||
import type { DiagramDefinition, DiagramMetadata } from './diagram-api/types.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;
|
export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,7 +23,7 @@ export class Diagram {
|
|||||||
|
|
||||||
private detectError?: UnknownDiagramError;
|
private detectError?: UnknownDiagramError;
|
||||||
constructor(public text: string, public metadata: Pick<DiagramMetadata, 'title'> = {}) {
|
constructor(public text: string, public metadata: Pick<DiagramMetadata, 'title'> = {}) {
|
||||||
this.text = encodeEntities(cleanupComments(text));
|
this.text = encodeEntities(text);
|
||||||
this.text += '\n';
|
this.text += '\n';
|
||||||
const cnf = configApi.getConfig();
|
const cnf = configApi.getConfig();
|
||||||
try {
|
try {
|
||||||
|
@@ -83,14 +83,4 @@ Expecting 'TXT', got 'NEWLINE'"
|
|||||||
expect(messages[0].message).toBe('I fl°°9829¶ß you!');
|
expect(messages[0].message).toBe('I fl°°9829¶ß you!');
|
||||||
expect(messages[1].message).toBe('I fl°°9829¶ß you fl°infin¶ß times more!');
|
expect(messages[1].message).toBe('I fl°°9829¶ß you fl°infin¶ß times more!');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should clean up comments inside getDiagramFromText when present in diagram definition', async () => {
|
|
||||||
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');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
@@ -67,6 +67,7 @@ vi.mock('stylis', () => {
|
|||||||
});
|
});
|
||||||
import { compile, serialize } from 'stylis';
|
import { compile, serialize } from 'stylis';
|
||||||
import { decodeEntities, encodeEntities } from './utils.js';
|
import { decodeEntities, encodeEntities } from './utils.js';
|
||||||
|
import { Diagram } from './Diagram.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://vitest.dev/guide/mocking.html Mock part of a module
|
* @see https://vitest.dev/guide/mocking.html Mock part of a module
|
||||||
@@ -744,4 +745,16 @@ describe('mermaidAPI', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getDiagramFromText', () => {
|
||||||
|
it('should clean up comments when present in diagram definition', async () => {
|
||||||
|
const diagram = await mermaidAPI.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');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -17,7 +17,7 @@ import { compile, serialize, stringify } from 'stylis';
|
|||||||
import { version } from '../package.json';
|
import { version } from '../package.json';
|
||||||
import * as configApi from './config.js';
|
import * as configApi from './config.js';
|
||||||
import { addDiagrams } from './diagram-api/diagram-orchestration.js';
|
import { addDiagrams } from './diagram-api/diagram-orchestration.js';
|
||||||
import { Diagram, getDiagramFromText } from './Diagram.js';
|
import { Diagram, getDiagramFromText as getDiagramFromTextInternal } from './Diagram.js';
|
||||||
import errorRenderer from './diagrams/error/errorRenderer.js';
|
import errorRenderer from './diagrams/error/errorRenderer.js';
|
||||||
import { attachFunctions } from './interactionDb.js';
|
import { attachFunctions } from './interactionDb.js';
|
||||||
import { log, setLogLevel } from './logger.js';
|
import { log, setLogLevel } from './logger.js';
|
||||||
@@ -28,7 +28,7 @@ import type { MermaidConfig } from './config.type.js';
|
|||||||
import { evaluate } from './diagrams/common/common.js';
|
import { evaluate } from './diagrams/common/common.js';
|
||||||
import isEmpty from 'lodash-es/isEmpty.js';
|
import isEmpty from 'lodash-es/isEmpty.js';
|
||||||
import { setA11yDiagramInfo, addSVGa11yTitleDescription } from './accessibility.js';
|
import { setA11yDiagramInfo, addSVGa11yTitleDescription } from './accessibility.js';
|
||||||
import type { DiagramStyleClassDef } from './diagram-api/types.js';
|
import type { DiagramMetadata, DiagramStyleClassDef } from './diagram-api/types.js';
|
||||||
import { preprocessDiagram } from './preprocess.js';
|
import { preprocessDiagram } from './preprocess.js';
|
||||||
import { decodeEntities } from './utils.js';
|
import { decodeEntities } from './utils.js';
|
||||||
|
|
||||||
@@ -519,6 +519,11 @@ function initialize(options: MermaidConfig = {}) {
|
|||||||
addDiagrams();
|
addDiagrams();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getDiagramFromText = (text: string, metadata: Pick<DiagramMetadata, 'title'> = {}) => {
|
||||||
|
const { code } = preprocessDiagram(text);
|
||||||
|
return getDiagramFromTextInternal(code, metadata);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add accessibility (a11y) information to the diagram.
|
* Add accessibility (a11y) information to the diagram.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user