Skip to content

Commit 570690e

Browse files
committed
Fix current directory anchored paths './'
1 parent 63925ce commit 570690e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,24 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
280280
var urlPathPrefix = context.Build.UrlPathPrefix ?? string.Empty;
281281

282282
if (!url.StartsWith('/') && !string.IsNullOrEmpty(url))
283-
url = context.CurrentUrlPath[urlPathPrefix.Length..].TrimEnd('/') + '/' + url;
283+
{
284+
// eat overall path prefix since its gets appended later
285+
var subPrefix = context.CurrentUrlPath.Length >= urlPathPrefix.Length
286+
? context.CurrentUrlPath[urlPathPrefix.Length..]
287+
: urlPathPrefix;
288+
289+
var markdownPath = context.MarkdownSourcePath.Name;
290+
291+
// if the current path is an index e.g /reference/cloud-k8s/
292+
// './' current path lookups should be relative to sub-path.
293+
// If it's not e.g /reference/cloud-k8s/api-docs/ these links should resolve on folder up.
294+
var siblingsGoToCurrent = url.StartsWith("./") && markdownPath == "index.md";
295+
if (!siblingsGoToCurrent)
296+
subPrefix = subPrefix[..subPrefix.LastIndexOf('/')];
297+
298+
var combined = '/' + Path.Combine(subPrefix, url).TrimStart('/');
299+
url = Path.GetFullPath(combined);
300+
}
284301

285302
if (url.EndsWith(".md"))
286303
{

tests/authoring/Inline/RelativeLinks.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
module ``inline elemenents``.``relative links``
5+
module ``inline elements``.``relative links``
66

77
open Xunit
88
open authoring
@@ -29,7 +29,12 @@ Through various means $$$including-this-inline-syntax$$$
2929
[link to parent](../parent.md)
3030
3131
[link to parent](../parent.md#some-header)
32+
33+
[link to sibling](./file2.md)
3234
"""
35+
Markdown "deeply/nested/file2.md" """
36+
# file2.md
37+
"""
3338
Markdown "deeply/parent.md" """
3439
# parent.md
3540
@@ -44,6 +49,7 @@ Through various means $$$including-this-inline-syntax$$$
4449
<p><a href="/#and-anchored">link to root</a></p>
4550
<p><a href="/deeply/parent">link to parent</a></p>
4651
<p><a href="/deeply/parent#some-header">link to parent</a></p>
52+
<p><a href="/deeply/nested/file2">link to sibling</a></p>
4753
"""
4854

4955
[<Fact>]

0 commit comments

Comments
 (0)