Skip to content

Commit 8781884

Browse files
committed
Added shapes for better accessibility
1 parent 49a12f3 commit 8781884

File tree

2 files changed

+107
-18
lines changed

2 files changed

+107
-18
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,11 @@ private static void WriteTreeDocument(string openapi, OpenApiDocument document,
605605

606606
writer.WriteLine(@"<div>");
607607
// write a span for each mermaidcolorscheme
608-
foreach (var color in OpenApiUrlTreeNode.MermaidColorScheme)
608+
foreach (var style in OpenApiUrlTreeNode.MermaidNodeStyles)
609609
{
610-
writer.WriteLine($"<span style=\"padding:2px;background-color:{color.Value}\">{color.Key.Replace("_"," ")}</span>");
610+
writer.WriteLine($"<span style=\"padding:2px;background-color:{style.Value.Color};border: 2px solid\">{style.Key.Replace("_"," ")}</span>");
611611
}
612-
writer.WriteLine("/div");
612+
writer.WriteLine("</div>");
613613
writer.WriteLine();
614614
writer.WriteLine("```mermaid");
615615
rootNode.WriteMermaid(writer);

src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ public void AddAdditionalData(Dictionary<string, List<string>> additionalData)
244244
public void WriteMermaid(StreamWriter writer)
245245
{
246246
writer.WriteLine("graph LR");
247-
foreach (var color in MermaidColorScheme)
247+
foreach (var style in MermaidNodeStyles)
248248
{
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");
250250
}
251251

252252
ProcessNode(this, writer);
@@ -255,35 +255,71 @@ public void WriteMermaid(StreamWriter writer)
255255
/// <summary>
256256
/// Dictionary that maps a set of HTTP methods to HTML color. Keys are sorted, uppercased, concatenated HTTP methods.
257257
/// </summary>
258-
public static Dictionary<string, string> MermaidColorScheme = new Dictionary<string, string>
258+
public static Dictionary<string, MermaidNodeStyle> MermaidNodeStyles = new Dictionary<string, MermaidNodeStyle>
259259
{
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) },
268269
};
269270

270271
private static void ProcessNode(OpenApiUrlTreeNode node, StreamWriter writer)
271272
{
272273
var path = string.IsNullOrEmpty(node.Path) ? "/" : SanitizeMermaidNode(node.Path);
274+
var methods = GetMethods(node);
275+
var (startChar, endChar) = GetShapeDelimiters(methods);
273276
foreach (var child in node.Children)
274277
{
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}");
276281
ProcessNode(child.Value, writer);
277282
}
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))
279290
.Distinct()
280291
.Select(o => o.ToString().ToUpper())
281292
.OrderBy(o => o)
282293
.ToList());
283-
if (String.IsNullOrEmpty(methods)) methods = "OTHER";
284-
writer.WriteLine($"class {path} {methods}");
285294
}
286295

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+
}
287323
private static string SanitizeMermaidNode(string token)
288324
{
289325
return token.Replace("\\", "/")
@@ -295,4 +331,57 @@ private static string SanitizeMermaidNode(string token)
295331
.Replace("default", "def_ault"); // default is a reserved word for classes
296332
}
297333
}
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+
}
298387
}

0 commit comments

Comments
 (0)