Skip to content

Commit 0bae97d

Browse files
committed
Fix anchor links for web-snippets
1 parent d96db86 commit 0bae97d

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ Files are downloaded to `%temp%MarkdownSnippets` with a maximum of 100 files kep
219219
Will render:
220220

221221
<!-- web-snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet -->
222-
<a id='snippet-snipPet'></a>
222+
<a id='snippet-https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt%23snipPet'></a>
223223
```txt
224224
Some code
225225
```
226-
<sup><a href='#snippet-snipPet' title='Start of snippet'>anchor</a></sup>
226+
<sup><a href='https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/main/src/Tests/DirectorySnippetExtractor/Case/code1.txt#snipPet' title='Snippet source file'>anchor</a></sup>
227227
<!-- endSnippet -->
228228

229229
### Including a full file
@@ -347,7 +347,7 @@ switch (linkFormat)
347347
throw new($"Unknown LinkFormat: {linkFormat}");
348348
}
349349
```
350-
<sup><a href='/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs#L103-L124' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildLink' title='Start of snippet'>anchor</a></sup>
350+
<sup><a href='/src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs#L121-L142' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildLink' title='Start of snippet'>anchor</a></sup>
351351
<!-- endSnippet -->
352352

353353

src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ void WriteSnippet(Action<string> appendLine, Snippet snippet, uint index)
5050

5151
static string GetAnchorText(Snippet snippet, uint index)
5252
{
53-
var id = $"snippet-{snippet.Key}";
53+
string id;
54+
var pathStr = snippet.Path;
55+
if (pathStr != null && pathStr.StartsWith("http", StringComparison.OrdinalIgnoreCase))
56+
{
57+
// For web-snippets, include the full URL and the snippet key, with '#' encoded
58+
var combined = $"snippet-{pathStr}#{snippet.Key}";
59+
id = combined.Replace("#", "%23");
60+
}
61+
else
62+
{
63+
id = $"snippet-{snippet.Key}";
64+
}
65+
5466
if (index == 0)
5567
{
5668
return id;
@@ -62,12 +74,18 @@ static string GetAnchorText(Snippet snippet, uint index)
6274
string GetSupText(Snippet snippet, string anchor)
6375
{
6476
var linkForAnchor = $"<a href='#{anchor}' title='Start of snippet'>anchor</a>";
65-
if (snippet.Path == null || linkFormat == LinkFormat.None)
77+
var pathLocal = snippet.Path;
78+
if (pathLocal == null || linkFormat == LinkFormat.None)
6679
{
6780
return linkForAnchor;
6881
}
6982

70-
var path = snippet.Path.Replace('\\', '/');
83+
var path = pathLocal.Replace('\\', '/');
84+
if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
85+
{
86+
// For web-snippets: link back to the original URL with the snippet key as a hash
87+
return $"<a href='{path}#{snippet.Key}' title='Snippet source file'>anchor</a>";
88+
}
7189
if (!path.StartsWith(targetDirectory))
7290
{
7391
// if file is not in the targetDirectory then the url wont work
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<a id='snippet-http://example.com/file.cs%23mysnippet'></a>
2+
```cs
3+
theValue
4+
```
5+
<sup><a href='http://example.com/file.cs#mysnippet' title='Snippet source file'>anchor</a></sup>

src/Tests/SnippetMarkdownHandlingTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ public Task AppendHashed()
7070
return Verify(builder.ToString());
7171
}
7272

73+
[Fact]
74+
public Task AppendWebSnippet()
75+
{
76+
var builder = new StringBuilder();
77+
var webSnippet = Snippet.Build(
78+
startLine: 1,
79+
endLine: 2,
80+
value: "theValue",
81+
key: "mysnippet",
82+
language: "cs",
83+
path: "http://example.com/file.cs",
84+
expressiveCode: null);
85+
var markdownHandling = new SnippetMarkdownHandling(Environment.CurrentDirectory, LinkFormat.GitHub, false);
86+
using (var writer = new StringWriter(builder))
87+
{
88+
markdownHandling.Append("key1", new List<Snippet> { webSnippet }, writer.WriteLine);
89+
}
90+
91+
return Verify(builder.ToString());
92+
}
93+
7394
static List<Snippet> Snippets() =>
7495
[Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", Environment.CurrentDirectory, expressiveCode: null)];
7596
}

0 commit comments

Comments
 (0)