diff --git a/README.md b/README.md index a90cee8..9f799a8 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Will generate the following Mermaid output: ```mermaid --- -title: Basic usage +title: "Basic usage" --- flowchart TD start["Start"] @@ -183,7 +183,7 @@ Will generate the following Mermaid output: ```mermaid --- -title: Various node shapes +title: "Various node shapes" --- flowchart TD rectangle["Rectangle"] @@ -247,7 +247,7 @@ Will generate the following Mermaid output: ```mermaid --- -title: Link types +title: "Link types" --- flowchart TD a["A"] @@ -308,7 +308,7 @@ Will generate the following Mermaid output: ```mermaid --- -title: Using subgraphs +title: "Using subgraphs" --- flowchart TD n["Node"] @@ -373,7 +373,7 @@ Will generate the following Mermaid output: ```mermaid --- -title: Styling nodes +title: "Styling nodes" --- flowchart TD a["A"] @@ -427,7 +427,7 @@ Will generate the following Mermaid output: ```mermaid --- -title: Styling links +title: "Styling links" --- flowchart TD a["A"] diff --git a/src/Mermaid.Flowcharts/FlowchartTitle.cs b/src/Mermaid.Flowcharts/FlowchartTitle.cs index d3c5067..75b7a0f 100644 --- a/src/Mermaid.Flowcharts/FlowchartTitle.cs +++ b/src/Mermaid.Flowcharts/FlowchartTitle.cs @@ -32,7 +32,7 @@ public static FlowchartTitle FromString(string text) throw new ArgumentException("Flowchart title must not be whitespace.", nameof(text)); } - return new(text); + return new(text.Replace("\"", "\\\"")); } public override string ToString() @@ -43,7 +43,7 @@ public string ToMermaidString(int indentations = 0, string indentationText = " StringBuilder flowchartTitleBuilder = new(); flowchartTitleBuilder .AppendLine($"{indentationText.Repeat(indentations)}---") - .AppendLine($"{indentationText.Repeat(indentations)}title: {Text}") + .AppendLine($"{indentationText.Repeat(indentations)}title: \"{Text}\"") .Append($"{indentationText.Repeat(indentations)}---"); return flowchartTitleBuilder.ToString(); } diff --git a/src/Mermaid.Flowcharts/Nodes/NodeIdentifier.cs b/src/Mermaid.Flowcharts/Nodes/NodeIdentifier.cs index c7f61eb..8ce15e2 100644 --- a/src/Mermaid.Flowcharts/Nodes/NodeIdentifier.cs +++ b/src/Mermaid.Flowcharts/Nodes/NodeIdentifier.cs @@ -28,24 +28,24 @@ public static NodeIdentifier Create() public static NodeIdentifier FromString(string text) { - if (text.StartsWith('_') || text.StartsWith('.') || text.StartsWith('-')) + if (string.IsNullOrEmpty(text)) { - throw new ArgumentException("Identifier must not start with a separator.", nameof(text)); + throw new ArgumentException("Identifier must not be empty.", nameof(text)); } - if (text.EndsWith('_') || text.EndsWith('.') || text.EndsWith('-')) + if (string.IsNullOrWhiteSpace(text)) { - throw new ArgumentException("Identifier must not end with a separator.", nameof(text)); + throw new ArgumentException("Identifier must not be whitespace.", nameof(text)); } - if (string.IsNullOrEmpty(text)) + if (text.StartsWith('_') || text.StartsWith('.') || text.StartsWith('-')) { - throw new ArgumentException("Identifier must not be empty.", nameof(text)); + throw new ArgumentException("Identifier must not start with a separator.", nameof(text)); } - if (string.IsNullOrWhiteSpace(text)) + if (text.EndsWith('_') || text.EndsWith('.') || text.EndsWith('-')) { - throw new ArgumentException("Identifier must not be whitespace.", nameof(text)); + throw new ArgumentException("Identifier must not end with a separator.", nameof(text)); } bool containsDisallowedValue = text.AsSpan().IndexOfAnyExcept(AllowedCharacters) > -1; diff --git a/tests/Mermaid.Flowcharts.Tests/FlowchartTitleTests.cs b/tests/Mermaid.Flowcharts.Tests/FlowchartTitleTests.cs index 42ca01f..ee4f059 100644 --- a/tests/Mermaid.Flowcharts.Tests/FlowchartTitleTests.cs +++ b/tests/Mermaid.Flowcharts.Tests/FlowchartTitleTests.cs @@ -75,7 +75,14 @@ public void Title_ShouldThrow_WhenNewline(string newline) 'a', """ --- - title: a + title: "a" + --- + """)] + [InlineData( + '"', + """ + --- + title: "\"" --- """)] public void Title_ShouldToMermaidString_SingleLetter(char letter, string expected) @@ -98,7 +105,17 @@ public void Title_ShouldToMermaidString_SingleLetter(char letter, string expecte " ", """ --- - title: a + title: "a" + --- + """ + )] + [InlineData( + '"', + 2, + " ", + """ + --- + title: "\"" --- """ )] diff --git a/tests/Mermaid.Flowcharts.Tests/ReadmeTests.cs b/tests/Mermaid.Flowcharts.Tests/ReadmeTests.cs index 2ead622..c0ef09b 100644 --- a/tests/Mermaid.Flowcharts.Tests/ReadmeTests.cs +++ b/tests/Mermaid.Flowcharts.Tests/ReadmeTests.cs @@ -38,7 +38,7 @@ public void BasicUsage() string expected = """ --- - title: Basic usage + title: "Basic usage" --- flowchart TD start["Start"] @@ -98,7 +98,7 @@ public void VariousNodeShapes() string expected = """ --- - title: Various node shapes + title: "Various node shapes" --- flowchart TD rectangle["Rectangle"] @@ -156,7 +156,7 @@ public void LinkTypes() string expected = """ --- - title: Link types + title: "Link types" --- flowchart TD a["A"] @@ -210,7 +210,7 @@ public void UsingSubgraphs() string expected = """ --- - title: Using subgraphs + title: "Using subgraphs" --- flowchart TD n["Node"] @@ -258,7 +258,7 @@ public void StylingNodes() string expected = """ --- - title: Styling nodes + title: "Styling nodes" --- flowchart TD a["A"] @@ -306,7 +306,7 @@ public void StylingLinks() string expected = """ --- - title: Styling links + title: "Styling links" --- flowchart TD a["A"]