mermaid-js / mermaid

Generation of diagrams like flowcharts or sequence diagrams from text in a similar manner as markdown
https://mermaid.js.org
MIT License
72.81k stars 6.65k forks source link

standardize diagram definitions #4499

Open Yokozuna59 opened 1 year ago

Yokozuna59 commented 1 year ago

Most of these rules were discussed in this PR #4486, but it became kind of messy with commits and reviews, so we're going to summarize them here (and update them continuously):


General Diagram Definition

// diagarm = Name of diagram, e.i., pie, flowchart, etc.

packages/mermaid/src/diagrams/diagram/
├── parser/
│   └── diagram.jison
├── diagram.spec.ts
├── diagramDb.ts
├── diagramDetector.ts
├── diagramDiagram.ts
├── diagramRenderer.ts
├── diagramStyles.ts
└── diagramTypes.ts
Yokozuna59 commented 1 year ago

I've more thoughts:

Files

  1. Add diagramTheme.ts: Mermaid uses lazy loading for rendering diagrams, it doesn't make since to set and use other diagram themes when the user trying to render one diagram (unless you're planning to add multiple diagram rendering in one code). So by creating this file, it would be helpful for getting and setting values with ts code instead of mermaid, and resolve variables name duplication.

Structure

  1. Make the diagramRenderer uses it getCofnig instead of global one.
  2. Make the returned values from diagram getConfig return actual values, not possible underfed. The undefined interface is for overriding default config, not when getting it.
  3. Add setConfig and reset for diagram itself, and the parent setConfig and reset should uses it.
  4. Separate the returned the type valued value from getConfig and setConfig.

Please let me know if you approve the below and above suggestion so I can the original issue, or you could change it right away.

Yokozuna59 commented 1 year ago

And shouldn't better to move types in config.type.ts into each diagramTypes.ts?

sidharthv96 commented 1 year ago

All the suggestions look great to me.

Yokozuna59 commented 1 year ago

All the suggestions look great to me.

Great! Could you add labels to the issue? i.e, Discussion.

And do you have something in your mind on behave of separating setConfig parameter type and getConfig return type? Because it shows as optional field whereas it's for sure has default value.

It's not clean to do the the following:

const width = config?.pie?.useWidth;
sidharthv96 commented 1 year ago

Yes, getConfig should not have optionals. But we have to make sure to do it properly (without forced type assertion if possible), so that we get compile time errors if there is no default value for a config.

Yokozuna59 commented 1 year ago

Yes, getConfig should not have optionals. But we have to make sure to do it properly (without forced type assertion if possible), so that we get compile time errors if there is no default value for a config.

One possible approach is using Required type, something like this:

export const DEFAULT_PIE_CONFIG: Required<PieDiagramConfig> = {
   useMaxWidth: true,
   useWidth: 1200,
   textPosition: 0.75,
 } as const;

By doing this, it well force to set all values of the type, even optional one. Theoretically, it will always shows that it defined. And we could use RequiredDeep for object types from type-fest

What do you think? I'll update the top message with suggested stuff in here https://github.com/mermaid-js/mermaid/issues/4499#issuecomment-1595189472 with this if all of them have been approved.

Yokozuna59 commented 1 year ago

It would be great if we standardized diagram keywords, because some diagrams just have the actual keyword:

And some have additional word:


I think this addition wouldn't really help; it's just making the keyword longer.

Yokozuna59 commented 1 year ago

And maybe add renderer directory?

packages/mermaid/src/diagrams/diagram/
├── ...
├── renderer/
│   ├── note.ts
│   ├── edge.ts
│   └── ...
└── diagramRenderer.ts # export functions from renderer dir

The number of files related to rendering is kinda large in number and size, specially flowchart, so it might be better to create a folder that contains all of those stuff. So that the diagram dir would be cleaner.

What do you think?

sidharthv96 commented 1 year ago

I'm aware that the root message is kind of outdated, but I haven't been receiving any feedback on the suggested ideas. Could I assume that they have been approved with those likes? @sidharthv96 https://github.com/mermaid-js/mermaid/issues/4542#issuecomment-1609376984

Please consider the comments marked with 👍 as me agreeing personally, not as an official approval of any sorts. We might have to make edits when others review the changes once they are inside a PR. 😄

nirname commented 1 year ago

Did we mention integration specs here?

Yokozuna59 commented 1 year ago

Did we mention integration specs here?

It might be better to mention in the script that generate new diagram, because I think this is related to definition itself, not all files related to diagram.

nirname commented 1 year ago

I have noticed that the name of the file contains diagram name as well (pieRenderer.ts). It may be even better and informative, but it is also excessive a bit.

Share your thoughts on (informative, excessive)

packages/mermaid/src/diagrams/diagram/
├── parser/
│   └── diagram.jison
├── diagram.spec.ts
├── diagramDb.ts
├── diagramDetector.ts
├── diagramDiagram.ts
├── diagramRenderer.ts
├── diagramStyles.ts
└── diagramTypes.ts

versus (less informative, but very string and uniform)

packages/mermaid/src/diagrams/diagram/
├── parser.jison
├── tests.spec.ts
├── DB.ts
├── detector.ts
├── diagram.ts
├── renderer.ts
├── styles.ts
└── types.ts
Yokozuna59 commented 1 year ago

IMO, the full form (informative) form is better because we can refer to diagram files directly rather than in the short form, and here are some reasons why:   In reviewing, GitHub shows and sorts files by file name, so almost all related files (to the actual diagram) will be grouped together. Files are stored inside a 5-6 folder hierarchy, so the actual folder (diagram name) most likely won't be visible, so we won't be able to know in what diagram what file has been modified right away. If we changed the spec folder into tests, we can't filter test cases easily.

But I agree with moving jison into diagram folder.

Yokozuna59 commented 1 year ago

It might be better to add new file called diagramDefaults.ts, this file would contain the default values for DBs, styles, themes, configs ...etc. rather than adding them to diagramDb.ts.

nirname commented 1 year ago

Didn't get your idea about defaults folder. There are too many things that can be called "default", so I'll try to sort things through:

Yokozuna59 commented 1 year ago

Didn't get your idea about defaults folder.

Just a typo :), my intention was a file. A file within diagram folder.

nirname commented 1 year ago

Fully agree with that

sidharthv96 commented 1 year ago
image

I am aligning closer to the naming convention suggested by @nirname. Our editors are smart enough to detect pie/db if we type piedb to open a file. GitHub shows the full path in reviews. So I don't really see why we should keep the diagram/diagramDB.ts convention instead of the cleaner diagram/DB.ts.

Yokozuna59 commented 1 year ago

Then should we rename files in #4727 for parser package?

sidharthv96 commented 1 year ago

I don't think that's necessary right now. We can discuss and decide later and do a single PR to implement the decision.