diff --git a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs index eafdd0c2..0900fdcf 100644 --- a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs +++ b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs @@ -168,10 +168,10 @@ private static void ProcessInternalLink(InlineProcessor processor, LinkInline li { var (url, anchor) = SplitUrlAndAnchor(link.Url ?? string.Empty); var includeFrom = GetIncludeFromPath(url, context); - + var file = ResolveFile(context, url); ValidateInternalUrl(processor, url, includeFrom, line, column, length, context); - ProcessLinkText(processor, link, context, url, anchor, line, column, length); - UpdateLinkUrl(link, url, anchor, context.Build.UrlPathPrefix ?? string.Empty); + ProcessLinkText(processor, link, context, url, anchor, line, column, length, file); + UpdateLinkUrl(link, url, context, anchor, file); } private static (string url, string? anchor) SplitUrlAndAnchor(string fullUrl) @@ -195,12 +195,11 @@ private static void ValidateInternalUrl(InlineProcessor processor, string url, s processor.EmitError(line, column, length, $"`{url}` does not exist. resolved to `{pathOnDisk}"); } - private static void ProcessLinkText(InlineProcessor processor, LinkInline link, ParserContext context, string url, string? anchor, int line, int column, int length) + private static void ProcessLinkText(InlineProcessor processor, LinkInline link, ParserContext context, string url, string? anchor, int line, int column, int length, IFileInfo file) { if (link.FirstChild != null && string.IsNullOrEmpty(anchor)) return; - var file = ResolveFile(context, url); var markdown = context.GetDocumentationFile?.Invoke(file) as MarkdownFile; if (markdown == null) @@ -236,15 +235,26 @@ private static void ValidateAnchor(InlineProcessor processor, MarkdownFile markd processor.EmitError(line, column, length, $"`{anchor}` does not exist in {markdown.FileName}."); } - private static void UpdateLinkUrl(LinkInline link, string url, string? anchor, string urlPathPrefix) + private static void UpdateLinkUrl(LinkInline link, string url, ParserContext context, string? anchor, IFileInfo file) { + var urlPathPrefix = context.Build.UrlPathPrefix ?? string.Empty; + + if (!url.StartsWith('/') && !string.IsNullOrEmpty(url)) + url = GetRootRelativePath(context, file); + if (url.EndsWith(".md")) url = Path.ChangeExtension(url, ".html"); - if (url.StartsWith("/") && !string.IsNullOrWhiteSpace(urlPathPrefix)) + if (!string.IsNullOrWhiteSpace(url) && !string.IsNullOrWhiteSpace(urlPathPrefix)) url = $"{urlPathPrefix.TrimEnd('/')}{url}"; - link.Url = !string.IsNullOrEmpty(anchor) ? $"{url}#{anchor}" : url; + link.Url = string.IsNullOrEmpty(anchor) ? url : $"{url}#{anchor}"; + } + + private static string GetRootRelativePath(ParserContext context, IFileInfo file) + { + var docsetDirectory = context.Configuration.SourceFile.Directory; + return file.FullName.Replace(docsetDirectory!.FullName, string.Empty); } private static bool IsCrossLink(Uri? uri) => diff --git a/tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs b/tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs index d3a935fa..a070dd9d 100644 --- a/tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs @@ -76,7 +76,7 @@ [Sub Requirements](testing/req.md#sub-requirements) public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Sub Requirements

""" + """

Sub Requirements

""" ); [Fact] @@ -94,7 +94,7 @@ [Sub Requirements](testing/req.md#new-reqs) public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Sub Requirements

""" + """

Sub Requirements

""" ); [Fact] @@ -111,7 +111,7 @@ public class ExternalPageAnchorAutoTitleTests(ITestOutputHelper output) : Anchor public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Special Requirements > Sub Requirements

""" + """

Special Requirements > Sub Requirements

""" ); [Fact] @@ -147,7 +147,7 @@ [Sub Requirements](testing/req.md#sub-requirements2) public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Sub Requirements

""" + """

Sub Requirements

""" ); [Fact] @@ -166,9 +166,8 @@ [Heading inside dropdown](testing/req.md#heading-inside-dropdown) public void GeneratesHtml() => // language=html Html.Should().Contain( - """Heading inside dropdown""" + """Heading inside dropdown""" ); - [Fact] public void HasError() => Collector.Diagnostics.Should().HaveCount(0); } diff --git a/tests/Elastic.Markdown.Tests/Inline/DirectiveBlockLinkTests.cs b/tests/Elastic.Markdown.Tests/Inline/DirectiveBlockLinkTests.cs index 500e8bc5..77f9e978 100644 --- a/tests/Elastic.Markdown.Tests/Inline/DirectiveBlockLinkTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/DirectiveBlockLinkTests.cs @@ -67,7 +67,7 @@ [Sub Requirements](testing/req.md#hint_ref) public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Sub Requirements

""" + """

Sub Requirements

""" ); [Fact] diff --git a/tests/Elastic.Markdown.Tests/Inline/InlineAnchorTests.cs b/tests/Elastic.Markdown.Tests/Inline/InlineAnchorTests.cs index 77591a9e..253b0bcf 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlineAnchorTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlineAnchorTests.cs @@ -203,7 +203,7 @@ [Sub Requirements](testing/req.md#custom-anchor) public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Sub Requirements

""" + """

Sub Requirements

""" ); [Fact] diff --git a/tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs b/tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs index 1fea4fed..339f3a47 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs @@ -20,7 +20,7 @@ public class InlineImageTest(ITestOutputHelper output) : InlineTest( public void GeneratesAttributesInHtml() => // language=html Html.Should().Contain( - """

Elasticsearch

""" + """

Elasticsearch

""" ); } @@ -37,6 +37,6 @@ public class RelativeInlineImageTest(ITestOutputHelper output) : InlineTest // language=html Html.Should().Contain( - """

Elasticsearch

""" + """

Elasticsearch

""" ); } diff --git a/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs b/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs index e36a69a0..1dd5d072 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs @@ -41,7 +41,7 @@ public class InlineLinkTests(ITestOutputHelper output) : LinkTestBase(output, public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Elasticsearch

""" + """

Elasticsearch

""" ); [Fact] @@ -58,7 +58,7 @@ public class LinkToPageTests(ITestOutputHelper output) : LinkTestBase(output, public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Requirements

""" + """

Requirements

""" ); [Fact] @@ -81,7 +81,7 @@ public class InsertPageTitleTests(ITestOutputHelper output) : LinkTestBase(outpu public void GeneratesHtml() => // language=html Html.Should().Contain( - """

Special Requirements

""" + """

Special Requirements

""" ); [Fact] @@ -106,7 +106,7 @@ public class LinkReferenceTest(ITestOutputHelper output) : LinkTestBase(output, public void GeneratesHtml() => // language=html Html.Should().Contain( - """

test

""" + """

test

""" ); [Fact] @@ -225,10 +225,10 @@ public void GeneratesHtml() => Html.TrimEnd().Should().Be("""

Links:

"""); diff --git a/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs b/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs index 4d794a85..8c3cc863 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs @@ -100,7 +100,7 @@ protected InlineTest( { "docs/index.md", new MockFileData(documentContents) } }, new MockFileSystemOptions { - CurrentDirectory = Paths.Root.FullName + CurrentDirectory = Paths.Root.FullName, }); // ReSharper disable once VirtualMemberCallInConstructor // nasty but sub implementations won't use class state. @@ -112,7 +112,8 @@ protected InlineTest( Collector = new TestDiagnosticsCollector(output); var context = new BuildContext(FileSystem) { - Collector = Collector + Collector = Collector, + UrlPathPrefix = "/docs" }; Set = new DocumentationSet(context); File = Set.GetMarkdownFile(FileSystem.FileInfo.New("docs/index.md")) ?? throw new NullReferenceException(); diff --git a/tests/authoring/Inline/InlineImages.fs b/tests/authoring/Inline/InlineImages.fs index 8320b9fd..e9258fb3 100644 --- a/tests/authoring/Inline/InlineImages.fs +++ b/tests/authoring/Inline/InlineImages.fs @@ -26,7 +26,7 @@ type ``relative path to image`` () = [] let ``validate HTML: preserves relative path`` () = markdown |> convertsToHtml """ -

Elasticsearch

+

Elasticsearch

""" type ``supplying a tittle`` () = @@ -37,7 +37,7 @@ type ``supplying a tittle`` () = [] let ``validate HTML: includes title`` () = markdown |> convertsToHtml """ -

Elasticsearch

+

Elasticsearch

""" type ``supplying a tittle with width and height`` () = @@ -48,7 +48,7 @@ type ``supplying a tittle with width and height`` () = [] let ``validate HTML: does not include width and height in title`` () = markdown |> convertsToHtml """ -

o

+

o

""" type ``supplying a tittle with width and height in percentage`` () = @@ -59,7 +59,7 @@ type ``supplying a tittle with width and height in percentage`` () = [] let ``validate HTML: does not include width and height in title`` () = markdown |> convertsToHtml """ -

o

+

o

""" type ``supplying a tittle with width only`` () = static let markdown = Setup.Markdown """ @@ -69,5 +69,5 @@ type ``supplying a tittle with width only`` () = [] let ``validate HTML: sets height to width if not supplied`` () = markdown |> convertsToHtml """ -

o

+

o

"""