MermaidSerialiser.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using System.Text;
  2. namespace RackPeek.Domain.Graph.Serialisers;
  3. /// <summary>
  4. /// Renders a <see cref="Graph"/> as a Mermaid flowchart string.
  5. /// Output is deterministic (nodes/edges in insertion order) so the
  6. /// same inventory always produces the same diagram — important for
  7. /// golden-file tests and for committing rendered diagrams to docs.
  8. /// </summary>
  9. public sealed class MermaidSerialiser {
  10. // Single neutral palette for a sleek monochrome look. Resource kind is
  11. // signalled by node shape, not colour, so diagrams stay calm even with
  12. // every kind of resource mixed in.
  13. private const string _nodeFill = "#1f2937"; // gray-800
  14. private const string _nodeStroke = "#52525b"; // zinc-600
  15. private const string _nodeText = "#e5e7eb"; // gray-200
  16. private const string _edgeStroke = "#52525b"; // zinc-600
  17. private const string _groupStroke = "#3f3f46"; // zinc-700
  18. private const string _groupText = "#a1a1aa"; // zinc-400
  19. private const string _nodeClass = "rpknode";
  20. private const string _groupClass = "rpkgroup";
  21. private const string _smallRowClass = "rpkrow";
  22. // Compact-mode (logical view) tuning. Small-row size controls how many
  23. // single-service host cards pack into one invisible row before wrapping.
  24. private const int _compactSmallRowSize = 4;
  25. private const int _compactTableColumns = 3;
  26. // Mermaid node shape per resource kind. Shape choice borrows from the
  27. // network-diagram conventions used by NetBox/draw.io/UniFi: hexagons for
  28. // security boundaries, stadiums for gateways, cylinders for compute,
  29. // circles for radios, etc. Looking at the silhouette alone should hint
  30. // at the role without colour or icons.
  31. private static readonly IReadOnlyDictionary<string, Shape> _shapes =
  32. new Dictionary<string, Shape>(StringComparer.OrdinalIgnoreCase) {
  33. // Physical / topology view shapes
  34. ["Firewall"] = new("{{\"", "\"}}"), // hexagon — boundary
  35. ["Router"] = new("([\"", "\"])"), // stadium — gateway
  36. ["Switch"] = new("[[\"", "\"]]"), // subroutine — distribution
  37. ["Server"] = new("[(\"", "\")]"), // cylinder — compute / storage
  38. ["AccessPoint"] = new("((\"", "\"))"), // circle — radio
  39. ["Ups"] = new("{\"", "\"}"), // rhombus — utility
  40. ["Desktop"] = new("(\"", "\")"), // rounded rect — endpoint
  41. ["Laptop"] = new("(\"", "\")"), // rounded rect — endpoint
  42. // Logical / service view shapes (don't appear with the physical
  43. // kinds in the same diagram, so shape reuse across views is OK)
  44. ["Service"] = new("[[\"", "\"]]"), // subroutine — consumable
  45. ["Hypervisor"] = new("([\"", "\"])"), // stadium — host gateway
  46. ["Vm"] = new("(\"", "\")"), // rounded — virtual machine
  47. ["Container"] = new("{{\"", "\"}}"), // hexagon — lightweight unit
  48. ["System"] = new("[\"", "\"]") // plain rect — fallback
  49. };
  50. private static readonly Shape _fallbackShape = new("[\"", "\"]");
  51. public string Serialise(Graph graph, string direction = "TD") {
  52. if (graph.RenderHint == GraphRenderHint.Compact)
  53. return SerialiseCompact(graph, direction);
  54. var sb = new StringBuilder();
  55. // Right-angle (Manhattan) edge routing — the visual signal that says
  56. // "this is a network diagram", borrowed from every serious topology
  57. // tool. Diagonal/curved lines read as "flowchart".
  58. //
  59. // Edge-label background is made transparent so connection labels read
  60. // as floating annotations rather than chunky chips that fight with
  61. // the line and the nodes for attention.
  62. // ELK renderer + orthogonal edge routing — Mermaid's default `dagre`
  63. // layout is fine for simple flowcharts but produces awkward arrow
  64. // landings on right-angle edges. ELK (Eclipse Layout Kernel) is the
  65. // engine NetBox/yEd/draw.io rely on for clean topology routing.
  66. //
  67. // Spacing values are generous on purpose — homelab diagrams read
  68. // better with air around nodes and between subnet/host clusters.
  69. // - `layout: elk` : use the Mermaid 11 ELK plugin (the
  70. // older `flowchart.defaultRenderer`
  71. // still works but is the legacy path).
  72. // - `elk.aspectRatio: 0.5` : ask ELK to favour tall over wide so
  73. // a host with dozens of services
  74. // doesn't fan out into a single row
  75. // kilometres long.
  76. // - `layered.wrapping.strategy : MULTI_EDGE
  77. // wraps an overlong layer into several
  78. // shorter ones — exactly what large
  79. // logical/service diagrams need.
  80. sb.AppendLine(
  81. "%%{init: {'layout': 'elk', 'flowchart': {'curve': 'step', 'nodeSpacing': 60, 'rankSpacing': 80, 'padding': 20, 'subGraphTitleMargin': {'top': 12, 'bottom': 12}}, 'elk': {'algorithm': 'layered', 'aspectRatio': 0.5, 'layered.wrapping.strategy': 'MULTI_EDGE', 'layered.nodePlacement.strategy': 'BRANDES_KOEPF'}, 'themeVariables': {'edgeLabelBackground': 'transparent', 'clusterBkg': 'transparent', 'clusterBorder': '" + _groupStroke + "'}}}%%");
  82. sb.Append("flowchart ").AppendLine(direction);
  83. EmitClassDefs(sb);
  84. Dictionary<string, string> idMap = AssignSafeIds(graph.Nodes);
  85. // Index groups & nodes for hierarchical emission.
  86. IReadOnlyList<GraphGroup> groups = graph.Groups ?? [];
  87. var childGroups = groups
  88. .GroupBy(g => g.ParentGroupId ?? string.Empty)
  89. .ToDictionary(g => g.Key, g => g.ToList());
  90. var groupsById = groups.ToDictionary(g => g.Id);
  91. HashSet<string> groupedNodeIds = new(
  92. groups.SelectMany(g => g.NodeIds), StringComparer.OrdinalIgnoreCase);
  93. // Emit top-level groups (parentGroupId == null/empty) — each recursively
  94. // contains its sub-groups and direct nodes.
  95. if (childGroups.TryGetValue(string.Empty, out List<GraphGroup>? topLevel))
  96. foreach (GraphGroup group in topLevel)
  97. EmitGroup(sb, group, childGroups, groupsById, graph.Nodes, idMap, indent: 1);
  98. // Emit any nodes that didn't fall into a group at the top level.
  99. foreach (GraphNode node in graph.Nodes) {
  100. if (groupedNodeIds.Contains(node.Id)) continue;
  101. EmitNode(sb, node, idMap, indent: 1);
  102. }
  103. if (graph.Edges.Count > 0) sb.AppendLine();
  104. foreach (GraphEdge edge in graph.Edges) {
  105. if (!idMap.TryGetValue(edge.Source, out var src) ||
  106. !idMap.TryGetValue(edge.Target, out var dst))
  107. continue;
  108. // Directional edges (runsOn, depends-on …) get an arrowhead so
  109. // the relationship reads correctly. Symmetric edges (port-to-port
  110. // physical connections) stay as plain lines.
  111. var connector = IsDirectional(edge.Kind) ? "-->" : "---";
  112. sb.Append(" ").Append(src);
  113. if (!string.IsNullOrWhiteSpace(edge.Label))
  114. sb.Append(' ').Append(connector).Append("|\"")
  115. .Append(Escape(edge.Label)).Append("\"|");
  116. else
  117. sb.Append(' ').Append(connector);
  118. sb.Append(' ').Append(dst).AppendLine();
  119. }
  120. // Dotted edges matching the dotted node borders. Labels float on top
  121. // (themeVariables.edgeLabelBackground=transparent) so the line stays
  122. // visually continuous through the label region.
  123. if (graph.Edges.Count > 0) {
  124. sb.AppendLine();
  125. sb.Append(" linkStyle default stroke:").Append(_edgeStroke)
  126. .AppendLine(",stroke-width:1.25px,stroke-dasharray:4 4,fill:none");
  127. }
  128. // Apply the group styling class to every subgraph id.
  129. foreach (GraphGroup group in groups) {
  130. sb.Append(" class ").Append(group.Id).Append(' ').Append(_groupClass).AppendLine();
  131. }
  132. return sb.ToString();
  133. }
  134. private void EmitGroup(
  135. StringBuilder sb,
  136. GraphGroup group,
  137. Dictionary<string, List<GraphGroup>> childGroups,
  138. Dictionary<string, GraphGroup> groupsById,
  139. IReadOnlyList<GraphNode> allNodes,
  140. Dictionary<string, string> idMap,
  141. int indent) {
  142. var pad = new string(' ', indent * 4);
  143. sb.Append(pad).Append("subgraph ").Append(group.Id)
  144. .Append(" [\"").Append(Escape(group.Label)).Append("\"]")
  145. .AppendLine();
  146. // Nested groups first
  147. if (childGroups.TryGetValue(group.Id, out List<GraphGroup>? children))
  148. foreach (GraphGroup child in children)
  149. EmitGroup(sb, child, childGroups, groupsById, allNodes, idMap, indent + 1);
  150. // Nodes that belong to this group directly (not via a child group)
  151. HashSet<string> nodesInChildren = new(
  152. (children ?? []).SelectMany(c => CollectAllNodeIds(c, childGroups)),
  153. StringComparer.OrdinalIgnoreCase);
  154. foreach (var nodeId in group.NodeIds) {
  155. if (nodesInChildren.Contains(nodeId)) continue;
  156. GraphNode? node = allNodes.FirstOrDefault(n =>
  157. string.Equals(n.Id, nodeId, StringComparison.OrdinalIgnoreCase));
  158. if (node is null) continue;
  159. EmitNode(sb, node, idMap, indent + 1);
  160. }
  161. sb.Append(pad).AppendLine("end");
  162. }
  163. private static IEnumerable<string> CollectAllNodeIds(
  164. GraphGroup group,
  165. Dictionary<string, List<GraphGroup>> childGroups) {
  166. foreach (var id in group.NodeIds) yield return id;
  167. if (!childGroups.TryGetValue(group.Id, out List<GraphGroup>? children)) yield break;
  168. foreach (GraphGroup c in children)
  169. foreach (var id in CollectAllNodeIds(c, childGroups))
  170. yield return id;
  171. }
  172. private void EmitNode(StringBuilder sb, GraphNode node, Dictionary<string, string> idMap, int indent) {
  173. var safeId = idMap[node.Id];
  174. Shape shape = ResolveShape(node.Kind);
  175. var label = BuildLabel(node);
  176. sb.Append(new string(' ', indent * 4)).Append(safeId)
  177. .Append(shape.Open).Append(label).Append(shape.Close)
  178. .Append(":::").Append(_nodeClass)
  179. .AppendLine();
  180. }
  181. private static string BuildLabel(GraphNode node) {
  182. // Two-line label: resource name on top, optional subtitle below.
  183. // Each use case decides what's most useful as a subtitle (kind for
  184. // the topology view, ip[:port] for the logical view) — the serialiser
  185. // is agnostic.
  186. var name = Escape(node.Label);
  187. if (string.IsNullOrWhiteSpace(node.Subtitle)) return name;
  188. return $"{name}<br/>{Escape(node.Subtitle!)}";
  189. }
  190. private static void EmitClassDefs(StringBuilder sb) {
  191. // Dotted node borders + dotted edges (via linkStyle below) keep the
  192. // whole diagram visually quiet — solid borders feel heavier than the
  193. // information they convey.
  194. sb.Append(" classDef ").Append(_nodeClass)
  195. .Append(" fill:").Append(_nodeFill)
  196. .Append(",stroke:").Append(_nodeStroke)
  197. .Append(",color:").Append(_nodeText)
  198. .Append(",stroke-width:1px,stroke-dasharray:3 3")
  199. .AppendLine();
  200. // Group containers: dotted outline, no fill, muted title. The cluster
  201. // background/border theme variables in the init directive cover the
  202. // built-in Mermaid styling; this class adds the dashed outline.
  203. sb.Append(" classDef ").Append(_groupClass)
  204. .Append(" fill:none,stroke:").Append(_groupStroke)
  205. .Append(",color:").Append(_groupText)
  206. .Append(",stroke-width:1px,stroke-dasharray:3 3")
  207. .AppendLine();
  208. sb.AppendLine();
  209. }
  210. private static Dictionary<string, string> AssignSafeIds(IReadOnlyList<GraphNode> nodes) {
  211. // Mermaid node IDs must be a small alphabet (letters, digits, underscore).
  212. // Map resource names → deterministic safe IDs, suffixing on collision.
  213. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  214. var taken = new HashSet<string>(StringComparer.Ordinal);
  215. foreach (GraphNode node in nodes) {
  216. var baseId = "n_" + Slug(node.Id);
  217. var candidate = baseId;
  218. var counter = 2;
  219. while (!taken.Add(candidate)) candidate = $"{baseId}_{counter++}";
  220. result[node.Id] = candidate;
  221. }
  222. return result;
  223. }
  224. private static string Slug(string value) {
  225. var sb = new StringBuilder(value.Length);
  226. foreach (var c in value)
  227. sb.Append(char.IsLetterOrDigit(c) ? char.ToLowerInvariant(c) : '_');
  228. return sb.Length == 0 ? "node" : sb.ToString();
  229. }
  230. private static Shape ResolveShape(string kind) =>
  231. _shapes.TryGetValue(kind, out Shape shape) ? shape : _fallbackShape;
  232. private static string Escape(string value) =>
  233. value.Replace("\\", "\\\\").Replace("\"", "\\\"");
  234. private static readonly HashSet<string> _directionalEdgeKinds = new(StringComparer.OrdinalIgnoreCase) {
  235. "runsOn",
  236. "dependsOn"
  237. };
  238. private static bool IsDirectional(string kind) =>
  239. _directionalEdgeKinds.Contains(kind);
  240. private readonly record struct Shape(string Open, string Close);
  241. // ---------------------------------------------------------------------
  242. // Compact mode (logical view): each system becomes a single "host card"
  243. // whose label is an HTML table of its services. No edges are drawn —
  244. // subgraph containment carries the runs-on relationship. Sibling cards
  245. // are chained vertically via invisible ~~~ links so ELK doesn't fan
  246. // them out into a kilometre-wide row, and single-row hosts are packed
  247. // into invisible row subgraphs of N to use the horizontal space.
  248. // ---------------------------------------------------------------------
  249. private string SerialiseCompact(Graph graph, string direction) {
  250. var sb = new StringBuilder();
  251. // htmlLabels + securityLevel: 'loose' let us put raw HTML inside the
  252. // node labels. aspectRatio is set above 0.5 because compact mode
  253. // already wraps long sibling lists itself via the small-row packing.
  254. sb.AppendLine(
  255. "%%{init: {'layout': 'elk', 'flowchart': {'curve': 'step', 'nodeSpacing': 10, 'rankSpacing': 10, 'padding': 0, 'htmlLabels': true, 'subGraphTitleMargin': {'top': 0, 'bottom': 0}, 'titleTopMargin': 0}, 'securityLevel': 'loose', 'elk': {'algorithm': 'layered', 'padding': '[top=0,bottom=4,left=6,right=6]', 'spacing.nodeNode': 8, 'spacing.nodeNodeBetweenLayers': 8, 'spacing.componentComponent': 6, 'layered.spacing.nodeNodeBetweenLayers': 8, 'nodeLabels.placement': '[H_CENTER, V_TOP, INSIDE]'}, 'themeVariables': {'edgeLabelBackground': 'transparent', 'clusterBkg': 'transparent', 'clusterBorder': '" + _groupStroke + "'}}}%%");
  256. sb.Append("flowchart ").AppendLine(direction);
  257. EmitClassDefs(sb);
  258. sb.Append(" classDef ").Append(_smallRowClass)
  259. .AppendLine(" fill:none,stroke:none,color:transparent");
  260. sb.AppendLine();
  261. Dictionary<string, string> idMap = AssignSafeIds(graph.Nodes);
  262. IReadOnlyList<GraphGroup> groups = graph.Groups ?? [];
  263. var childGroups = groups
  264. .GroupBy(g => g.ParentGroupId ?? string.Empty)
  265. .ToDictionary(g => g.Key, g => g.ToList());
  266. HashSet<string> groupedNodeIds = new(
  267. groups.SelectMany(g => g.NodeIds), StringComparer.OrdinalIgnoreCase);
  268. // Invisible chains and packed-row ids are collected during traversal
  269. // and emitted in a block at the end.
  270. var chains = new List<IReadOnlyList<string>>();
  271. var smallRowIds = new List<string>();
  272. void Emit(GraphGroup group, int indent) {
  273. var pad = new string(' ', indent * 4);
  274. sb.Append(pad).Append("subgraph ").Append(group.Id)
  275. .Append(" [\"").Append(Escape(group.Label)).Append("\"]").AppendLine();
  276. List<GraphGroup> subChildren =
  277. childGroups.TryGetValue(group.Id, out List<GraphGroup>? cs) ? cs : new();
  278. foreach (GraphGroup child in subChildren) Emit(child, indent + 1);
  279. if (subChildren.Count > 1)
  280. chains.Add(subChildren.Select(c => c.Id).ToList());
  281. HashSet<string> nodesInChildren = new(
  282. subChildren.SelectMany(c => CollectAllNodeIds(c, childGroups)),
  283. StringComparer.OrdinalIgnoreCase);
  284. // Partition the group's direct nodes into "big" cards (host with
  285. // multiple service rows) and "small" cards (no rows or one row).
  286. // Bigs get a dedicated row each; smalls pack horizontally.
  287. var bigs = new List<GraphNode>();
  288. var smalls = new List<GraphNode>();
  289. foreach (var nodeId in group.NodeIds) {
  290. if (nodesInChildren.Contains(nodeId)) continue;
  291. GraphNode? node = graph.Nodes.FirstOrDefault(n =>
  292. string.Equals(n.Id, nodeId, StringComparison.OrdinalIgnoreCase));
  293. if (node is null) continue;
  294. if ((node.Rows?.Count ?? 0) > 1) bigs.Add(node);
  295. else smalls.Add(node);
  296. }
  297. var verticalChain = new List<string>();
  298. foreach (GraphNode b in bigs) {
  299. EmitCompactNode(sb, b, idMap, indent + 1);
  300. verticalChain.Add(idMap[b.Id]);
  301. }
  302. for (int i = 0, rowIdx = 0; i < smalls.Count; i += _compactSmallRowSize, rowIdx++) {
  303. var slice = smalls.Skip(i).Take(_compactSmallRowSize).ToList();
  304. // Single small host doesn't need an invisible row wrapper —
  305. // wrapping adds another nested subgraph (with its own
  306. // padding/title overhead) for no layout benefit.
  307. if (slice.Count == 1) {
  308. EmitCompactNode(sb, slice[0], idMap, indent + 1);
  309. verticalChain.Add(idMap[slice[0].Id]);
  310. continue;
  311. }
  312. var rowId = group.Id + "__srow" + rowIdx;
  313. smallRowIds.Add(rowId);
  314. verticalChain.Add(rowId);
  315. sb.Append(pad).Append(" subgraph ").Append(rowId).AppendLine(" [\" \"]");
  316. sb.Append(pad).Append(" direction LR").AppendLine();
  317. foreach (GraphNode s in slice)
  318. EmitCompactNode(sb, s, idMap, indent + 2);
  319. sb.Append(pad).AppendLine(" end");
  320. sb.Append(pad).Append(" ");
  321. sb.AppendJoin(" ~~~ ", slice.Select(s => idMap[s.Id]));
  322. sb.AppendLine();
  323. }
  324. if (verticalChain.Count > 1) chains.Add(verticalChain);
  325. sb.Append(pad).AppendLine("end");
  326. }
  327. if (childGroups.TryGetValue(string.Empty, out List<GraphGroup>? topLevel)) {
  328. foreach (GraphGroup g in topLevel) Emit(g, 1);
  329. if (topLevel.Count > 1)
  330. chains.Add(topLevel.Select(g => g.Id).ToList());
  331. }
  332. // Ungrouped nodes (uncommon in compact mode but render them sanely).
  333. foreach (GraphNode node in graph.Nodes) {
  334. if (groupedNodeIds.Contains(node.Id)) continue;
  335. EmitCompactNode(sb, node, idMap, 1);
  336. }
  337. // Invisible vertical chains last — these are what tell ELK to stack
  338. // siblings vertically instead of flowing into one long row.
  339. if (chains.Count > 0) sb.AppendLine();
  340. foreach (IReadOnlyList<string> chain in chains) {
  341. if (chain.Count < 2) continue;
  342. sb.Append(" ");
  343. sb.AppendJoin(" ~~~ ", chain);
  344. sb.AppendLine();
  345. }
  346. sb.AppendLine();
  347. foreach (GraphGroup group in groups)
  348. sb.Append(" class ").Append(group.Id).Append(' ').Append(_groupClass).AppendLine();
  349. foreach (var rowId in smallRowIds)
  350. sb.Append(" class ").Append(rowId).Append(' ').Append(_smallRowClass).AppendLine();
  351. return sb.ToString();
  352. }
  353. private void EmitCompactNode(StringBuilder sb, GraphNode node, Dictionary<string, string> idMap, int indent) {
  354. var safeId = idMap[node.Id];
  355. Shape shape = ResolveShape(node.Kind);
  356. var label = BuildCompactLabel(node);
  357. sb.Append(new string(' ', indent * 4)).Append(safeId)
  358. .Append(shape.Open).Append(label).Append(shape.Close)
  359. .Append(":::").Append(_nodeClass)
  360. .AppendLine();
  361. }
  362. private static string BuildCompactLabel(GraphNode node) {
  363. var sb = new StringBuilder();
  364. sb.Append("<div style='text-align:left;font-family:system-ui;padding:4px 6px'>");
  365. sb.Append("<div style='font-weight:600;font-size:14px'>");
  366. sb.Append(EscapeHtml(node.Label));
  367. if (!string.IsNullOrWhiteSpace(node.Subtitle)) {
  368. sb.Append(" - <span style='color:#9ca3af'>");
  369. sb.Append(EscapeHtml(node.Subtitle!));
  370. sb.Append("</span>");
  371. }
  372. sb.Append("</div>");
  373. if (node.Rows is { Count: > 0 }) {
  374. sb.Append("<hr style='border:none;border-top:1px dashed #52525b;margin:6px 0'>");
  375. sb.Append("<table style='border-collapse:collapse;font-size:11px'>");
  376. for (var i = 0; i < node.Rows.Count; i += _compactTableColumns) {
  377. sb.Append("<tr>");
  378. for (var c = 0; c < _compactTableColumns; c++) {
  379. var idx = i + c;
  380. if (idx >= node.Rows.Count) { sb.Append("<td></td>"); continue; }
  381. GraphNodeRow row = node.Rows[idx];
  382. sb.Append("<td style='padding:2px 10px 2px 0;white-space:nowrap'>");
  383. sb.Append("<span style='color:#e5e7eb'>").Append(EscapeHtml(row.Name)).Append("</span>");
  384. if (!string.IsNullOrEmpty(row.Detail))
  385. sb.Append("<span style='color:#71717a'>").Append(EscapeHtml(row.Detail!)).Append("</span>");
  386. sb.Append("</td>");
  387. }
  388. sb.Append("</tr>");
  389. }
  390. sb.Append("</table>");
  391. }
  392. sb.Append("</div>");
  393. // Mermaid label is wrapped in "...", so any " in our HTML must be
  394. // entity-encoded. We avoid literal " in inline styles by using
  395. // single quotes; this last pass catches anything still embedded.
  396. return sb.ToString().Replace("\"", "&quot;");
  397. }
  398. private static string EscapeHtml(string s) =>
  399. s.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
  400. }