Skip to content

Commit

Permalink
Merge pull request #3859 from aloisklink/fix/3706_support-indented-ya…
Browse files Browse the repository at this point in the history
…ml-only-in-html

Support parsing indented mermaid/YAML only from HTML
  • Loading branch information
sidharthv96 authored Dec 23, 2022
2 parents 03c5bc1 + a58b41a commit c23cd49
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 68 deletions.
8 changes: 4 additions & 4 deletions demos/classchart.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<h1>Class diagram demos</h1>

<pre class="mermaid">
---
title: Demo Class Diagram
---
classDiagram
---
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
61 changes: 30 additions & 31 deletions demos/er.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,42 @@

<body>
<pre class="mermaid">
---
title: This is a title
---
erDiagram
%% title This is a title
%% accDescription Test a description

---
title: This is a title
---
erDiagram
%% title This is a title
%% accDescription Test a description
"Person . CUSTOMER"||--o{ ORDER : places

"Person . CUSTOMER"||--o{ ORDER : places
ORDER ||--|{ "€£LINE_ITEM ¥" : contains

ORDER ||--|{ "€£LINE_ITEM ¥" : contains
"Person . CUSTOMER" }|..|{ "Address//StreetAddress::[DELIVERY ADDRESS]" : uses

"Person . CUSTOMER" }|..|{ "Address//StreetAddress::[DELIVERY ADDRESS]" : uses
"Address//StreetAddress::[DELIVERY ADDRESS]" {
int customerID FK
string line1 "this is the first address line comment"
string line2
string city
string region
string state
string(5) postal_code
string country
}

"Address//StreetAddress::[DELIVERY ADDRESS]" {
int customerID FK
string line1 "this is the first address line comment"
string line2
string city
string region
string state
string(5) postal_code
string country
}

"a_~`!@#$^&*()-_=+[]{}|/;:'.?¡⁄™€£‹¢›∞fi§‡•°ª·º‚≠±œŒ∑„®†ˇ¥Á¨ˆˆØπ∏“«»åÅßÍ∂΃ϩ˙Ó∆Ô˚¬Ò…ÚæÆΩ¸≈π˛çÇ√◊∫ı˜µÂ≤¯≥˘÷¿" {
string name "this is an entity with an absurd name just to show characters that are now acceptable as long as the name is in double quotes"
}
"a_~`!@#$^&*()-_=+[]{}|/;:'.?¡⁄™€£‹¢›∞fi§‡•°ª·º‚≠±œŒ∑„®†ˇ¥Á¨ˆˆØπ∏“«»åÅßÍ∂΃ϩ˙Ó∆Ô˚¬Ò…ÚæÆΩ¸≈π˛çÇ√◊∫ı˜µÂ≤¯≥˘÷¿" {
string name "this is an entity with an absurd name just to show characters that are now acceptable as long as the name is in double quotes"
}

"€£LINE_ITEM ¥" {
int orderID FK
int currencyId FK
number price
number quantity
number adjustment
number final_price
}
"€£LINE_ITEM ¥" {
int orderID FK
int currencyId FK
number price
number quantity
number adjustment
number final_price
}
</pre>
<hr />

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
20 changes: 10 additions & 10 deletions demos/state.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<h1>State diagram demos</h1>
<h2>Very simple showing change from State1 to State2</h2>
<pre class="mermaid">
---
title: Very simple diagram
---
stateDiagram
accTitle: This is the accessible title
---
title: Very simple diagram
---
stateDiagram
accTitle: This is the accessible title
accDescr:This is an accessible description
State1 --> State2
</pre>
Expand All @@ -47,13 +47,13 @@ <h4>And these are how they are applied:</h4>
</code>
</p>
<pre class="mermaid">
---
title: Very simple diagram
---
stateDiagram
---
title: Very simple diagram
---
stateDiagram
direction TB

accTitle: This is the accessible title
accTitle: This is the accessible title
accDescr: This is an accessible description

classDef notMoving fill:white
Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"moment-mini": "^2.24.0",
"non-layered-tidy-tree-layout": "^2.0.2",
"stylis": "^4.1.2",
"ts-dedent": "^2.2.0",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions packages/mermaid/src/diagram-api/diagram-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions packages/mermaid/src/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid
* functionality and to render the diagrams to svg code!
*/
import dedent from 'ts-dedent';

import { MermaidConfig } from './config.type';
import { log } from './logger';
import utils from './utils';
Expand Down Expand Up @@ -148,8 +150,7 @@ const initThrowsErrors = function (
txt = element.innerHTML;

// transforms the html to pure text
txt = utils
.entityDecode(txt)
txt = dedent(utils.entityDecode(txt)) // removes indentation, required for YAML parsing
.trim()
.replace(/<br\s*\/?>/gi, '<br/>');

Expand Down Expand Up @@ -290,8 +291,7 @@ const initThrowsErrorsAsync = async function (
txt = element.innerHTML;

// transforms the html to pure text
txt = utils
.entityDecode(txt)
txt = dedent(utils.entityDecode(txt)) // removes indentation, required for YAML parsing
.trim()
.replace(/<br\s*\/?>/gi, '<br/>');

Expand Down
13 changes: 13 additions & 0 deletions packages/mermaid/src/mermaidAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,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();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,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 () {
Expand Down
16 changes: 11 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c23cd49

Please sign in to comment.