From d96db86d46a9fbc48d50c1017ef5febe658102cb Mon Sep 17 00:00:00 2001 From: David Gardiner Date: Thu, 30 Oct 2025 12:04:02 +1030 Subject: [PATCH 1/3] Fix round-tripping of web-snippet - Ensure that content of web-snippet is refreshed --- .../Processing/MarkdownProcessor.cs | 62 +++++++++++++------ src/MarkdownSnippets/Processing/SnippetKey.cs | 29 +++++++++ ...s.WithCommentWebSnippetUpdate.verified.txt | 22 +++++++ .../MarkdownProcessorTests.cs | 22 +++++++ 4 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithCommentWebSnippetUpdate.verified.txt diff --git a/src/MarkdownSnippets/Processing/MarkdownProcessor.cs b/src/MarkdownSnippets/Processing/MarkdownProcessor.cs index 318aac61..63333b88 100644 --- a/src/MarkdownSnippets/Processing/MarkdownProcessor.cs +++ b/src/MarkdownSnippets/Processing/MarkdownProcessor.cs @@ -230,6 +230,20 @@ void AppendWebSnippet(string url, string snippetKey) continue; } + if (SnippetKey.ExtractStartCommentWebSnippet(line, out url, out snippetKey)) + { + AppendWebSnippet(url, snippetKey); + + index++; + + lines.RemoveUntil( + index, + "", + relativePath, + line); + continue; + } + if (line.Current.TrimStart() == "") { tocLine = line; @@ -302,34 +316,46 @@ void ProcessWebSnippetLine(Action appendLine, List missi { appendLine($""); // Download file content - var (success, content) = Downloader.DownloadContent(url).GetAwaiter().GetResult(); - if (!success || string.IsNullOrWhiteSpace(content)) + try { - var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path); - missings.Add(missing); - appendLine("```"); - appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **"); - appendLine("```"); + var (success, content) = Downloader.DownloadContent(url).GetAwaiter().GetResult(); + if (!success || string.IsNullOrWhiteSpace(content)) + { + var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path); + missings.Add(missing); + appendLine("```"); + appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **"); + appendLine("```"); + appendLine(""); + return; + } + // Extract snippets from content + using var reader = new StringReader(content); + var snippets = FileSnippetExtractor.Read(reader, url); + var found = snippets.FirstOrDefault(_ => _.Key == snippetKey); + if (found == null) + { + var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path); + missings.Add(missing); + appendLine("```"); + appendLine($"** Could not find snippet '{snippetKey}' in '{url}' **"); + appendLine("```"); + appendLine(""); + return; + } + appendSnippets(snippetKey, [found], appendLine); appendLine(""); - return; + used.Add(found); } - // Extract snippets from content - using var reader = new StringReader(content); - var snippets = FileSnippetExtractor.Read(reader, url); - var found = snippets.FirstOrDefault(_ => _.Key == snippetKey); - if (found == null) + catch { var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path); missings.Add(missing); appendLine("```"); - appendLine($"** Could not find snippet '{snippetKey}' in '{url}' **"); + appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **"); appendLine("```"); appendLine(""); - return; } - appendSnippets(snippetKey, [found], appendLine); - appendLine(""); - used.Add(found); } bool TryGetSnippets( diff --git a/src/MarkdownSnippets/Processing/SnippetKey.cs b/src/MarkdownSnippets/Processing/SnippetKey.cs index d067f76e..b48b7795 100644 --- a/src/MarkdownSnippets/Processing/SnippetKey.cs +++ b/src/MarkdownSnippets/Processing/SnippetKey.cs @@ -16,6 +16,32 @@ public static bool ExtractStartCommentSnippet(Line line, [NotNullWhen(true)] out return true; } + public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey) + { + var lineCurrent = line.Current.TrimStart(); + if (!IsStartCommentWebSnippetLine(lineCurrent)) + { + url = null; + snippetKey = null; + return false; + } + + var substring = lineCurrent[18..]; // after ""); + var value = substring[..indexOf].Trim(); + + var hashIndex = value.LastIndexOf('#'); + if (hashIndex < 0 || hashIndex == value.Length - 1) + { + url = null; + snippetKey = null; + return false; + } + url = value[..hashIndex]; + snippetKey = value[(hashIndex + 1)..]; + return true; + } + public static bool ExtractSnippet(Line line, [NotNullWhen(true)] out string? key) { var lineCurrent = line.Current.TrimStart(); @@ -65,4 +91,7 @@ public static bool IsStartCommentSnippetLine(string line) => public static bool IsWebSnippetLine(string line) => line.StartsWith("web-snippet:", StringComparison.OrdinalIgnoreCase); + + public static bool IsStartCommentWebSnippetLine(string line) => + line.StartsWith(" +```txt +Some code +``` + + +after +} \ No newline at end of file diff --git a/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs index 25af7d80..9f03156b 100644 --- a/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs +++ b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs @@ -792,6 +792,28 @@ public Task WithIndentedMultiLineSnippet() ]); } + [Fact] + public Task WithCommentWebSnippetUpdate() + { + var content = """ + + before + + + OLD CONTENT + THAT SHOULD BE + REPLACED + + + after + + """; + + return SnippetVerifier.Verify( + DocumentConvention.InPlaceOverwrite, + content); + } + static Snippet SnippetBuild(string language, string key) => Snippet.Build( language: language, From 0bae97d5df00f40998f592c66c559da36f612fe9 Mon Sep 17 00:00:00 2001 From: David Gardiner Date: Thu, 30 Oct 2025 15:08:06 +1030 Subject: [PATCH 2/3] Fix anchor links for web-snippets --- readme.md | 6 ++--- .../Processing/SnippetMarkdownHandling.cs | 24 ++++++++++++++++--- ...andlingTests.AppendWebSnippet.verified.txt | 5 ++++ src/Tests/SnippetMarkdownHandlingTests.cs | 21 ++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippet.verified.txt diff --git a/readme.md b/readme.md index 8be83bb1..d60c1869 100644 --- a/readme.md +++ b/readme.md @@ -219,11 +219,11 @@ Files are downloaded to `%temp%MarkdownSnippets` with a maximum of 100 files kep Will render: - + ```txt Some code ``` - anchor + anchor ### Including a full file @@ -347,7 +347,7 @@ switch (linkFormat) throw new($"Unknown LinkFormat: {linkFormat}"); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs b/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs index 8c08af40..ab4eab6d 100644 --- a/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs +++ b/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs @@ -50,7 +50,19 @@ void WriteSnippet(Action appendLine, Snippet snippet, uint index) static string GetAnchorText(Snippet snippet, uint index) { - var id = $"snippet-{snippet.Key}"; + string id; + var pathStr = snippet.Path; + if (pathStr != null && pathStr.StartsWith("http", StringComparison.OrdinalIgnoreCase)) + { + // For web-snippets, include the full URL and the snippet key, with '#' encoded + var combined = $"snippet-{pathStr}#{snippet.Key}"; + id = combined.Replace("#", "%23"); + } + else + { + id = $"snippet-{snippet.Key}"; + } + if (index == 0) { return id; @@ -62,12 +74,18 @@ static string GetAnchorText(Snippet snippet, uint index) string GetSupText(Snippet snippet, string anchor) { var linkForAnchor = $"anchor"; - if (snippet.Path == null || linkFormat == LinkFormat.None) + var pathLocal = snippet.Path; + if (pathLocal == null || linkFormat == LinkFormat.None) { return linkForAnchor; } - var path = snippet.Path.Replace('\\', '/'); + var path = pathLocal.Replace('\\', '/'); + if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) + { + // For web-snippets: link back to the original URL with the snippet key as a hash + return $"anchor"; + } if (!path.StartsWith(targetDirectory)) { // if file is not in the targetDirectory then the url wont work diff --git a/src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippet.verified.txt b/src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippet.verified.txt new file mode 100644 index 00000000..e264ecab --- /dev/null +++ b/src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippet.verified.txt @@ -0,0 +1,5 @@ + +```cs +theValue +``` +anchor diff --git a/src/Tests/SnippetMarkdownHandlingTests.cs b/src/Tests/SnippetMarkdownHandlingTests.cs index 1b080f8f..4ea29b8e 100644 --- a/src/Tests/SnippetMarkdownHandlingTests.cs +++ b/src/Tests/SnippetMarkdownHandlingTests.cs @@ -70,6 +70,27 @@ public Task AppendHashed() return Verify(builder.ToString()); } + [Fact] + public Task AppendWebSnippet() + { + var builder = new StringBuilder(); + var webSnippet = Snippet.Build( + startLine: 1, + endLine: 2, + value: "theValue", + key: "mysnippet", + language: "cs", + path: "http://example.com/file.cs", + expressiveCode: null); + var markdownHandling = new SnippetMarkdownHandling(Environment.CurrentDirectory, LinkFormat.GitHub, false); + using (var writer = new StringWriter(builder)) + { + markdownHandling.Append("key1", new List { webSnippet }, writer.WriteLine); + } + + return Verify(builder.ToString()); + } + static List Snippets() => [Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", Environment.CurrentDirectory, expressiveCode: null)]; } \ No newline at end of file From 4d823199c258ccde7d4f36bda69bc987a91e7d60 Mon Sep 17 00:00:00 2001 From: David Gardiner Date: Thu, 30 Oct 2025 15:32:14 +1030 Subject: [PATCH 3/3] web-snippet can optionally take second URL - If this exists, then it will be used for the a href link and line numbers will be passed (eg. suitable for GitHub content) --- readme.md | 16 ++++- readme.source.md | 18 +++++- .../Processing/MarkdownProcessor.cs | 35 ++++++++--- src/MarkdownSnippets/Processing/SnippetKey.cs | 61 +++++++++++++++---- .../Processing/SnippetMarkdownHandling.cs | 6 +- src/MarkdownSnippets/Reading/Snippet.cs | 39 +++++++++++- ...hCommentWebSnippetWithViewUrl.verified.txt | 22 +++++++ ...thInlineWebSnippetWithViewUrl.verified.txt | 22 +++++++ .../MarkdownProcessorTests.cs | 40 ++++++++++++ ...s.AppendWebSnippetWithViewUrl.verified.txt | 5 ++ src/Tests/SnippetMarkdownHandlingTests.cs | 22 +++++++ 11 files changed, 260 insertions(+), 26 deletions(-) create mode 100644 src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithCommentWebSnippetWithViewUrl.verified.txt create mode 100644 src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithInlineWebSnippetWithViewUrl.verified.txt create mode 100644 src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippetWithViewUrl.verified.txt diff --git a/readme.md b/readme.md index d60c1869..f5b85fae 100644 --- a/readme.md +++ b/readme.md @@ -226,6 +226,20 @@ Will render: anchor +You can optionally provide a second URL that will be used for the source link. This is useful when the raw content URL is different from the view URL. For example: + +`web-snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet https://github.com/SimonCropp/MarkdownSnippets/blob/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet` + +Will render: + + + + ```txt + Some code + ``` + anchor + + ### Including a full file If no snippet is found matching the defined name. The target directory will be searched for a file matching that name. For example: @@ -347,7 +361,7 @@ switch (linkFormat) throw new($"Unknown LinkFormat: {linkFormat}"); } ``` -snippet source | anchor +snippet source | anchor diff --git a/readme.source.md b/readme.source.md index 5c290083..8023a1e7 100644 --- a/readme.source.md +++ b/readme.source.md @@ -212,11 +212,25 @@ Files are downloaded to `%temp%MarkdownSnippets` with a maximum of 100 files kep Will render: - + ```txt Some code ``` - anchor + anchor + + +You can optionally provide a second URL that will be used for the source link. This is useful when the raw content URL is different from the view URL. For example: + +`web-snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet https://github.com/SimonCropp/MarkdownSnippets/blob/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet` + +Will render: + + + + ```txt + Some code + ``` + anchor ### Including a full file diff --git a/src/MarkdownSnippets/Processing/MarkdownProcessor.cs b/src/MarkdownSnippets/Processing/MarkdownProcessor.cs index 63333b88..34c5c44d 100644 --- a/src/MarkdownSnippets/Processing/MarkdownProcessor.cs +++ b/src/MarkdownSnippets/Processing/MarkdownProcessor.cs @@ -190,11 +190,11 @@ void AppendSnippet(string key1) line.Current = builder.ToString(); } - void AppendWebSnippet(string url, string snippetKey) + void AppendWebSnippet(string url, string snippetKey, string? viewUrl = null) { builder.Clear(); var indentedAppendLine = CreateIndentedAppendLine(line.LeadingWhitespace); - ProcessWebSnippetLine(indentedAppendLine, missingSnippets, usedSnippets, url, snippetKey, line); + ProcessWebSnippetLine(indentedAppendLine, missingSnippets, usedSnippets, url, snippetKey, viewUrl, line); builder.TrimEnd(); line.Current = builder.ToString(); } @@ -205,9 +205,9 @@ void AppendWebSnippet(string url, string snippetKey) continue; } - if (SnippetKey.ExtractWebSnippet(line, out var url, out var snippetKey)) + if (SnippetKey.ExtractWebSnippet(line, out var url, out var snippetKey, out var viewUrl)) { - AppendWebSnippet(url, snippetKey); + AppendWebSnippet(url, snippetKey, viewUrl); continue; } @@ -230,9 +230,9 @@ void AppendWebSnippet(string url, string snippetKey) continue; } - if (SnippetKey.ExtractStartCommentWebSnippet(line, out url, out snippetKey)) + if (SnippetKey.ExtractStartCommentWebSnippet(line, out url, out snippetKey, out viewUrl)) { - AppendWebSnippet(url, snippetKey); + AppendWebSnippet(url, snippetKey, viewUrl); index++; @@ -312,9 +312,12 @@ void ProcessSnippetLine(Action appendLine, List missings appendLine(""); } - void ProcessWebSnippetLine(Action appendLine, List missings, List used, string url, string snippetKey, Line line) + void ProcessWebSnippetLine(Action appendLine, List missings, List used, string url, string snippetKey, string? viewUrl, Line line) { - appendLine($""); + var commentText = viewUrl == null + ? $"" + : $""; + appendLine(commentText); // Download file content try { @@ -343,9 +346,21 @@ void ProcessWebSnippetLine(Action appendLine, List missi appendLine(""); return; } - appendSnippets(snippetKey, [found], appendLine); + // Create new snippet with viewUrl if provided + var snippetToAppend = viewUrl == null + ? found + : Snippet.Build( + language: found.Language, + startLine: found.StartLine, + endLine: found.EndLine, + value: found.Value, + key: found.Key, + path: found.Path, + expressiveCode: found.ExpressiveCode, + viewUrl: viewUrl); + appendSnippets(snippetKey, [snippetToAppend], appendLine); appendLine(""); - used.Add(found); + used.Add(snippetToAppend); } catch { diff --git a/src/MarkdownSnippets/Processing/SnippetKey.cs b/src/MarkdownSnippets/Processing/SnippetKey.cs index b48b7795..252b4ae1 100644 --- a/src/MarkdownSnippets/Processing/SnippetKey.cs +++ b/src/MarkdownSnippets/Processing/SnippetKey.cs @@ -16,13 +16,17 @@ public static bool ExtractStartCommentSnippet(Line line, [NotNullWhen(true)] out return true; } - public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey) + public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey) => + ExtractStartCommentWebSnippet(line, out url, out snippetKey, out _); + + public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey, out string? viewUrl) { var lineCurrent = line.Current.TrimStart(); if (!IsStartCommentWebSnippetLine(lineCurrent)) { url = null; snippetKey = null; + viewUrl = null; return false; } @@ -30,15 +34,30 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] var indexOf = substring.IndexOf("-->"); var value = substring[..indexOf].Trim(); - var hashIndex = value.LastIndexOf('#'); - if (hashIndex < 0 || hashIndex == value.Length - 1) + // Check for optional second URL separated by whitespace + var parts = value.Split([' ', '\t'], StringSplitOptions.RemoveEmptyEntries); + string firstPart; + if (parts.Length >= 2) + { + firstPart = parts[0]; + viewUrl = parts[1]; + } + else + { + firstPart = value; + viewUrl = null; + } + + var hashIndex = firstPart.LastIndexOf('#'); + if (hashIndex < 0 || hashIndex == firstPart.Length - 1) { url = null; snippetKey = null; + viewUrl = null; return false; } - url = value[..hashIndex]; - snippetKey = value[(hashIndex + 1)..]; + url = firstPart[..hashIndex]; + snippetKey = firstPart[(hashIndex + 1)..]; return true; } @@ -61,25 +80,45 @@ public static bool ExtractSnippet(Line line, [NotNullWhen(true)] out string? key return true; } - public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey) + public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey) => + ExtractWebSnippet(line, out url, out snippetKey, out _); + + public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey, out string? viewUrl) { var lineCurrent = line.Current.TrimStart(); if (!IsWebSnippetLine(lineCurrent)) { url = null; snippetKey = null; + viewUrl = null; return false; } - var value = lineCurrent[12..].Trim(); // after 'web-snippet:', fixed from 11 to 12 - var hashIndex = value.LastIndexOf('#'); - if (hashIndex < 0 || hashIndex == value.Length - 1) + var value = lineCurrent[12..].Trim(); // after 'web-snippet:' + + // Check for optional second URL separated by whitespace + var parts = value.Split([' ', '\t'], StringSplitOptions.RemoveEmptyEntries); + string firstPart; + if (parts.Length >= 2) + { + firstPart = parts[0]; + viewUrl = parts[1]; + } + else + { + firstPart = value; + viewUrl = null; + } + + var hashIndex = firstPart.LastIndexOf('#'); + if (hashIndex < 0 || hashIndex == firstPart.Length - 1) { url = null; snippetKey = null; + viewUrl = null; return false; } - url = value[..hashIndex]; - snippetKey = value[(hashIndex + 1)..]; + url = firstPart[..hashIndex]; + snippetKey = firstPart[(hashIndex + 1)..]; return true; } diff --git a/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs b/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs index ab4eab6d..fc00134b 100644 --- a/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs +++ b/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs @@ -83,7 +83,11 @@ string GetSupText(Snippet snippet, string anchor) var path = pathLocal.Replace('\\', '/'); if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { - // For web-snippets: link back to the original URL with the snippet key as a hash + // For web-snippets: use ViewUrl if provided, otherwise link back to the original URL with the snippet key as a hash + if (snippet.ViewUrl != null) + { + return $"anchor"; + } return $"anchor"; } if (!path.StartsWith(targetDirectory)) diff --git a/src/MarkdownSnippets/Reading/Snippet.cs b/src/MarkdownSnippets/Reading/Snippet.cs index 4ff10834..ff136eb0 100644 --- a/src/MarkdownSnippets/Reading/Snippet.cs +++ b/src/MarkdownSnippets/Reading/Snippet.cs @@ -48,7 +48,39 @@ public static Snippet Build(int startLine, int endLine, string value, string key language = language, Path = path, ExpressiveCode = expressiveCode, - Error = "" + Error = "", + ViewUrl = null + }; + } + + /// + /// Initialise a new instance of with an optional view URL. + /// + public static Snippet Build(int startLine, int endLine, string value, string key, string language, string? path, string? expressiveCode, string? viewUrl) + { + Guard.AgainstNullAndEmpty(key, nameof(key)); + Guard.AgainstEmpty(path, nameof(path)); + Guard.AgainstEmpty(expressiveCode, nameof(expressiveCode)); + Guard.AgainstEmpty(viewUrl, nameof(viewUrl)); + Guard.AgainstUpperCase(language, nameof(language)); + if (language.StartsWith('.')) + { + throw new ArgumentException("Language cannot start with '.'", nameof(language)); + } + + Guard.AgainstNegativeAndZero(startLine, nameof(startLine)); + Guard.AgainstNegativeAndZero(endLine, nameof(endLine)); + return new() + { + StartLine = startLine, + EndLine = endLine, + value = value, + Key = key, + language = language, + Path = path, + ExpressiveCode = expressiveCode, + Error = "", + ViewUrl = viewUrl }; } @@ -67,6 +99,11 @@ public static Snippet Build(int startLine, int endLine, string value, string key /// public string? ExpressiveCode { get; private init; } + /// + /// Optional URL to use for viewing the snippet source (for web-snippets). + /// + public string? ViewUrl { get; private init; } + /// /// The language of the snippet, extracted from the file extension of the input file. /// diff --git a/src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithCommentWebSnippetWithViewUrl.verified.txt b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithCommentWebSnippetWithViewUrl.verified.txt new file mode 100644 index 00000000..a533630c --- /dev/null +++ b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithCommentWebSnippetWithViewUrl.verified.txt @@ -0,0 +1,22 @@ +{ + UsedSnippets: [ + { + Key: snipPet, + Language: txt, + Value: Some code, + Error: , + FileLocation: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt(1-3), + IsInError: false + } + ], + result: +before + + +```txt +Some code +``` + + +after +} diff --git a/src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithInlineWebSnippetWithViewUrl.verified.txt b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithInlineWebSnippetWithViewUrl.verified.txt new file mode 100644 index 00000000..a533630c --- /dev/null +++ b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.WithInlineWebSnippetWithViewUrl.verified.txt @@ -0,0 +1,22 @@ +{ + UsedSnippets: [ + { + Key: snipPet, + Language: txt, + Value: Some code, + Error: , + FileLocation: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt(1-3), + IsInError: false + } + ], + result: +before + + +```txt +Some code +``` + + +after +} diff --git a/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs index 9f03156b..07c6f276 100644 --- a/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs +++ b/src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs @@ -814,6 +814,46 @@ THAT SHOULD BE content); } + [Fact] + public Task WithCommentWebSnippetWithViewUrl() + { + var content = """ + + before + + + OLD CONTENT + THAT SHOULD BE + REPLACED + + + after + + """; + + return SnippetVerifier.Verify( + DocumentConvention.InPlaceOverwrite, + content); + } + + [Fact] + public Task WithInlineWebSnippetWithViewUrl() + { + var content = """ + + before + + web-snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet https://github.com/SimonCropp/MarkdownSnippets/blob/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt + + after + + """; + + return SnippetVerifier.Verify( + DocumentConvention.SourceTransform, + content); + } + static Snippet SnippetBuild(string language, string key) => Snippet.Build( language: language, diff --git a/src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippetWithViewUrl.verified.txt b/src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippetWithViewUrl.verified.txt new file mode 100644 index 00000000..bfb85779 --- /dev/null +++ b/src/Tests/SnippetMarkdownHandlingTests.AppendWebSnippetWithViewUrl.verified.txt @@ -0,0 +1,5 @@ + +```cs +theValue +``` +anchor diff --git a/src/Tests/SnippetMarkdownHandlingTests.cs b/src/Tests/SnippetMarkdownHandlingTests.cs index 4ea29b8e..259015c7 100644 --- a/src/Tests/SnippetMarkdownHandlingTests.cs +++ b/src/Tests/SnippetMarkdownHandlingTests.cs @@ -91,6 +91,28 @@ public Task AppendWebSnippet() return Verify(builder.ToString()); } + [Fact] + public Task AppendWebSnippetWithViewUrl() + { + var builder = new StringBuilder(); + var webSnippet = Snippet.Build( + startLine: 5, + endLine: 10, + value: "theValue", + key: "mysnippet", + language: "cs", + path: "http://example.com/raw/file.cs", + expressiveCode: null, + viewUrl: "https://github.com/user/repo/blob/main/file.cs"); + var markdownHandling = new SnippetMarkdownHandling(Environment.CurrentDirectory, LinkFormat.GitHub, false); + using (var writer = new StringWriter(builder)) + { + markdownHandling.Append("key1", new List { webSnippet }, writer.WriteLine); + } + + return Verify(builder.ToString()); + } + static List Snippets() => [Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", Environment.CurrentDirectory, expressiveCode: null)]; } \ No newline at end of file