Skip to content

Commit

Permalink
Disallow leading whitespace before delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonM committed Nov 19, 2022
1 parent 70f0247 commit a11ab3d
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 52 deletions.
10 changes: 5 additions & 5 deletions cypress/integration/rendering/classDiagram-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,11 @@ describe('Class diagram V2', () => {
it('1433: should render a simple class with a title', () => {
imgSnapshotTest(
`---
title: simple class diagram
---
classDiagram-v2
class Class10
`,
title: simple class diagram
---
classDiagram-v2
class Class10
`,
{}
);
});
Expand Down
12 changes: 6 additions & 6 deletions cypress/integration/rendering/erDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ describe('Entity Relationship Diagram', () => {
it('1433: should render a simple ER diagram with a title', () => {
imgSnapshotTest(
`---
title: simple ER diagram
---
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
`,
title: simple ER diagram
---
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
`,
{}
);
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/integration/rendering/flowchart-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,11 @@ flowchart RL
it('1433: should render a titled flowchart with titleTopMargin set to 0', () => {
imgSnapshotTest(
`---
title: Simple flowchart
---
flowchart TD
A --> B
`,
title: Simple flowchart
---
flowchart TD
A --> B
`,
{ titleTopMargin: 0 }
);
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/integration/rendering/gitGraph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ describe('Git Graph diagram', () => {
it('1433: should render a simple gitgraph with a title', () => {
imgSnapshotTest(
`---
title: simple gitGraph
---
gitGraph
commit
`,
title: simple gitGraph
---
gitGraph
commit
`,
{}
);
});
Expand Down
12 changes: 6 additions & 6 deletions cypress/integration/rendering/stateDiagram-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,12 @@ stateDiagram-v2
it('1433: should render a simple state diagram with a title', () => {
imgSnapshotTest(
`---
title: simple state diagram
---
stateDiagram-v2
[*] --> State1
State1 --> [*]
`,
title: simple state diagram
---
stateDiagram-v2
[*] --> State1
State1 --> [*]
`,
{}
);
});
Expand Down
6 changes: 3 additions & 3 deletions demos/classchart.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<h1>Class diagram demos</h1>

<pre class="mermaid">
---
title: Demo Class Diagram
---
---
title: Demo Class Diagram
---
classDiagram
accTitle: Demo Class Diagram
accDescr: This class diagram show the abstract Animal class, and 3 classes that inherit from it: Duck, Fish, and Zebra.
Expand Down
12 changes: 6 additions & 6 deletions demos/flowchart.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ <h1>Comparison "graph vs. flowchart"</h1>
<h2>Sample 1</h2>
<h3>graph</h3>
<pre class="mermaid">
---
title: This is a complicated flow
---
---
title: This is a complicated flow
---
graph LR
accTitle: This is a complicated flow
accDescr: This is the descriptoin for the complicated flow.
Expand Down Expand Up @@ -224,9 +224,9 @@ <h3>flowchart</h3>
<h2>Sample 2</h2>
<h3>graph</h3>
<pre class="mermaid">
---
title: What to buy
---
---
title: What to buy
---
graph TD
accTitle: What to buy
accDescr: Options of what to buy with Christmas money
Expand Down
6 changes: 3 additions & 3 deletions demos/git.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<body>
<h1>Git diagram demo</h1>
<pre class="mermaid">
---
title: Simple Git diagram
---
---
title: Simple Git diagram
---
gitGraph:
options
{
Expand Down
6 changes: 3 additions & 3 deletions demos/journey.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<body>
<h1>Journey diagram demo</h1>
<pre class="mermaid">
---
title: My working day
---
---
title: My working day
---
journey
accTitle: Very simple journey demo
accDescr: 2 main sections: work and home, each with just a few tasks
Expand Down
12 changes: 6 additions & 6 deletions demos/state.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<h1>State diagram demos</h1>
<h2>Very simple showing change from State1 to State2</h2>
<pre class="mermaid">
---
title: Very simple diagram
---
---
title: Very simple diagram
---
stateDiagram
accTitle: This is the accessible title
accDescr:This is an accessible description
Expand All @@ -46,9 +46,9 @@ <h4>And these are how they are applied:</h4>
</code>
</p>
<pre class="mermaid">
---
title: Very simple diagram
---
---
title: Very simple diagram
---
stateDiagram-v2
accTitle: This is the accessible title
accDescr: This is an accessible description
Expand Down
7 changes: 7 additions & 0 deletions packages/mermaid/src/diagram-api/frontmatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ describe('extractFrontmatter', () => {
expect(db.setDiagramTitle).toHaveBeenCalledWith('foo---bar');
});

it('handles frontmatter with multi-line string and multiple delimiters', () => {
const db = dbMock();
const text = `---\ntitle: |\n multi-line string\n ---\n---\ndiagram`;
expect(extractFrontMatter(text, db)).toEqual('diagram');
expect(db.setDiagramTitle).toHaveBeenCalledWith('multi-line string\n---\n');
});

it('handles frontmatter with title', () => {
const db = dbMock();
const text = `---\ntitle: foo\n---\ndiagram`;
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagram-api/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as yaml from 'js-yaml';
// Note that JS doesn't support the "\A" anchor, which means we can't use
// multiline mode.
// Relevant YAML spec: https://yaml.org/spec/1.2.2/#914-explicit-documents
export const frontMatterRegex = /^(?:\s*---\s*[\r\n])(.*?)(?:[\r\n]\s*---\s*[\r\n]+)/s;
export const frontMatterRegex = /^(?:---\s*[\r\n])(.*?)(?:[\r\n]---\s*[\r\n]+)/s;

type FrontMatterMetadata = {
title?: string;
Expand Down
5 changes: 2 additions & 3 deletions packages/mermaid/src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,9 @@ Alice->Bob: hi`;
const type = detectType(str);
expect(type).toBe('gitGraph');
});
it('should handle frontmatter with leading spaces', function () {
it('should not allow frontmatter with leading spaces', function () {
const str = ' ---\ntitle: foo\n---\n gitGraph TB:\nbfs1:queue';
const type = detectType(str);
expect(type).toBe('gitGraph');
expect(() => detectType(str)).toThrow('No diagram type detected for text');
});
});
describe('when finding substring in array ', function () {
Expand Down

0 comments on commit a11ab3d

Please sign in to comment.