fix #3391: Remove flowchart as fallback for diagram detection.

This commit is contained in:
Sidharth Vinod
2022-10-06 19:14:25 +08:00
parent d59f878020
commit 1615c6d9f9
6 changed files with 53 additions and 25 deletions

View File

@@ -0,0 +1,14 @@
<html>
<body>
<pre class="mermaid">
none
hello world
</pre>
<script src="./mermaid.js"></script>
<script>
mermaid.initialize({
logLevel: 1,
});
</script>
</body>
</html>

View File

@@ -48,7 +48,8 @@
"e2e": "start-server-and-test dev http://localhost:9000/ cypress", "e2e": "start-server-and-test dev http://localhost:9000/ cypress",
"ci": "vitest run", "ci": "vitest run",
"test": "pnpm lint && vitest run", "test": "pnpm lint && vitest run",
"test:watch": "vitest --coverage --watch", "test:watch": "vitest --watch",
"test:coverage": "vitest --coverage",
"prepublishOnly": "pnpm build && pnpm test", "prepublishOnly": "pnpm build && pnpm test",
"prepare": "concurrently \"husky install\" \"pnpm build\"", "prepare": "concurrently \"husky install\" \"pnpm build\"",
"pre-commit": "lint-staged" "pre-commit": "lint-staged"

View File

@@ -8,11 +8,18 @@ export class Diagram {
parser; parser;
renderer; renderer;
db; db;
private detectTypeFailed = false;
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
constructor(public txt: string, parseError?: Function) { constructor(public txt: string, parseError?: Function) {
const cnf = configApi.getConfig(); const cnf = configApi.getConfig();
this.txt = txt; this.txt = txt;
try {
this.type = detectType(txt, cnf); this.type = detectType(txt, cnf);
} catch (e) {
this.handleError(e, parseError);
this.type = 'error';
this.detectTypeFailed = true;
}
const diagram = getDiagram(this.type); const diagram = getDiagram(this.type);
log.debug('Type ' + this.type); log.debug('Type ' + this.type);
// Setup diagram // Setup diagram
@@ -32,12 +39,22 @@ export class Diagram {
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
parse(text: string, parseError?: Function): boolean { parse(text: string, parseError?: Function): boolean {
if (this.detectTypeFailed) {
return false;
}
try { try {
text = text + '\n'; text = text + '\n';
this.db.clear(); this.db.clear();
this.parser.parse(text); this.parser.parse(text);
return true; return true;
} catch (error) { } catch (error) {
this.handleError(error, parseError);
}
return false;
}
// eslint-disable-next-line @typescript-eslint/ban-types
handleError(error: unknown, parseError?: Function) {
// Is this the correct way to access mermiad's parseError() // Is this the correct way to access mermiad's parseError()
// method ? (or global.mermaid.parseError()) ? // method ? (or global.mermaid.parseError()) ?
if (parseError) { if (parseError) {
@@ -54,8 +71,6 @@ export class Diagram {
throw error; throw error;
} }
} }
return false;
}
getParser() { getParser() {
return this.parser; return this.parser;

View File

@@ -35,16 +35,13 @@ const detectors: Record<string, DetectorRecord> = {};
export const detectType = function (text: string, config?: MermaidConfig): string { export const detectType = function (text: string, config?: MermaidConfig): string {
text = text.replace(directive, '').replace(anyComment, '\n'); text = text.replace(directive, '').replace(anyComment, '\n');
// console.log(detectors);
for (const [key, detectorRecord] of Object.entries(detectors)) { for (const [key, detectorRecord] of Object.entries(detectors)) {
if (detectorRecord.detector(text, config)) { if (detectorRecord.detector(text, config)) {
return key; return key;
} }
} }
// TODO: #3391
// throw new Error(`No diagram type detected for text: ${text}`); throw new Error(`No diagram type detected for text: ${text}`);
return 'flowchart';
}; };
export const addDetector = (key: string, detector: DiagramDetector, path: string) => { export const addDetector = (key: string, detector: DiagramDetector, path: string) => {

View File

@@ -15,7 +15,9 @@ describe('DiagramAPI', () => {
it('should handle diagram registrations', () => { it('should handle diagram registrations', () => {
expect(() => getDiagram('loki')).toThrow(); expect(() => getDiagram('loki')).toThrow();
expect(() => detectType('loki diagram')).not.toThrow(); // TODO: #3391 expect(() => detectType('loki diagram')).toThrow(
'No diagram type detected for text: loki diagram'
);
const detector: DiagramDetector = (str: string) => { const detector: DiagramDetector = (str: string) => {
return str.match('loki') !== null; return str.match('loki') !== null;
}; };

View File

@@ -429,8 +429,7 @@ export const clear = function (ver = 'gen-1') {
vertices = {}; vertices = {};
classes = {}; classes = {};
edges = []; edges = [];
funs = []; funs = [setupToolTips];
funs.push(setupToolTips);
subGraphs = []; subGraphs = [];
subGraphLookup = {}; subGraphLookup = {};
subCount = 0; subCount = 0;