Skip to content

Commit be20456

Browse files
authored
Fix assuming _snippets files only live one folder deep when resolving relative image links inside of them (#1242)
1 parent 5cdbdaf commit be20456

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Elastic.Markdown/Myst/Directives/ImageBlock.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ private void ExtractImageUrl(ParserContext context)
9999

100100
ImageUrl = DiagnosticLinkInlineParser.UpdateRelativeUrl(context, imageUrl);
101101

102-
103102
var file = DiagnosticLinkInlineParser.ResolveFile(context, imageUrl);
104103
if (file.Exists)
105104
Found = true;

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,25 @@ public static string UpdateRelativeUrl(ParserContext context, string url)
338338

339339
// if we are trying to resolve a relative url from a _snippet folder ensure we eat the _snippet folder
340340
// as it's not part of url by chopping of the extra parent navigation
341-
if (newUrl.StartsWith("../") && context.DocumentationFileLookup(context.MarkdownSourcePath) is SnippetFile)
342-
newUrl = url[3..];
341+
if (newUrl.StartsWith("../") && context.DocumentationFileLookup(context.MarkdownSourcePath) is SnippetFile snippet)
342+
{
343+
//figure out how many nested folders inside `_snippets` we need to ignore.
344+
var d = snippet.SourceFile.Directory;
345+
var offset = 0;
346+
while (d is not null && d.Name != "_snippets")
347+
{
348+
d = d.Parent;
349+
if (d is not null && d.Name != "_snippets")
350+
offset++;
351+
}
352+
353+
//Because we ignore these folders in the url structure we need to eat the relative paths
354+
while (offset >= 0 && newUrl.StartsWith("../"))
355+
{
356+
newUrl = newUrl[3..];
357+
offset--;
358+
}
359+
}
343360

344361
// TODO check through context.DocumentationFileLookup if file is index vs "index.md" check
345362
var markdownPath = context.MarkdownSourcePath;

0 commit comments

Comments
 (0)