diff --git a/src/MarkdownSnippets/Processing/SnippetKey.cs b/src/MarkdownSnippets/Processing/SnippetKey.cs index 252b4ae1..476107b7 100644 --- a/src/MarkdownSnippets/Processing/SnippetKey.cs +++ b/src/MarkdownSnippets/Processing/SnippetKey.cs @@ -11,8 +11,12 @@ public static bool ExtractStartCommentSnippet(Line line, [NotNullWhen(true)] out var substring = lineCurrent[14..]; var indexOf = substring.IndexOf("-->"); - key = substring[..indexOf] - .Trim(); + if (indexOf < 0) + { + throw new SnippetException($"Could not find closing '-->' in: {line.Original}. Path: {line.Path}. Line: {line.LineNumber}"); + } + + key = substring[..indexOf].Trim(); return true; } @@ -32,6 +36,11 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] var substring = lineCurrent[18..]; // after ""); + if (indexOf < 0) + { + throw new SnippetException($"Could not find closing '-->' in: {line.Original}. Path: {line.Path}. Line: {line.LineNumber}"); + } + var value = substring[..indexOf].Trim(); // Check for optional second URL separated by whitespace diff --git a/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentSnippet.cs b/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentSnippet.cs index 0873c112..6ca54769 100644 --- a/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentSnippet.cs +++ b/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentSnippet.cs @@ -13,4 +13,13 @@ public void Simple() Assert.True(SnippetKey.ExtractStartCommentSnippet(new("", "path", 1), out var key)); Assert.Equal("snippet", key); } + + [Fact] + public void MissingClosingComment_Throws() + { + var line = new Line("", exception.Message); + Assert.Contains("test.md", exception.Message); + } } \ No newline at end of file diff --git a/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentWebSnippet.cs b/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentWebSnippet.cs new file mode 100644 index 00000000..f13da0b9 --- /dev/null +++ b/src/Tests/MarkdownProcessor/SnippetKey_ExtractStartCommentWebSnippet.cs @@ -0,0 +1,19 @@ +public class SnippetKey_ExtractStartCommentWebSnippet +{ + [Fact] + public void Simple() + { + Assert.True(SnippetKey.ExtractStartCommentWebSnippet(new("", "path", 1), out var url, out var key)); + Assert.Equal("https://example.com/file.cs", url); + Assert.Equal("mysnippet", key); + } + + [Fact] + public void MissingClosingComment_Throws() + { + var line = new Line("", exception.Message); + Assert.Contains("test.md", exception.Message); + } +} \ No newline at end of file