Skip to content

Commit ddda1ce

Browse files
authored
Fix CurrentUrlPath being empty on deeply nested snippets (#887)
* Fix CurrentUrlPath being empty on deeply nested snippets * fix tests
1 parent 8ee477f commit ddda1ce

File tree

8 files changed

+30
-14
lines changed

8 files changed

+30
-14
lines changed

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,10 @@ void ValidateExists(string from, string to, IReadOnlyDictionary<string, string?>
213213

214214
public FrozenDictionary<int, MarkdownFile> MarkdownFiles { get; }
215215

216-
public MarkdownFile? DocumentationFileLookup(IFileInfo sourceFile)
216+
public DocumentationFile? DocumentationFileLookup(IFileInfo sourceFile)
217217
{
218218
var relativePath = Path.GetRelativePath(SourceDirectory.FullName, sourceFile.FullName);
219-
if (FlatMappedFiles.TryGetValue(relativePath, out var file) && file is MarkdownFile markdownFile)
220-
return markdownFile;
221-
return null;
219+
return FlatMappedFiles.GetValueOrDefault(relativePath);
222220
}
223221

224222
public MarkdownFile? GetPrevious(MarkdownFile current)

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ DocumentationSet set
4141
{
4242
FileName = sourceFile.Name;
4343
FilePath = sourceFile.FullName;
44+
IsIndex = FileName == "index.md";
45+
4446
UrlPathPrefix = build.UrlPathPrefix;
4547
MarkdownParser = parser;
4648
Collector = build.Collector;
@@ -76,6 +78,8 @@ public DocumentationGroup? Parent
7678
public YamlFrontMatter? YamlFrontMatter { get; private set; }
7779
public string? TitleRaw { get; protected set; }
7880

81+
public bool IsIndex { get; internal set; }
82+
7983
public string? Title
8084
{
8185
get => _title;

src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ .. documentationFiles
315315
}
316316
}
317317

318+
if (indexFile is not null)
319+
indexFile.IsIndex = true;
320+
318321
return indexFile ?? files.FirstOrDefault();
319322
}
320323

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,10 @@ private static void WriteIncludeBlock(HtmlRenderer renderer, IncludeBlock block,
227227
return;
228228

229229
var snippet = block.Build.ReadFileSystem.FileInfo.New(block.IncludePath);
230-
var parentPath = block.Context.MarkdownSourcePath;
230+
231+
var parentPath = block.Context.MarkdownParentPath ?? block.Context.MarkdownSourcePath;
231232
var document = parser.ParseSnippetAsync(snippet, parentPath, block.Context.YamlFrontMatter, default).GetAwaiter().GetResult();
233+
232234
var html = document.ToHtml(parser.Pipeline);
233235
_ = renderer.Write(html);
234236
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,14 @@ private static IFileInfo ResolveFile(ParserContext context, string url) =>
289289
private static void ValidateAnchor(InlineProcessor processor, MarkdownFile markdown, string anchor, LinkInline link)
290290
{
291291
if (!markdown.Anchors.Contains(anchor))
292-
processor.EmitError(link, $"`{anchor}` does not exist in {markdown.FileName}.");
292+
processor.EmitError(link, $"`{anchor}` does not exist in {markdown.RelativePath}.");
293293
}
294294

295295
private static void UpdateLinkUrl(LinkInline link, string url, ParserContext context, string? anchor)
296296
{
297+
// TODO revisit when we refactor our documentation set graph
298+
// This method grew too complex, we need to revisit our documentation set graph generation so we can ask these questions
299+
// on `DocumentationFile` that are mostly precomputed
297300
var urlPathPrefix = context.Build.UrlPathPrefix ?? string.Empty;
298301

299302
if (!url.StartsWith('/') && !string.IsNullOrEmpty(url))
@@ -303,16 +306,19 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
303306
? context.CurrentUrlPath[urlPathPrefix.Length..]
304307
: urlPathPrefix;
305308

306-
var markdownPath = context.MarkdownSourcePath.Name;
309+
// if we are trying to resolve a relative url from a _snippet folder ensure we eat the _snippet folder
310+
// as it's not part of url by chopping of the extra parent navigation
311+
if (url.StartsWith("../") && context.DocumentationFileLookup(context.MarkdownSourcePath) is SnippetFile snippetFile)
312+
url = url.Substring(3);
307313

314+
// TODO check through context.DocumentationFileLookup if file is index vs "index.md" check
315+
var markdownPath = context.MarkdownSourcePath;
308316
// if the current path is an index e.g /reference/cloud-k8s/
309317
// './' current path lookups should be relative to sub-path.
310318
// If it's not e.g /reference/cloud-k8s/api-docs/ these links should resolve on folder up.
311-
var siblingsGoToCurrent = url.StartsWith("./") && markdownPath == "index.md";
312319
var lastIndexPath = subPrefix.LastIndexOf('/');
313-
if (lastIndexPath >= 0 && !siblingsGoToCurrent)
320+
if (lastIndexPath >= 0 && markdownPath.Name != "index.md")
314321
subPrefix = subPrefix[..lastIndexPath];
315-
316322
var combined = '/' + Path.Combine(subPrefix, url).TrimStart('/');
317323
url = Path.GetFullPath(combined);
318324
}
@@ -341,6 +347,7 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
341347
url = url[..^5];
342348

343349
link.Url = string.IsNullOrEmpty(anchor) ? url : $"{url}#{anchor}";
350+
344351
}
345352

346353
private static bool IsCrossLink([NotNullWhen(true)] Uri? uri) =>

src/Elastic.Markdown/Myst/ParserContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class ParserContext : MarkdownParserContext, IParserResolvers
5353
public ConfigurationFile Configuration { get; }
5454
public ICrossLinkResolver CrossLinkResolver { get; }
5555
public IFileInfo MarkdownSourcePath { get; }
56+
public IFileInfo? MarkdownParentPath { get; }
5657
public string CurrentUrlPath { get; }
5758
public YamlFrontMatter? YamlFrontMatter { get; }
5859
public BuildContext Build { get; }
@@ -67,15 +68,16 @@ public ParserContext(ParserState state)
6768
Configuration = state.Configuration;
6869
YamlFrontMatter = state.YamlFrontMatter;
6970
SkipValidation = state.SkipValidation;
71+
MarkdownParentPath = state.ParentMarkdownPath;
7072

7173
CrossLinkResolver = state.CrossLinkResolver;
7274
MarkdownSourcePath = state.MarkdownSourcePath;
7375
DocumentationFileLookup = state.DocumentationFileLookup;
74-
var parentPath = state.ParentMarkdownPath;
7576

76-
CurrentUrlPath = DocumentationFileLookup(parentPath ?? MarkdownSourcePath) is MarkdownFile md
77+
CurrentUrlPath = DocumentationFileLookup(state.ParentMarkdownPath ?? MarkdownSourcePath) is MarkdownFile md
7778
? md.Url
7879
: string.Empty;
80+
7981
if (SkipValidation && string.IsNullOrEmpty(CurrentUrlPath))
8082
{
8183
//TODO investigate this deeper.

tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected DirectiveTest(ITestOutputHelper output, [LanguageInjection("markdown")
7070
var context = new BuildContext(Collector, FileSystem);
7171
var linkResolver = new TestCrossLinkResolver();
7272
Set = new DocumentationSet(context, logger, linkResolver);
73-
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) ?? throw new NullReferenceException();
73+
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) as MarkdownFile ?? throw new NullReferenceException();
7474
Html = default!; //assigned later
7575
Document = default!;
7676
}

tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected InlineTest(
117117
};
118118
var linkResolver = new TestCrossLinkResolver();
119119
Set = new DocumentationSet(context, logger, linkResolver);
120-
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) ?? throw new NullReferenceException();
120+
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) as MarkdownFile ?? throw new NullReferenceException();
121121
Html = default!; //assigned later
122122
Document = default!;
123123
}

0 commit comments

Comments
 (0)