From 29e1a988c4d1d4517d9b94ee29bea4410ed7383f Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 9 May 2025 17:29:11 +0200 Subject: [PATCH] Fix assuming _snippets files only live one folder deep when resolving relative image links inside of them --- .../Myst/Directives/ImageBlock.cs | 1 - .../DiagnosticLinkInlineParser.cs | 21 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/ImageBlock.cs b/src/Elastic.Markdown/Myst/Directives/ImageBlock.cs index 448d7fab7..4c454ab0f 100644 --- a/src/Elastic.Markdown/Myst/Directives/ImageBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/ImageBlock.cs @@ -99,7 +99,6 @@ private void ExtractImageUrl(ParserContext context) ImageUrl = DiagnosticLinkInlineParser.UpdateRelativeUrl(context, imageUrl); - var file = DiagnosticLinkInlineParser.ResolveFile(context, imageUrl); if (file.Exists) Found = true; diff --git a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs index f236ad4ed..4cc09e85a 100644 --- a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs +++ b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs @@ -338,8 +338,25 @@ public static string UpdateRelativeUrl(ParserContext context, string url) // if we are trying to resolve a relative url from a _snippet folder ensure we eat the _snippet folder // as it's not part of url by chopping of the extra parent navigation - if (newUrl.StartsWith("../") && context.DocumentationFileLookup(context.MarkdownSourcePath) is SnippetFile) - newUrl = url[3..]; + if (newUrl.StartsWith("../") && context.DocumentationFileLookup(context.MarkdownSourcePath) is SnippetFile snippet) + { + //figure out how many nested folders inside `_snippets` we need to ignore. + var d = snippet.SourceFile.Directory; + var offset = 0; + while (d is not null && d.Name != "_snippets") + { + d = d.Parent; + if (d is not null && d.Name != "_snippets") + offset++; + } + + //Because we ignore these folders in the url structure we need to eat the relative paths + while (offset >= 0 && newUrl.StartsWith("../")) + { + newUrl = newUrl[3..]; + offset--; + } + } // TODO check through context.DocumentationFileLookup if file is index vs "index.md" check var markdownPath = context.MarkdownSourcePath;