From accba3f408d1f1a9a68a05fff9899e2d51627fe7 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Thu, 1 Dec 2022 22:43:03 +0000 Subject: [PATCH] chore: improve errors for bad YAML frontmatter Adds a custom error message for any mermaid diagram that starts with a `---`. Normally, these are expected to be part of a YAML front-matter block, but indentation issues or a missing closing `---` may cause these to be not parsed correctly. --- .../src/diagram-api/diagram-orchestration.ts | 27 +++++++++++++++++++ packages/mermaid/src/mermaidAPI.spec.ts | 13 +++++++++ packages/mermaid/src/utils.spec.js | 4 +-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/diagram-api/diagram-orchestration.ts b/packages/mermaid/src/diagram-api/diagram-orchestration.ts index a26edb303..6c7ab6907 100644 --- a/packages/mermaid/src/diagram-api/diagram-orchestration.ts +++ b/packages/mermaid/src/diagram-api/diagram-orchestration.ts @@ -125,6 +125,33 @@ export const addDiagrams = () => { }, (text) => text.toLowerCase().trim() === 'error' ); + registerDiagram( + '---', + // --- diagram type may appear if YAML front-matter is not parsed correctly + { + db: { + clear: () => { + // Quite ok, clear needs to be there for --- to work as a regular diagram + }, + }, + styles: errorStyles, // should never be used + renderer: errorRenderer, // should never be used + parser: { + parser: { yy: {} }, + parse: () => { + throw new Error( + 'Diagrams beginning with --- are not valid. ' + + '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" + ); + }, + }, + init: () => null, // no op + }, + (text) => { + return text.toLowerCase().trimStart().startsWith('---'); + } + ); registerDiagram( 'c4', diff --git a/packages/mermaid/src/mermaidAPI.spec.ts b/packages/mermaid/src/mermaidAPI.spec.ts index 55d46ae7c..092661dc6 100644 --- a/packages/mermaid/src/mermaidAPI.spec.ts +++ b/packages/mermaid/src/mermaidAPI.spec.ts @@ -629,6 +629,19 @@ describe('mermaidAPI', function () { expect(mermaid.parseError).toEqual(undefined); expect(() => mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow(); }); + it('throws for a nicer error for a invalid definition starting with `---`', function () { + expect(mermaid.parseError).toEqual(undefined); + expect(() => + mermaidAPI.parse(` + --- + title: a malformed YAML front-matter + `) + ).toThrow( + 'Diagrams beginning with --- are not valid. ' + + '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" + ); + }); it('does not throw for a valid definition', function () { expect(() => mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow(); }); diff --git a/packages/mermaid/src/utils.spec.js b/packages/mermaid/src/utils.spec.js index 54262f10e..bdf94d992 100644 --- a/packages/mermaid/src/utils.spec.js +++ b/packages/mermaid/src/utils.spec.js @@ -238,9 +238,9 @@ Alice->Bob: hi`; const type = detectType(str); expect(type).toBe('gitGraph'); }); - it('should not allow frontmatter with leading spaces', function () { + it('should handle malformed frontmatter (with leading spaces) with `---` error graphtype', function () { const str = ' ---\ntitle: foo\n---\n gitGraph TB:\nbfs1:queue'; - expect(() => detectType(str)).toThrow('No diagram type detected for text'); + expect(detectType(str)).toBe('---'); }); }); describe('when finding substring in array ', function () {