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

Allow registered diagrams to be overridden. #5050

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/mermaid/src/diagram-api/detectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@

export const addDetector = (key: string, detector: DiagramDetector, loader?: DiagramLoader) => {
if (detectors[key]) {
log.error(`Detector with key ${key} already exists`);
} else {
detectors[key] = { detector, loader };
log.warn(`Detector with key ${key} already exists. Overwriting.`);

Check warning on line 74 in packages/mermaid/src/diagram-api/detectType.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagram-api/detectType.ts#L74

Added line #L74 was not covered by tests
}
detectors[key] = { detector, loader };

Check warning on line 76 in packages/mermaid/src/diagram-api/detectType.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagram-api/detectType.ts#L76

Added line #L76 was not covered by tests
log.debug(`Detector with key ${key} added${loader ? ' with loader' : ''}`);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagram-api/diagramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
detector?: DiagramDetector
) => {
if (diagrams[id]) {
throw new Error(`Diagram ${id} already registered.`);
log.warn(`Diagram with id ${id} already registered. Overwriting.`);

Check warning on line 52 in packages/mermaid/src/diagram-api/diagramAPI.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagram-api/diagramAPI.ts#L52

Added line #L52 was not covered by tests
}
diagrams[id] = diagram;
if (detector) {
Expand Down
56 changes: 37 additions & 19 deletions packages/mermaid/src/diagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,32 @@ import { describe, test, expect } from 'vitest';
import { Diagram, getDiagramFromText } from './Diagram.js';
import { addDetector } from './diagram-api/detectType.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js';
import type { DiagramLoader } from './diagram-api/types.js';

addDiagrams();

const getDummyDiagram = (id: string, title?: string): Awaited<ReturnType<DiagramLoader>> => {
return {
id,
diagram: {
db: {
getDiagramTitle: () => title ?? id,
},
parser: {
parse: () => {
// no-op
},
},
renderer: {
draw: () => {
// no-op
},
},
styles: {},
},
};
};

describe('diagram detection', () => {
test('should detect inbuilt diagrams', async () => {
const graph = (await getDiagramFromText('graph TD; A-->B')) as Diagram;
Expand All @@ -21,30 +44,25 @@ describe('diagram detection', () => {
addDetector(
'loki',
(str) => str.startsWith('loki'),
() =>
Promise.resolve({
id: 'loki',
diagram: {
db: {},
parser: {
parse: () => {
// no-op
},
},
renderer: {
draw: () => {
// no-op
},
},
styles: {},
},
})
() => Promise.resolve(getDummyDiagram('loki'))
);
const diagram = (await getDiagramFromText('loki TD; A-->B')) as Diagram;
const diagram = await getDiagramFromText('loki TD; A-->B');
expect(diagram).toBeInstanceOf(Diagram);
expect(diagram.type).toBe('loki');
});

test('should allow external diagrams to override internal ones with same ID', async () => {
const title = 'overridden';
addDetector(
'flowchart-elk',
(str) => str.startsWith('flowchart-elk'),
() => Promise.resolve(getDummyDiagram('flowchart-elk', title))
);
const diagram = await getDiagramFromText('flowchart-elk TD; A-->B');
expect(diagram).toBeInstanceOf(Diagram);
expect(diagram.db.getDiagramTitle?.()).toBe(title);
});

test('should throw the right error for incorrect diagram', async () => {
await expect(getDiagramFromText('graph TD; A-->')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Parse error on line 2:
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { isDetailedError } from './utils.js';
import type { DetailedError } from './utils.js';
import type { ExternalDiagramDefinition } from './diagram-api/types.js';
import type { UnknownDiagramError } from './errors.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js';

export type {
MermaidConfig,
Expand Down Expand Up @@ -243,6 +244,7 @@ const registerExternalDiagrams = async (
lazyLoad?: boolean;
} = {}
) => {
addDiagrams();
registerLazyLoadedDiagrams(...diagrams);
if (lazyLoad === false) {
await loadRegisteredDiagrams();
Expand Down
Loading