Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/MarkdownSnippets/Processing/SnippetKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -32,6 +36,11 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)]

var substring = lineCurrent[18..]; // after "<!-- web-snippet: "
var indexOf = substring.IndexOf("-->");
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ public void Simple()
Assert.True(SnippetKey.ExtractStartCommentSnippet(new("<!-- snippet: snippet -->", "path", 1), out var key));
Assert.Equal("snippet", key);
}

[Fact]
public void MissingClosingComment_Throws()
{
var line = new Line("<!-- snippet: my-snippet", "test.md", 5);
var exception = Assert.Throws<SnippetException>(() => SnippetKey.ExtractStartCommentSnippet(line, out _));
Assert.Contains("-->", exception.Message);
Assert.Contains("test.md", exception.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class SnippetKey_ExtractStartCommentWebSnippet
{
[Fact]
public void Simple()
{
Assert.True(SnippetKey.ExtractStartCommentWebSnippet(new("<!-- web-snippet: https://example.com/file.cs#mysnippet -->", "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("<!-- web-snippet: https://example.com/file.cs#mysnippet", "test.md", 10);
var exception = Assert.Throws<SnippetException>(() => SnippetKey.ExtractStartCommentWebSnippet(line, out _, out _));
Assert.Contains("-->", exception.Message);
Assert.Contains("test.md", exception.Message);
}
}