Skip to content

Commit d96db86

Browse files
committed
Fix round-tripping of web-snippet
- Ensure that content of web-snippet is refreshed
1 parent a16fa61 commit d96db86

File tree

4 files changed

+117
-18
lines changed

4 files changed

+117
-18
lines changed

src/MarkdownSnippets/Processing/MarkdownProcessor.cs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ void AppendWebSnippet(string url, string snippetKey)
230230
continue;
231231
}
232232

233+
if (SnippetKey.ExtractStartCommentWebSnippet(line, out url, out snippetKey))
234+
{
235+
AppendWebSnippet(url, snippetKey);
236+
237+
index++;
238+
239+
lines.RemoveUntil(
240+
index,
241+
"<!-- endSnippet -->",
242+
relativePath,
243+
line);
244+
continue;
245+
}
246+
233247
if (line.Current.TrimStart() == "<!-- toc -->")
234248
{
235249
tocLine = line;
@@ -302,34 +316,46 @@ void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missi
302316
{
303317
appendLine($"<!-- web-snippet: {url}#{snippetKey} -->");
304318
// Download file content
305-
var (success, content) = Downloader.DownloadContent(url).GetAwaiter().GetResult();
306-
if (!success || string.IsNullOrWhiteSpace(content))
319+
try
307320
{
308-
var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path);
309-
missings.Add(missing);
310-
appendLine("```");
311-
appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **");
312-
appendLine("```");
321+
var (success, content) = Downloader.DownloadContent(url).GetAwaiter().GetResult();
322+
if (!success || string.IsNullOrWhiteSpace(content))
323+
{
324+
var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path);
325+
missings.Add(missing);
326+
appendLine("```");
327+
appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **");
328+
appendLine("```");
329+
appendLine("<!-- endSnippet -->");
330+
return;
331+
}
332+
// Extract snippets from content
333+
using var reader = new StringReader(content);
334+
var snippets = FileSnippetExtractor.Read(reader, url);
335+
var found = snippets.FirstOrDefault(_ => _.Key == snippetKey);
336+
if (found == null)
337+
{
338+
var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path);
339+
missings.Add(missing);
340+
appendLine("```");
341+
appendLine($"** Could not find snippet '{snippetKey}' in '{url}' **");
342+
appendLine("```");
343+
appendLine("<!-- endSnippet -->");
344+
return;
345+
}
346+
appendSnippets(snippetKey, [found], appendLine);
313347
appendLine("<!-- endSnippet -->");
314-
return;
348+
used.Add(found);
315349
}
316-
// Extract snippets from content
317-
using var reader = new StringReader(content);
318-
var snippets = FileSnippetExtractor.Read(reader, url);
319-
var found = snippets.FirstOrDefault(_ => _.Key == snippetKey);
320-
if (found == null)
350+
catch
321351
{
322352
var missing = new MissingSnippet($"{url}#{snippetKey}", line.LineNumber, line.Path);
323353
missings.Add(missing);
324354
appendLine("```");
325-
appendLine($"** Could not find snippet '{snippetKey}' in '{url}' **");
355+
appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **");
326356
appendLine("```");
327357
appendLine("<!-- endSnippet -->");
328-
return;
329358
}
330-
appendSnippets(snippetKey, [found], appendLine);
331-
appendLine("<!-- endSnippet -->");
332-
used.Add(found);
333359
}
334360

335361
bool TryGetSnippets(

src/MarkdownSnippets/Processing/SnippetKey.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@ public static bool ExtractStartCommentSnippet(Line line, [NotNullWhen(true)] out
1616
return true;
1717
}
1818

19+
public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey)
20+
{
21+
var lineCurrent = line.Current.TrimStart();
22+
if (!IsStartCommentWebSnippetLine(lineCurrent))
23+
{
24+
url = null;
25+
snippetKey = null;
26+
return false;
27+
}
28+
29+
var substring = lineCurrent[18..]; // after "<!-- web-snippet: "
30+
var indexOf = substring.IndexOf("-->");
31+
var value = substring[..indexOf].Trim();
32+
33+
var hashIndex = value.LastIndexOf('#');
34+
if (hashIndex < 0 || hashIndex == value.Length - 1)
35+
{
36+
url = null;
37+
snippetKey = null;
38+
return false;
39+
}
40+
url = value[..hashIndex];
41+
snippetKey = value[(hashIndex + 1)..];
42+
return true;
43+
}
44+
1945
public static bool ExtractSnippet(Line line, [NotNullWhen(true)] out string? key)
2046
{
2147
var lineCurrent = line.Current.TrimStart();
@@ -65,4 +91,7 @@ public static bool IsStartCommentSnippetLine(string line) =>
6591

6692
public static bool IsWebSnippetLine(string line) =>
6793
line.StartsWith("web-snippet:", StringComparison.OrdinalIgnoreCase);
94+
95+
public static bool IsStartCommentWebSnippetLine(string line) =>
96+
line.StartsWith("<!-- web-snippet:", StringComparison.OrdinalIgnoreCase);
6897
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
UsedSnippets: [
3+
{
4+
Key: snipPet,
5+
Language: txt,
6+
Value: Some code,
7+
Error: ,
8+
FileLocation: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt(1-3),
9+
IsInError: false
10+
}
11+
],
12+
result:
13+
before
14+
15+
<!-- web-snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet -->
16+
```txt
17+
Some code
18+
```
19+
<!-- endSnippet -->
20+
21+
after
22+
}

src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,28 @@ public Task WithIndentedMultiLineSnippet()
792792
]);
793793
}
794794

795+
[Fact]
796+
public Task WithCommentWebSnippetUpdate()
797+
{
798+
var content = """
799+
800+
before
801+
802+
<!-- web-snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet -->
803+
OLD CONTENT
804+
THAT SHOULD BE
805+
REPLACED
806+
<!-- endSnippet -->
807+
808+
after
809+
810+
""";
811+
812+
return SnippetVerifier.Verify(
813+
DocumentConvention.InPlaceOverwrite,
814+
content);
815+
}
816+
795817
static Snippet SnippetBuild(string language, string key) =>
796818
Snippet.Build(
797819
language: language,

0 commit comments

Comments
 (0)