collab/mermaid/docs/community/new-diagram-jison.mdblame
View source
6dd74de1> **Warning**
6dd74de2>
6dd74de3> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
6dd74de4>
6dd74de5> ## Please edit the corresponding file in [/packages/mermaid/src/docs/community/new-diagram-jison.md](../../packages/mermaid/src/docs/community/new-diagram-jison.md).
6dd74de6
6dd74de7# Adding a New Diagram/Chart (Deprecated) 📊
6dd74de8
6dd74de9> **Warning**
6dd74de10> JISON grammars are deprecated in mermaid. Please use Langium instead. See [New Diagram](./new-diagram.md) for more information.
6dd74de11>
6dd74de12> **New diagrams with JISON grammars will not be accepted.**
6dd74de13
6dd74de14### Step 1: Grammar & Parsing
6dd74de15
6dd74de16#### Grammar
6dd74de17
6dd74de18This would be to define a JISON grammar for the new diagram type. That should start with a way to identify that the text in the mermaid tag is a diagram of that type. Create a new folder under diagrams for your new diagram type and a parser folder in it. This leads us to step 2.
6dd74de19
6dd74de20For instance:
6dd74de21
6dd74de22- the flowchart starts with the keyword _graph_
6dd74de23- the sequence diagram starts with the keyword _sequenceDiagram_
6dd74de24
6dd74de25#### Store data found during parsing
6dd74de26
6dd74de27There are some jison specific sub steps here where the parser stores the data encountered when parsing the diagram, this data is later used by the renderer. You can during the parsing call an object provided to the parser by the user of the parser. This object can be called during parsing for storing data.
6dd74de28
6dd74de29```jison
6dd74de30statement
6dd74de31 : 'participant' actor { $$='actor'; }
6dd74de32 | signal { $$='signal'; }
6dd74de33 | note_statement { $$='note'; }
6dd74de34 | 'title' message { yy.setTitle($2); }
6dd74de35 ;
6dd74de36```
6dd74de37
6dd74de38In the extract of the grammar above, it is defined that a call to the setTitle method in the data object will be done when parsing and the title keyword is encountered.
6dd74de39
6dd74de40> **Note**
6dd74de41> Make sure that the `parseError` function for the parser is defined and calling `mermaid.parseError`. This way a common way of detecting parse errors is provided for the end-user.
6dd74de42
6dd74de43For more info look at the example diagram type:
6dd74de44
6dd74de45The `yy` object has the following function:
6dd74de46
6dd74de47```javascript
6dd74de48exports.parseError = function (err, hash) {
6dd74de49 mermaid.parseError(err, hash);
6dd74de50};
6dd74de51```
6dd74de52
6dd74de53when parsing the `yy` object is initialized as per below:
6dd74de54
6dd74de55```javascript
6dd74de56const parser = exampleParser.parser;
6dd74de57parser.yy = db;
6dd74de58```
6dd74de59
6dd74de60### Step 2: Rendering
6dd74de61
6dd74de62Write a renderer that given the data found during parsing renders the diagram. To look at an example look at sequenceRenderer.js rather than the flowchart renderer as this is a more generic example.
6dd74de63
6dd74de64Place the renderer in the diagram folder.
6dd74de65
6dd74de66### Step 3: Detection of the new diagram type
6dd74de67
6dd74de68The second thing to do is to add the capability to detect the new diagram to type to the detectType in `diagram-api/detectType.ts`. The detection should return a key for the new diagram type.
6dd74de69[This key will be used to as the aria roledescription](#aria-roledescription), so it should be a word that clearly describes the diagram type.
6dd74de70For example, if your new diagram uses a UML deployment diagram, a good key would be "UMLDeploymentDiagram" because assistive technologies such as a screen reader
6dd74de71would voice that as "U-M-L Deployment diagram." Another good key would be "deploymentDiagram" because that would be voiced as "Deployment Diagram." A bad key would be "deployment" because that would not sufficiently describe the diagram.
6dd74de72
6dd74de73Note that the diagram type key does not have to be the same as the diagram keyword chosen for the [grammar](#grammar), but it is helpful if they are the same.
6dd74de74
6dd74de75### Step 4: The final piece - triggering the rendering
6dd74de76
6dd74de77At this point when mermaid is trying to render the diagram, it will detect it as being of the new type but there will be no match when trying to render the diagram. To fix this add a new case in the switch statement in main.js:init this should match the diagram type returned from step #2. The code in this new case statement should call the renderer for the diagram type with the data found by the parser as an argument.
6dd74de78
6dd74de79## Usage of the parser as a separate module
6dd74de80
6dd74de81### Setup
6dd74de82
6dd74de83```javascript
6dd74de84const graph = require('./graphDb');
6dd74de85const flow = require('./parser/flow');
6dd74de86flow.parser.yy = graph;
6dd74de87```
6dd74de88
6dd74de89### Parsing
6dd74de90
6dd74de91```javascript
6dd74de92flow.parser.parse(text);
6dd74de93```
6dd74de94
6dd74de95### Data extraction
6dd74de96
6dd74de97```javascript
6dd74de98graph.getDirection();
6dd74de99graph.getVertices();
6dd74de100graph.getEdges();
6dd74de101```
6dd74de102
6dd74de103The parser is also exposed in the mermaid api by calling:
6dd74de104
6dd74de105```javascript
6dd74de106const parser = mermaid.getParser();
6dd74de107```
6dd74de108
6dd74de109Note that the parse needs a graph object to store the data as per:
6dd74de110
6dd74de111```javascript
6dd74de112flow.parser.yy = graph;
6dd74de113```
6dd74de114
6dd74de115Look at `graphDb.js` for more details on that object.
6dd74de116
6dd74de117## Layout
6dd74de118
6dd74de119If you are using a dagre based layout, please use flowchart-v2 as a template and by doing that you will be using dagre-wrapper instead of dagreD3 which we are migrating away from.
6dd74de120
6dd74de121### Common parts of a diagram
6dd74de122
6dd74de123There are a few features that are common between the different types of diagrams. We try to standardize the diagrams that work as similar as possible for the end user. The commonalities are:
6dd74de124
6dd74de125- Directives, a way of modifying the diagram configuration from within the diagram code.
6dd74de126- Accessibility, a way for an author to provide additional information like titles and descriptions to people accessing a text with diagrams using a screen reader.
6dd74de127- Themes, there is a common way to modify the styling of diagrams in Mermaid.
6dd74de128- Comments should follow mermaid standards
6dd74de129
6dd74de130Here are some pointers on how to handle these different areas.
6dd74de131
6dd74de132## Accessibility
6dd74de133
6dd74de134Mermaid automatically adds the following accessibility information for the diagram SVG HTML element:
6dd74de135
6dd74de136- aria-roledescription
6dd74de137- accessible title
6dd74de138- accessible description
6dd74de139
6dd74de140### aria-roledescription
6dd74de141
6dd74de142The aria-roledescription is automatically set to [the diagram type](#step-3--detection-of-the-new-diagram-type) and inserted into the SVG element.
6dd74de143
6dd74de144See [the definition of aria-roledescription](https://www.w3.org/TR/wai-aria-1.1/#aria-roledescription) in [the Accessible Rich Internet Applications W3 standard.](https://www.w3.org/WAI/standards-guidelines/aria/)
6dd74de145
6dd74de146### accessible title and description
6dd74de147
6dd74de148The syntax for accessible titles and descriptions is described in [the Accessibility documentation section.](../config/accessibility.md)
6dd74de149
6dd74de150As a design goal, the jison syntax should be similar between the diagrams.
6dd74de151
6dd74de152```jison
6dd74de153
6dd74de154* lexical grammar */
6dd74de155%lex
6dd74de156%x acc_title
6dd74de157%x acc_descr
6dd74de158%x acc_descr_multiline
6dd74de159
6dd74de160%%
6dd74de161accTitle\s*":"\s* { this.begin("acc_title");return 'acc_title'; }
6dd74de162<acc_title>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_title_value"; }
6dd74de163accDescr\s*":"\s* { this.begin("acc_descr");return 'acc_descr'; }
6dd74de164<acc_descr>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_descr_value"; }
6dd74de165accDescr\s*"{"\s* { this.begin("acc_descr_multiline");}
6dd74de166<acc_descr_multiline>[\}] { this.popState(); }
6dd74de167<acc_descr_multiline>[^\}]* return "acc_descr_multiline_value";
6dd74de168
6dd74de169statement
6dd74de170 : acc_title acc_title_value { $$=$2.trim();yy.setTitle($$); }
6dd74de171 | acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }
6dd74de172 | acc_descr_multiline_value { $$=$1.trim();yy.setAccDescription($$); }
6dd74de173
6dd74de174```
6dd74de175
6dd74de176The functions for setting title and description are provided by a common module. This is the import from flowDb.js:
6dd74de177
6dd74de178```
6dd74de179import {
6dd74de180 setAccTitle,
6dd74de181 getAccTitle,
6dd74de182 getAccDescription,
6dd74de183 setAccDescription,
6dd74de184 clear as commonClear,
6dd74de185} from '../../commonDb';
6dd74de186```
6dd74de187
6dd74de188The accessibility title and description are inserted into the SVG element in the `render` function in mermaidAPI.
6dd74de189
6dd74de190## Theming
6dd74de191
6dd74de192Mermaid supports themes and has an integrated theming engine. You can read more about how the themes can be used [in the docs](../config/theming.md).
6dd74de193
6dd74de194When adding themes to a diagram it comes down to a few important locations in the code.
6dd74de195
6dd74de196The entry point for the styling engine is in **src/styles.js**. The getStyles function will be called by Mermaid when the styles are being applied to the diagram.
6dd74de197
6dd74de198This function will in turn call a function _your diagram should provide_ returning the css for the new diagram. The diagram specific, also which is commonly also called getStyles and located in the folder for your diagram under src/diagrams and should be named styles.js. The getStyles function will be called with the theme options as an argument like in the following example:
6dd74de199
6dd74de200```js
6dd74de201const getStyles = (options) =>
6dd74de202 `
6dd74de203 .line {
6dd74de204 stroke-width: 1;
6dd74de205 stroke: ${options.lineColor};
6dd74de206 stroke-dasharray: 2;
6dd74de207 }
6dd74de208 // ...
6dd74de209 `;
6dd74de210```
6dd74de211
6dd74de212Note that you need to provide your function to the main getStyles by adding it into the themes object in **src/styles.js** like in the xyzDiagram in the provided example:
6dd74de213
6dd74de214```js
6dd74de215const themes = {
6dd74de216 flowchart,
6dd74de217 'flowchart-v2': flowchart,
6dd74de218 sequence,
6dd74de219 xyzDiagram,
6dd74de220 //...
6dd74de221};
6dd74de222```
6dd74de223
6dd74de224The actual options and values for the colors are defined in **src/theme/theme-\[xyz].js**. If you provide the options your diagram needs in the existing theme files then the theming will work smoothly without hiccups.