Skip to content

Commit 1cd37b1

Browse files
committed
fix authoring tests
1 parent df73aac commit 1cd37b1

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

src/Elastic.Documentation/Extensions/IFileInfoExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ public static bool IsSubPathOf(this IFileInfo file, IDirectoryInfo parentDirecto
2828
return parent is not null && parent.IsSubPathOf(parentDirectory);
2929
}
3030

31+
public static IFileInfo EnsureSubPathOf(this IFileInfo file, IDirectoryInfo parentDirectory, string relativePath)
32+
{
33+
var fs = file.FileSystem;
34+
var path = Path.GetFullPath(fs.Path.Combine(parentDirectory.FullName, relativePath));
35+
var pathInfo = fs.FileInfo.New(path);
36+
List<string> intermediaryDirectories = ["x"];
37+
while (!pathInfo.IsSubPathOf(parentDirectory))
38+
{
39+
path = Path.GetFullPath(fs.Path.Combine([parentDirectory.FullName, .. intermediaryDirectories, relativePath]));
40+
pathInfo = fs.FileInfo.New(path);
41+
intermediaryDirectories.Add("x");
42+
}
43+
44+
return pathInfo;
45+
}
46+
3147
/// Checks if <paramref name="file"/> has parent directory <paramref name="parentName"/>, defaults to OrdinalIgnoreCase comparison
3248
public static bool HasParent(this IFileInfo file, string parentName)
3349
{

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,18 @@ public RepositoryLinks CreateLinkReference()
272272
{
273273
var redirects = Configuration.Redirects;
274274
var crossLinks = Context.Collector.CrossLinks.ToHashSet().ToArray();
275-
var markdownInNavigation = NavigationIndexedByOrder.Values
276-
.OfType<ILeafNavigationItem<MarkdownFile>>()
275+
276+
var leafs = NavigationIndexedByOrder.Values
277+
.OfType<ILeafNavigationItem<MarkdownFile>>().ToArray();
278+
var nodes = NavigationIndexedByOrder.Values
279+
.OfType<INodeNavigationItem<INavigationModel, INavigationItem>>()
280+
.ToArray();
281+
282+
var markdownInNavigation =
283+
leafs
277284
.Select(m => (Markdown: m.Model, Navigation: (INavigationItem)m))
278-
.Concat(NavigationIndexedByOrder.Values
279-
.OfType<INodeNavigationItem<MarkdownFile, INavigationItem>>()
280-
.Select(g => (Markdown: g.Index.Model, Navigation: (INavigationItem)g))
285+
.Concat(nodes
286+
.Select(g => (Markdown: (MarkdownFile)g.Index.Model, Navigation: (INavigationItem)g))
281287
)
282288
.ToList();
283289

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO.Abstractions;
77
using System.Runtime.InteropServices;
88
using System.Text.RegularExpressions;
9+
using Elastic.Documentation.Extensions;
910
using Elastic.Documentation.Links;
1011
using Elastic.Markdown.Diagnostics;
1112
using Elastic.Markdown.Helpers;
@@ -359,12 +360,16 @@ private static void UpdateLinkUrl(LinkInline link, MarkdownFile? linkMarkdown, s
359360
public static string UpdateRelativeUrl(ParserContext context, string url)
360361
{
361362
var urlPathPrefix = context.Build.UrlPathPrefix ?? string.Empty;
363+
364+
var fi = context.MarkdownSourcePath;
365+
362366
var newUrl = url;
363367
if (!newUrl.StartsWith('/') && !string.IsNullOrEmpty(newUrl))
364368
{
365-
var subPrefix = context.CurrentUrlPath.Length >= urlPathPrefix.Length
366-
? context.CurrentUrlPath[urlPathPrefix.Length..]
367-
: urlPathPrefix;
369+
var path = Path.GetFullPath(fi.FileSystem.Path.Combine(fi.Directory!.FullName, newUrl));
370+
var pathInfo = fi.FileSystem.FileInfo.New(path);
371+
pathInfo = pathInfo.EnsureSubPathOf(context.Configuration.ScopeDirectory, newUrl);
372+
var relativePath = fi.FileSystem.Path.GetRelativePath(context.Configuration.ScopeDirectory.FullName, pathInfo.FullName);
368373

369374
// if we are trying to resolve a relative url from a _snippet folder ensure we eat the _snippet folder
370375
// as it's not part of url by chopping of the extra parent navigation
@@ -387,17 +392,8 @@ public static string UpdateRelativeUrl(ParserContext context, string url)
387392
offset--;
388393
}
389394
}
390-
391-
// TODO check through context.DocumentationFileLookup if file is index vs "index.md" check
392-
var markdownPath = context.MarkdownSourcePath;
393-
// if the current path is an index e.g /reference/cloud-k8s/
394-
// './' current path lookups should be relative to sub-path.
395-
// If it's not e.g /reference/cloud-k8s/api-docs/ these links should resolve on folder up.
396-
var lastIndexPath = subPrefix.LastIndexOf('/');
397-
if (lastIndexPath >= 0 && markdownPath.Name != "index.md")
398-
subPrefix = subPrefix[..lastIndexPath];
399-
var combined = '/' + Path.Combine(subPrefix, newUrl).TrimStart('/');
400-
newUrl = Path.GetFullPath(combined);
395+
else
396+
newUrl = $"/{Path.Combine(urlPathPrefix, relativePath).TrimStart('/')}";
401397

402398
}
403399
// When running on Windows, path traversal results must be normalized prior to being used in a URL

tests/authoring/Generator/LinkReferenceFile.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Through various means $$$including-this-inline-syntax$$$
6363
},
6464
"url_path_prefix": "",
6565
"links": {
66-
"file.md": {},
6766
"index.md": {
6867
"anchors": [
6968
"including-this-inline-syntax",
@@ -82,7 +81,8 @@ Through various means $$$including-this-inline-syntax$$$
8281
},
8382
"testing/redirects/first-page.md": {
8483
"anchors": [ "has-an-anchor-as-well" ]
85-
}
84+
},
85+
"file.md": {}
8686
},
8787
"cross_links": [],
8888
"redirects": {

tests/authoring/Inline/RelativeLinks.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ Through various means $$$including-this-inline-syntax$$$
4747
let ``validate index.md HTML`` () =
4848
generator |> converts "deeply/nested/file.md" |> toHtml """
4949
<p><a href="/#and-anchored">link to root</a></p>
50-
<p><a href="/deeply/parent">link to parent</a></p>
51-
<p><a href="/deeply/parent#some-header">link to parent</a></p>
52-
<p><a href="/deeply/nested/file2">link to sibling</a></p>
50+
<p><a href="/deeply/parent/">link to parent</a></p>
51+
<p><a href="/deeply/parent/#some-header">link to parent</a></p>
52+
<p><a href="/deeply/nested/file2/">link to sibling</a></p>
5353
"""
5454

5555
[<Fact>]

0 commit comments

Comments
 (0)