Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,29 @@ public static string UpdateRelativeUrl(ParserContext context, string url)
newUrl = newUrl[3..];
offset--;
}
newUrl = Path.GetFullPath(Path.Combine(urlPathPrefix, snippet.RelativeFolder, url))
.OptionalWindowsReplace().TrimStart('/');
}
else
newUrl = $"/{Path.Combine(urlPathPrefix, relativePath).OptionalWindowsReplace().TrimStart('/')}";
}

if (context.Build.AssemblerBuild && context.TryFindDocument(fi) is MarkdownFile currentMarkdown)
{
// Acquire navigation-aware path
if (context.PositionalNavigation.MarkdownNavigationLookup.TryGetValue(currentMarkdown, out var currentNavigation) && !string.IsNullOrEmpty(currentNavigation.Url))
{
var currentUrl = currentNavigation.Url;
if (currentUrl.LastIndexOf('/') > 0)
{
var basePath = currentUrl[..currentUrl.LastIndexOf('/')];
newUrl = Path.GetFullPath(Path.Combine(basePath, url));
}
}
newUrl = $"/{Path.Combine(newUrl.StartsWith(urlPathPrefix) ? string.Empty : urlPathPrefix, newUrl.TrimStart('/'))
.OptionalWindowsReplace().TrimStart('/')}";
}

// When running on Windows, path traversal results must be normalized prior to being used in a URL
// Path.GetFullPath() will result in the drive letter being appended to the path, which needs to be pruned back.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down
Loading