fix: Detector order

This commit is contained in:
Sidharth Vinod
2023-02-21 23:00:03 +05:30
parent 0df8c149f9
commit 1ac219282b
3 changed files with 50 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
import { it, describe, expect } from 'vitest';
import { detectType } from './detectType';
import { addDiagrams } from './diagram-orchestration';
describe('diagram-orchestration', () => {
it('should register diagrams', () => {
expect(() => detectType('graph TD; A-->B')).toThrow();
addDiagrams();
expect(detectType('graph TD; A-->B')).toBe('flowchart');
});
describe('proper diagram types should be detetced', () => {
beforeAll(() => {
addDiagrams();
});
it.each([
{ text: 'graph TD;', expected: 'flowchart' },
{ text: 'flowchart TD;', expected: 'flowchart-v2' },
{ text: 'flowchart-v2 TD;', expected: 'flowchart-v2' },
{ text: 'flowchart-elk TD;', expected: 'flowchart-elk' },
{ text: 'error', expected: 'error' },
{ text: 'C4Context;', expected: 'c4' },
{ text: 'classDiagram', expected: 'class' },
{ text: 'classDiagram-v2', expected: 'classDiagram' },
{ text: 'erDiagram', expected: 'er' },
{ text: 'journey', expected: 'journey' },
{ text: 'gantt', expected: 'gantt' },
{ text: 'pie', expected: 'pie' },
{ text: 'requirementDiagram', expected: 'requirement' },
{ text: 'info', expected: 'info' },
{ text: 'sequenceDiagram', expected: 'sequence' },
{ text: 'mindmap', expected: 'mindmap' },
{ text: 'timeline', expected: 'timeline' },
{ text: 'gitGraph', expected: 'gitGraph' },
{ text: 'stateDiagram', expected: 'state' },
{ text: 'stateDiagram-v2', expected: 'stateDiagram' },
])('should $text be detected as $expected', ({ text, expected }) => {
expect(detectType(text)).toBe(expected);
});
});
});

View File

@@ -45,7 +45,7 @@ export const addDiagrams = () => {
throw new Error( throw new Error(
'Diagrams beginning with --- are not valid. ' + 'Diagrams beginning with --- are not valid. ' +
'If you were trying to use a YAML front-matter, please ensure that ' + 'If you were trying to use a YAML front-matter, please ensure that ' +
"you've correctly opened and closed the YAML front-matter with unindented `---` blocks" "you've correctly opened and closed the YAML front-matter with un-indented `---` blocks"
); );
}, },
}, },
@@ -55,25 +55,28 @@ export const addDiagrams = () => {
return text.toLowerCase().trimStart().startsWith('---'); return text.toLowerCase().trimStart().startsWith('---');
} }
); );
// Ordering of detectors is important. The first one to return true will be used.
registerLazyLoadedDiagrams( registerLazyLoadedDiagrams(
error, error,
c4, c4,
classDiagram,
classDiagramV2, classDiagramV2,
classDiagram,
er, er,
gantt, gantt,
info, info,
pie, pie,
requirement, requirement,
sequence, sequence,
flowchartElk,
// TODO @knsv: Should v2 come before flowchart?
// This will fail few unit tests as they expect graph to be detected as flowchart, but it is detected as flowchart-v2.
flowchart, flowchart,
flowchartV2, flowchartV2,
flowchartElk,
mindmap, mindmap,
timeline, timeline,
git, git,
state,
stateV2, stateV2,
state,
journey journey
); );
}; };

View File

@@ -666,7 +666,7 @@ describe('mermaidAPI', () => {
).rejects.toThrow( ).rejects.toThrow(
'Diagrams beginning with --- are not valid. ' + 'Diagrams beginning with --- are not valid. ' +
'If you were trying to use a YAML front-matter, please ensure that ' + 'If you were trying to use a YAML front-matter, please ensure that ' +
"you've correctly opened and closed the YAML front-matter with unindented `---` blocks" "you've correctly opened and closed the YAML front-matter with un-indented `---` blocks"
); );
}); });
it('does not throw for a valid definition', async () => { it('does not throw for a valid definition', async () => {