feat: Add diagramType to RenderResult and ParseResult #5117

This commit is contained in:
Sidharth Vinod
2023-12-06 21:11:07 +05:30
parent 000b432bb2
commit c3c7ccd78a
5 changed files with 78 additions and 32 deletions

View File

@@ -6,7 +6,7 @@ import { dedent } from 'ts-dedent';
import type { MermaidConfig } from './config.type.js';
import { log } from './logger.js';
import utils from './utils.js';
import type { ParseOptions, RenderResult } from './mermaidAPI.js';
import type { ParseOptions, ParseResult, RenderResult } from './mermaidAPI.js';
import { mermaidAPI } from './mermaidAPI.js';
import { registerLazyLoadedDiagrams, detectType } from './diagram-api/detectType.js';
import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js';
@@ -24,6 +24,7 @@ export type {
ParseErrorFunction,
RenderResult,
ParseOptions,
ParseResult,
UnknownDiagramError,
};
@@ -314,10 +315,10 @@ const executeQueue = async () => {
* Parse the text and validate the syntax.
* @param text - The mermaid diagram definition.
* @param parseOptions - Options for parsing.
* @returns true if the diagram is valid, false otherwise if parseOptions.suppressErrors is true.
* @returns - If the diagram is valid, returns an object with isValid set to true and the diagramType set to type of the diagram.
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false.
*/
const parse = async (text: string, parseOptions?: ParseOptions): Promise<boolean | void> => {
const parse = async (text: string, parseOptions?: ParseOptions): Promise<ParseResult | void> => {
return new Promise((resolve, reject) => {
// This promise will resolve when the render call is done.
// It will be queued first and will be executed when it is first in line

View File

@@ -1,5 +1,5 @@
'use strict';
import { vi } from 'vitest';
import { vi, it, expect, describe, beforeEach } from 'vitest';
// -------------------------------------
// Mocks and mocking
@@ -682,17 +682,26 @@ describe('mermaidAPI', () => {
it('returns false for invalid definition with silent option', async () => {
await expect(
mermaidAPI.parse('this is not a mermaid diagram definition', { suppressErrors: true })
).resolves.toBe(false);
).resolves.toStrictEqual({ isValid: false });
});
it('resolves for valid definition', async () => {
await expect(
mermaidAPI.parse('graph TD;A--x|text including URL space|B;')
).resolves.toBeTruthy();
await expect(mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).resolves
.toMatchInlineSnapshot(`
{
"diagramType": "flowchart-v2",
"isValid": true,
}
`);
});
it('returns true for valid definition with silent option', async () => {
await expect(
mermaidAPI.parse('graph TD;A--x|text including URL space|B;', { suppressErrors: true })
).resolves.toBe(true);
).resolves.toMatchInlineSnapshot(`
{
"diagramType": "flowchart-v2",
"isValid": true,
}
`);
});
});
@@ -734,7 +743,8 @@ describe('mermaidAPI', () => {
it('should set aria-roledscription to the diagram type AND should call addSVGa11yTitleDescription', async () => {
const a11yDiagramInfo_spy = vi.spyOn(accessibility, 'setA11yDiagramInfo');
const a11yTitleDesc_spy = vi.spyOn(accessibility, 'addSVGa11yTitleDescription');
await mermaidAPI.render(id, diagramText);
const result = await mermaidAPI.render(id, diagramText);
expect(result.diagramType).toBe(expectedDiagramType);
expect(a11yDiagramInfo_spy).toHaveBeenCalledWith(
expect.anything(),
expectedDiagramType

View File

@@ -60,6 +60,18 @@ export interface ParseOptions {
suppressErrors?: boolean;
}
export type ParseResult =
| {
isValid: true;
/**
* The diagram type, e.g. 'flowchart', 'sequence', etc.
*/
diagramType: string;
}
| {
isValid: false;
};
// This makes it clear that we're working with a d3 selected element of some kind, even though it's hard to specify the exact type.
export type D3Element = any;
@@ -68,6 +80,10 @@ export interface RenderResult {
* The svg code for the rendered graph.
*/
svg: string;
/**
* The diagram type, e.g. 'flowchart', 'sequence', etc.
*/
diagramType: string;
/**
* Bind function to be called after the svg has been inserted into the DOM.
* This is necessary for adding event listeners to the elements in the svg.
@@ -91,28 +107,24 @@ function processAndSetConfigs(text: string) {
* Parse the text and validate the syntax.
* @param text - The mermaid diagram definition.
* @param parseOptions - Options for parsing.
* @returns true if the diagram is valid, false otherwise if parseOptions.suppressErrors is true.
* @returns - If the diagram is valid, returns an object with isValid set to true and the diagramType set to type of the diagram.
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false.
*/
async function parse(text: string, parseOptions?: ParseOptions): Promise<boolean> {
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult> {
addDiagrams();
text = processAndSetConfigs(text).code;
try {
await getDiagramFromText(text);
const { code } = processAndSetConfigs(text);
const diagram = await getDiagramFromText(code);
return { isValid: true, diagramType: diagram.type };
} catch (error) {
if (parseOptions?.suppressErrors) {
return false;
return { isValid: false };
}
throw error;
}
return true;
}
// append !important; to each cssClass followed by a final !important, all enclosed in { }
//
/**
* Create a CSS style that starts with the given class name, then the element,
* with an enclosing block that has each of the cssClasses followed by !important;
@@ -483,6 +495,7 @@ const render = async function (
}
return {
diagramType,
svg: svgCode,
bindFunctions: diag.db.bindFunctions,
};