Skip to content

Commit

Permalink
Merge pull request #3436 from mermaid-js/3061_decoupling
Browse files Browse the repository at this point in the history
Creating detectors and moving out diagram specific code from the diag…
  • Loading branch information
knsv authored Sep 12, 2022
2 parents 885e69c + 06365fa commit 0edc275
Show file tree
Hide file tree
Showing 27 changed files with 575 additions and 435 deletions.
24 changes: 12 additions & 12 deletions cypress/platform/knsv.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</style>
</head>
<body>
<pre class="mermaid2" style="width: 50%">
<pre class="mermaid" style="width: 50%">
flowchart LR
classDef aPID stroke:#4e4403,fill:#fdde29,color:#4e4403,rx:5px,ry:5px;
classDef crm stroke:#333333,fill:#DCDCDC,color:#333333,rx:5px,ry:5px;
Expand Down Expand Up @@ -99,7 +99,7 @@
class A someclass;
class C someclass;
</pre>
<pre class="mermaid2" style="width: 50%">
<pre class="mermaid" style="width: 50%">
sequenceDiagram
title: My Sequence Diagram Title
accTitle: My Acc Sequence Diagram
Expand All @@ -109,14 +109,14 @@
John-->>Alice: Great!
Alice-)John: See you later!
</pre>
<pre class="mermaid2" style="width: 50%">
<pre class="mermaid" style="width: 50%">
graph TD
A -->|000| B
B -->|111| C

linkStyle 1 stroke:#ff3,stroke-width:4px,color:red;
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
journey
accTitle: My User Journey Diagram
accDescr: My User Journey Diagram Description
Expand All @@ -130,10 +130,10 @@
Go downstairs: 5: Me
Sit down: 5: Me
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
info
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
requirementDiagram
accTitle: My req Diagram
accDescr: My req Diagram Description
Expand Down Expand Up @@ -174,7 +174,7 @@
test_req - contains -> test_req3
test_req <- copies - test_entity2
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
gantt
dateFormat YYYY-MM-DD
title Adding GANTT diagram functionality to mermaid
Expand Down Expand Up @@ -206,7 +206,7 @@
Add gantt diagram to demo page :20h
Add another diagram to demo page :48h
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
stateDiagram
state Active {
Idle
Expand Down Expand Up @@ -234,7 +234,7 @@
end
B ->> A: Return
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
classDiagram
accTitle: My class diagram
accDescr: My class diagram Description
Expand All @@ -259,7 +259,7 @@
A->>Bob: Hola
Bob-->A: Pasten !
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
gitGraph
commit id: "ZERO"
branch develop
Expand Down Expand Up @@ -288,7 +288,7 @@
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
classDiagram
Animal "1" <|-- Duck
Animal <|-- Fish
Expand All @@ -311,7 +311,7 @@
+run()
}
</pre>
<pre class="mermaid2" style="width: 100%">
<pre class="mermaid" style="width: 100%">
erDiagram
CAR ||--o{ NAMED-DRIVER : allows
CAR {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mermaid",
"version": "9.1.6",
"version": "9.2.0-rc1",
"description": "Markdownish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
"main": "dist/mermaid.min.js",
"module": "dist/mermaid.esm.min.mjs",
Expand Down
37 changes: 2 additions & 35 deletions src/diagram-api/detectType.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { MermaidConfig } from '../config.type';

export type DiagramDetector = (text: string) => boolean;
export type DiagramDetector = (text: string, config?: MermaidConfig) => boolean;

const directive =
/[%]{2}[{]\s*(?:(?:(\w+)\s*:|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
const anyComment = /\s*%%.*\n/gm;

const detectors: Record<string, DiagramDetector> = {};
const diagramMatchers: Record<string, RegExp> = {
c4: /^\s*C4Context|C4Container|C4Component|C4Dynamic|C4Deployment/,
sequence: /^\s*sequenceDiagram/,
gantt: /^\s*gantt/,
classDiagram: /^\s*classDiagram-v2/,
stateDiagram: /^\s*stateDiagram-v2/,
'flowchart-v2': /^\s*flowchart/, // Might need to add |graph to fix #3391
info: /^\s*info/,
pie: /^\s*pie/,
er: /^\s*erDiagram/,
journey: /^\s*journey/,
// gitGraph: /^\s*gitGraph/,
requirement: /^\s*requirement(Diagram)?/,
};

/**
* @function detectType Detects the type of the graph text. Takes into consideration the possible
Expand All @@ -47,28 +33,9 @@ const diagramMatchers: Record<string, RegExp> = {
*/
export const detectType = function (text: string, config?: MermaidConfig): string {
text = text.replace(directive, '').replace(anyComment, '\n');
for (const [diagram, matcher] of Object.entries(diagramMatchers)) {
if (text.match(matcher)) {
return diagram;
}
}

if (text.match(/^\s*classDiagram/)) {
if (config?.class?.defaultRenderer === 'dagre-wrapper') return 'classDiagram';
return 'class';
}

if (text.match(/^\s*stateDiagram/)) {
if (config?.state?.defaultRenderer === 'dagre-wrapper') return 'stateDiagram';
return 'state';
}

if (config?.flowchart?.defaultRenderer === 'dagre-wrapper') {
return 'flowchart-v2';
}

for (const [key, detector] of Object.entries(detectors)) {
if (detector(text)) {
if (detector(text, config)) {
return key;
}
}
Expand Down
Loading

0 comments on commit 0edc275

Please sign in to comment.