@@ -244,9 +244,9 @@ public void AddAdditionalData(Dictionary<string, List<string>> additionalData)
244
244
public void WriteMermaid ( StreamWriter writer )
245
245
{
246
246
writer . WriteLine ( "graph LR" ) ;
247
- foreach ( var color in MermaidColorScheme )
247
+ foreach ( var style in MermaidNodeStyles )
248
248
{
249
- writer . WriteLine ( $ "classDef { color . Key } fill:{ color . Value } ,stroke:#333,stroke-width:4px ") ;
249
+ writer . WriteLine ( $ "classDef { style . Key } fill:{ style . Value . Color } ,stroke:#333,stroke-width:2px ") ;
250
250
}
251
251
252
252
ProcessNode ( this , writer ) ;
@@ -255,35 +255,71 @@ public void WriteMermaid(StreamWriter writer)
255
255
/// <summary>
256
256
/// Dictionary that maps a set of HTTP methods to HTML color. Keys are sorted, uppercased, concatenated HTTP methods.
257
257
/// </summary>
258
- public static Dictionary < string , string > MermaidColorScheme = new Dictionary < string , string >
258
+ public static Dictionary < string , MermaidNodeStyle > MermaidNodeStyles = new Dictionary < string , MermaidNodeStyle >
259
259
{
260
- { "GET" , "lightSteelBlue" } ,
261
- { "POST" , "SteelBlue" } ,
262
- { "GET_POST" , "forestGreen" } ,
263
- { "DELETE_GET_PATCH" , "yellowGreen" } ,
264
- { "DELETE_GET_PUT" , "olive" } ,
265
- { "DELETE_GET" , "DarkSeaGreen" } ,
266
- { "DELETE" , "tomato" } ,
267
- { "OTHER" , "white" }
260
+ { "GET" , new MermaidNodeStyle ( "lightSteelBlue" , MermaidNodeShape . SquareCornerRectangle ) } ,
261
+ { "POST" , new MermaidNodeStyle ( "Lightcoral" , MermaidNodeShape . OddShape ) } ,
262
+ { "GET_POST" , new MermaidNodeStyle ( "forestGreen" , MermaidNodeShape . RoundedCornerRectangle ) } ,
263
+ { "DELETE_GET_PATCH" , new MermaidNodeStyle ( "yellowGreen" , MermaidNodeShape . Circle ) } ,
264
+ { "DELETE_GET_PATCH_PUT" , new MermaidNodeStyle ( "oliveDrab" , MermaidNodeShape . Circle ) } ,
265
+ { "DELETE_GET_PUT" , new MermaidNodeStyle ( "olive" , MermaidNodeShape . Circle ) } ,
266
+ { "DELETE_GET" , new MermaidNodeStyle ( "DarkSeaGreen" , MermaidNodeShape . Circle ) } ,
267
+ { "DELETE" , new MermaidNodeStyle ( "Tomato" , MermaidNodeShape . Rhombus ) } ,
268
+ { "OTHER" , new MermaidNodeStyle ( "White" , MermaidNodeShape . SquareCornerRectangle ) } ,
268
269
} ;
269
270
270
271
private static void ProcessNode ( OpenApiUrlTreeNode node , StreamWriter writer )
271
272
{
272
273
var path = string . IsNullOrEmpty ( node . Path ) ? "/" : SanitizeMermaidNode ( node . Path ) ;
274
+ var methods = GetMethods ( node ) ;
275
+ var ( startChar , endChar ) = GetShapeDelimiters ( methods ) ;
273
276
foreach ( var child in node . Children )
274
277
{
275
- writer . WriteLine ( $ "{ path } --> { SanitizeMermaidNode ( child . Value . Path ) } [\" { child . Key } \" ]") ;
278
+ var childMethods = GetMethods ( child . Value ) ;
279
+ var ( childStartChar , childEndChar ) = GetShapeDelimiters ( childMethods ) ;
280
+ writer . WriteLine ( $ "{ path } { startChar } \" { node . Segment } \" { endChar } --> { SanitizeMermaidNode ( child . Value . Path ) } { childStartChar } \" { child . Key } \" { childEndChar } ") ;
276
281
ProcessNode ( child . Value , writer ) ;
277
282
}
278
- var methods = String . Join ( "_" , node . PathItems . SelectMany ( p => p . Value . Operations . Select ( o => o . Key ) )
283
+ if ( String . IsNullOrEmpty ( methods ) ) methods = "OTHER" ;
284
+ writer . WriteLine ( $ "class { path } { methods } ") ;
285
+ }
286
+
287
+ private static string GetMethods ( OpenApiUrlTreeNode node )
288
+ {
289
+ return String . Join ( "_" , node . PathItems . SelectMany ( p => p . Value . Operations . Select ( o => o . Key ) )
279
290
. Distinct ( )
280
291
. Select ( o => o . ToString ( ) . ToUpper ( ) )
281
292
. OrderBy ( o => o )
282
293
. ToList ( ) ) ;
283
- if ( String . IsNullOrEmpty ( methods ) ) methods = "OTHER" ;
284
- writer . WriteLine ( $ "class { path } { methods } ") ;
285
294
}
286
295
296
+ private static ( string , string ) GetShapeDelimiters ( string methods )
297
+ {
298
+
299
+ if ( MermaidNodeStyles . ContainsKey ( methods ) )
300
+ {
301
+ //switch on shape
302
+ switch ( MermaidNodeStyles [ methods ] . Shape )
303
+ {
304
+ case MermaidNodeShape . Circle :
305
+ return ( "((" , "))" ) ;
306
+ case MermaidNodeShape . RoundedCornerRectangle :
307
+ return ( "(" , ")" ) ;
308
+ case MermaidNodeShape . Rhombus :
309
+ return ( "{" , "}" ) ;
310
+ case MermaidNodeShape . SquareCornerRectangle :
311
+ return ( "[" , "]" ) ;
312
+ case MermaidNodeShape . OddShape :
313
+ return ( ">" , "]" ) ;
314
+ default :
315
+ return ( "[" , "]" ) ;
316
+ }
317
+ }
318
+ else
319
+ {
320
+ return ( "[" , "]" ) ;
321
+ }
322
+ }
287
323
private static string SanitizeMermaidNode ( string token )
288
324
{
289
325
return token . Replace ( "\\ " , "/" )
@@ -295,4 +331,57 @@ private static string SanitizeMermaidNode(string token)
295
331
. Replace ( "default" , "def_ault" ) ; // default is a reserved word for classes
296
332
}
297
333
}
334
+ /// <summary>
335
+ /// Defines the color and shape of a node in a Mermaid graph diagram
336
+ /// </summary>
337
+ public class MermaidNodeStyle
338
+ {
339
+ /// <summary>
340
+ ///
341
+ /// </summary>
342
+ /// <param name="color"></param>
343
+ /// <param name="shape"></param>
344
+ public MermaidNodeStyle ( string color , MermaidNodeShape shape )
345
+ {
346
+ Color = color ;
347
+ Shape = shape ;
348
+ }
349
+
350
+ /// <summary>
351
+ ///
352
+ /// </summary>
353
+ public string Color { get ; }
354
+
355
+ /// <summary>
356
+ ///
357
+ /// </summary>
358
+ public MermaidNodeShape Shape { get ; }
359
+ }
360
+
361
+ /// <summary>
362
+ ///
363
+ /// </summary>
364
+ public enum MermaidNodeShape
365
+ {
366
+ /// <summary>
367
+ /// Rectangle with square corners
368
+ /// </summary>
369
+ SquareCornerRectangle ,
370
+ /// <summary>
371
+ /// Rectangle with rounded corners
372
+ /// </summary>
373
+ RoundedCornerRectangle ,
374
+ /// <summary>
375
+ /// Circle
376
+ /// </summary>
377
+ Circle ,
378
+ /// <summary>
379
+ /// Rhombus
380
+ /// </summary>
381
+ Rhombus ,
382
+ /// <summary>
383
+ /// Odd shape
384
+ /// </summary>
385
+ OddShape
386
+ }
298
387
}
0 commit comments