Skip to content

Commit

Permalink
Add title support using YAML frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonM committed Nov 19, 2022
1 parent 09ed41b commit 70f0247
Show file tree
Hide file tree
Showing 48 changed files with 477 additions and 5 deletions.
12 changes: 12 additions & 0 deletions __mocks__/d3.ts
Expand Up @@ -53,6 +53,18 @@ export const MockD3 = (name, parent) => {
get __parent() {
return parent;
},
node() {
return {
getBBox() {
return {
x: 5,
y: 10,
height: 15,
width: 20,
};
},
};
},
};
elem.append = (name) => {
const mockElem = MockD3(name, elem);
Expand Down
12 changes: 12 additions & 0 deletions cypress/integration/rendering/classDiagram-v2.spec.js
Expand Up @@ -496,4 +496,16 @@ describe('Class diagram V2', () => {
);
cy.get('svg');
});

it('1433: should render a simple class with a title', () => {
imgSnapshotTest(
`---
title: simple class diagram
---
classDiagram-v2
class Class10
`,
{}
);
});
});
13 changes: 13 additions & 0 deletions cypress/integration/rendering/erDiagram.spec.js
Expand Up @@ -273,4 +273,17 @@ describe('Entity Relationship Diagram', () => {
);
cy.get('svg');
});

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
`,
{}
);
});
});
11 changes: 11 additions & 0 deletions cypress/integration/rendering/flowchart-v2.spec.js
Expand Up @@ -663,4 +663,15 @@ flowchart RL
{ htmlLabels: true, flowchart: { htmlLabels: true }, securityLevel: 'loose' }
);
});
it('1433: should render a titled flowchart with titleTopMargin set to 0', () => {
imgSnapshotTest(
`---
title: Simple flowchart
---
flowchart TD
A --> B
`,
{ titleTopMargin: 0 }
);
});
});
11 changes: 11 additions & 0 deletions cypress/integration/rendering/gitGraph.spec.js
Expand Up @@ -322,4 +322,15 @@ describe('Git Graph diagram', () => {
{}
);
});
it('1433: should render a simple gitgraph with a title', () => {
imgSnapshotTest(
`---
title: simple gitGraph
---
gitGraph
commit
`,
{}
);
});
});
12 changes: 12 additions & 0 deletions cypress/integration/rendering/stateDiagram-v2.spec.js
Expand Up @@ -559,4 +559,16 @@ stateDiagram-v2
);
});
});
it('1433: should render a simple state diagram with a title', () => {
imgSnapshotTest(
`---
title: simple state diagram
---
stateDiagram-v2
[*] --> State1
State1 --> [*]
`,
{}
);
});
});
3 changes: 3 additions & 0 deletions demos/classchart.html
Expand Up @@ -17,6 +17,9 @@
<h1>Class diagram demos</h1>

<pre class="mermaid">
---
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
3 changes: 3 additions & 0 deletions demos/er.html
Expand Up @@ -20,6 +20,9 @@
<body>
<pre class="mermaid">

---
title: This is a title
---
erDiagram
%% title This is a title
%% accDescription Test a description
Expand Down
6 changes: 6 additions & 0 deletions demos/flowchart.html
Expand Up @@ -17,6 +17,9 @@ <h1>Comparison "graph vs. flowchart"</h1>
<h2>Sample 1</h2>
<h3>graph</h3>
<pre class="mermaid">
---
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 @@ -221,6 +224,9 @@ <h3>flowchart</h3>
<h2>Sample 2</h2>
<h3>graph</h3>
<pre class="mermaid">
---
title: What to buy
---
graph TD
accTitle: What to buy
accDescr: Options of what to buy with Christmas money
Expand Down
3 changes: 3 additions & 0 deletions demos/git.html
Expand Up @@ -16,6 +16,9 @@
<body>
<h1>Git diagram demo</h1>
<pre class="mermaid">
---
title: Simple Git diagram
---
gitGraph:
options
{
Expand Down
6 changes: 4 additions & 2 deletions demos/journey.html
Expand Up @@ -16,8 +16,10 @@
<body>
<h1>Journey diagram demo</h1>
<pre class="mermaid">
journey
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
6 changes: 6 additions & 0 deletions demos/state.html
Expand Up @@ -17,6 +17,9 @@
<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
accDescr:This is an accessible description
Expand All @@ -43,6 +46,9 @@ <h4>And these are how they are applied:</h4>
</code>
</p>
<pre class="mermaid">
---
title: Very simple diagram
---
stateDiagram-v2
accTitle: This is the accessible title
accDescr: This is an accessible description
Expand Down
2 changes: 1 addition & 1 deletion docs/config/setup/modules/defaultConfig.md
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[defaultConfig.ts:1881](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L1881)
[defaultConfig.ts:1933](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L1933)

---

Expand Down
12 changes: 12 additions & 0 deletions docs/syntax/classDiagram.md
Expand Up @@ -14,6 +14,9 @@ The class diagram is the main building block of object-oriented modeling. It is
Mermaid can render class diagrams.

```mermaid-example
---
title: Animal example
---
classDiagram
note "From Duck till Zebra"
Animal <|-- Duck
Expand All @@ -40,6 +43,9 @@ classDiagram
```

```mermaid
---
title: Animal example
---
classDiagram
note "From Duck till Zebra"
Animal <|-- Duck
Expand Down Expand Up @@ -77,6 +83,9 @@ A single instance of a class in the diagram contains three compartments:
- The bottom compartment contains the operations the class can execute. They are also left-aligned and the first letter is lowercase.

```mermaid-example
---
title: Bank example
---
classDiagram
class BankAccount
BankAccount : +String owner
Expand All @@ -87,6 +96,9 @@ classDiagram
```

```mermaid
---
title: Bank example
---
classDiagram
class BankAccount
BankAccount : +String owner
Expand Down
6 changes: 6 additions & 0 deletions docs/syntax/entityRelationshipDiagram.md
Expand Up @@ -13,13 +13,19 @@ Note that practitioners of ER modelling almost always refer to _entity types_ si
Mermaid can render ER diagrams

```mermaid-example
---
title: Order example
---
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
```

```mermaid
---
title: Order example
---
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
Expand Down
12 changes: 12 additions & 0 deletions docs/syntax/flowchart.md
Expand Up @@ -15,11 +15,17 @@ It can also accommodate different arrow types, multi directional arrows, and lin
### A node (default)

```mermaid-example
---
title: Node
---
flowchart LR
id
```

```mermaid
---
title: Node
---
flowchart LR
id
```
Expand All @@ -33,11 +39,17 @@ found for the node that will be used. Also if you define edges for the node late
one previously defined will be used when rendering the box.

```mermaid-example
---
title: Node with text
---
flowchart LR
id1[This is the text in the box]
```

```mermaid
---
title: Node with text
---
flowchart LR
id1[This is the text in the box]
```
Expand Down
6 changes: 6 additions & 0 deletions docs/syntax/gitgraph.md
Expand Up @@ -13,6 +13,9 @@ These kind of diagram are particularly helpful to developers and devops teams to
Mermaid can render Git diagrams

```mermaid-example
---
title: Example Git diagram
---
gitGraph
commit
commit
Expand All @@ -27,6 +30,9 @@ Mermaid can render Git diagrams
```

```mermaid
---
title: Example Git diagram
---
gitGraph
commit
commit
Expand Down
6 changes: 6 additions & 0 deletions docs/syntax/stateDiagram.md
Expand Up @@ -11,6 +11,9 @@
Mermaid can render state diagrams. The syntax tries to be compliant with the syntax used in plantUml as this will make it easier for users to share diagrams between mermaid and plantUml.

```mermaid-example
---
title: Simple sample
---
stateDiagram-v2
[*] --> Still
Still --> [*]
Expand All @@ -22,6 +25,9 @@ stateDiagram-v2
```

```mermaid
---
title: Simple sample
---
stateDiagram-v2
[*] --> Still
Still --> [*]
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -60,6 +60,7 @@
"@types/eslint": "^8.4.10",
"@types/express": "^4.17.14",
"@types/jsdom": "^20.0.1",
"@types/js-yaml": "^4.0.5",
"@types/lodash": "^4.14.188",
"@types/mdast": "^3.0.10",
"@types/node": "^18.11.9",
Expand Down Expand Up @@ -90,6 +91,7 @@
"jest": "^29.3.1",
"jison": "^0.4.18",
"jsdom": "^20.0.2",
"js-yaml": "^4.1.0",
"lint-staged": "^13.0.3",
"path-browserify": "^1.0.1",
"pnpm": "^7.15.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/mermaid/src/Diagram.ts
Expand Up @@ -2,6 +2,7 @@ import * as configApi from './config';
import { log } from './logger';
import { getDiagram, registerDiagram } from './diagram-api/diagramAPI';
import { detectType, getDiagramLoader } from './diagram-api/detectType';
import { extractFrontMatter } from './diagram-api/frontmatter';
import { isDetailedError, type DetailedError } from './utils';

export type ParseErrorFunction = (err: string | DetailedError, hash?: any) => void;
Expand Down Expand Up @@ -29,6 +30,16 @@ export class Diagram {
this.db.clear?.();
this.renderer = diagram.renderer;
this.parser = diagram.parser;
const originalParse = this.parser.parse.bind(this.parser);
// Wrap the jison parse() method to handle extracting frontmatter.
//
// This can't be done in this.parse() because some code
// directly calls diagram.parser.parse(), bypassing this.parse().
//
// Similarly, we can't do this in getDiagramFromText() because some code
// calls diagram.db.clear(), which would reset anything set by
// extractFrontMatter().
this.parser.parse = (text: string) => originalParse(extractFrontMatter(text, this.db));
this.parser.parser.yy = this.db;
if (diagram.init) {
diagram.init(cnf);
Expand Down

0 comments on commit 70f0247

Please sign in to comment.