Skip to content

Commit

Permalink
Fix relative links (#442)
Browse files Browse the repository at this point in the history
* Fix relative links

* fix

* test

* fixes

* Set url path prefix for tests

* ok
  • Loading branch information
reakaleek authored Feb 10, 2025
1 parent 689c4e5 commit 792cf01
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) =>
Expand Down
11 changes: 5 additions & 6 deletions tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ [Sub Requirements](testing/req.md#sub-requirements)
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html#sub-requirements">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req.html#sub-requirements">Sub Requirements</a></p>"""
);

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

[Fact]
Expand All @@ -111,7 +111,7 @@ public class ExternalPageAnchorAutoTitleTests(ITestOutputHelper output) : Anchor
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html#sub-requirements">Special Requirements &gt; Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req.html#sub-requirements">Special Requirements &gt; Sub Requirements</a></p>"""
);

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

[Fact]
Expand All @@ -166,9 +166,8 @@ [Heading inside dropdown](testing/req.md#heading-inside-dropdown)
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<a href="testing/req.html#heading-inside-dropdown">Heading inside dropdown</a>"""
"""<a href="/docs/testing/req.html#heading-inside-dropdown">Heading inside dropdown</a>"""
);

[Fact]
public void HasError() => Collector.Diagnostics.Should().HaveCount(0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ [Sub Requirements](testing/req.md#hint_ref)
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html#hint_ref">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req.html#hint_ref">Sub Requirements</a></p>"""
);

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion tests/Elastic.Markdown.Tests/Inline/InlineAnchorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ [Sub Requirements](testing/req.md#custom-anchor)
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html#custom-anchor">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req.html#custom-anchor">Sub Requirements</a></p>"""
);

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class InlineImageTest(ITestOutputHelper output) : InlineTest<LinkInline>(
public void GeneratesAttributesInHtml() =>
// language=html
Html.Should().Contain(
"""<p><img src="/_static/img/observability.png" alt="Elasticsearch" /></p>"""
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
);
}

Expand All @@ -37,6 +37,6 @@ public class RelativeInlineImageTest(ITestOutputHelper output) : InlineTest<Link
public void GeneratesAttributesInHtml() =>
// language=html
Html.Should().Contain(
"""<p><img src="_static/img/observability.png" alt="Elasticsearch" /></p>"""
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
);
}
12 changes: 6 additions & 6 deletions tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class InlineLinkTests(ITestOutputHelper output) : LinkTestBase(output,
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="/_static/img/observability.png">Elasticsearch</a></p>"""
"""<p><a href="/docs/_static/img/observability.png">Elasticsearch</a></p>"""
);

[Fact]
Expand All @@ -58,7 +58,7 @@ public class LinkToPageTests(ITestOutputHelper output) : LinkTestBase(output,
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html">Requirements</a></p>"""
"""<p><a href="/docs/testing/req.html">Requirements</a></p>"""
);

[Fact]
Expand All @@ -81,7 +81,7 @@ public class InsertPageTitleTests(ITestOutputHelper output) : LinkTestBase(outpu
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html">Special Requirements</a></p>"""
"""<p><a href="/docs/testing/req.html">Special Requirements</a></p>"""
);

[Fact]
Expand All @@ -106,7 +106,7 @@ public class LinkReferenceTest(ITestOutputHelper output) : LinkTestBase(output,
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="testing/req.html">test</a></p>"""
"""<p><a href="/docs/testing/req.html">test</a></p>"""
);

[Fact]
Expand Down Expand Up @@ -225,10 +225,10 @@ public void GeneratesHtml() =>
Html.TrimEnd().Should().Be("""
<p>Links:</p>
<ul>
<li><a href="/testing/req.html">Special Requirements</a></li>
<li><a href="/docs/testing/req.html">Special Requirements</a></li>
</ul>
<ul>
<li><a href="/testing/req.html">Special Requirements</a></li>
<li><a href="/docs/testing/req.html">Special Requirements</a></li>
</ul>
""");

Expand Down
5 changes: 3 additions & 2 deletions tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions tests/authoring/Inline/InlineImages.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ``relative path to image`` () =
[<Fact>]
let ``validate HTML: preserves relative path`` () =
markdown |> convertsToHtml """
<p><img src="_static/img/observability.png" alt="Elasticsearch" /></p>
<p><img src="/_static/img/observability.png" alt="Elasticsearch" /></p>
"""

type ``supplying a tittle`` () =
Expand All @@ -37,7 +37,7 @@ type ``supplying a tittle`` () =
[<Fact>]
let ``validate HTML: includes title`` () =
markdown |> convertsToHtml """
<p><img src="_static/img/observability.png" alt="Elasticsearch" title="Hello world" /></p>
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Hello world" /></p>
"""

type ``supplying a tittle with width and height`` () =
Expand All @@ -48,7 +48,7 @@ type ``supplying a tittle with width and height`` () =
[<Fact>]
let ``validate HTML: does not include width and height in title`` () =
markdown |> convertsToHtml """
<p><img src="obs.png" width="250px" height="400px" alt="o" title="Title"/></p>
<p><img src="/obs.png" width="250px" height="400px" alt="o" title="Title"/></p>
"""

type ``supplying a tittle with width and height in percentage`` () =
Expand All @@ -59,7 +59,7 @@ type ``supplying a tittle with width and height in percentage`` () =
[<Fact>]
let ``validate HTML: does not include width and height in title`` () =
markdown |> convertsToHtml """
<p><img src="obs.png" width="50%" height="40%" alt="o" title="Title"/></p>
<p><img src="/obs.png" width="50%" height="40%" alt="o" title="Title"/></p>
"""
type ``supplying a tittle with width only`` () =
static let markdown = Setup.Markdown """
Expand All @@ -69,5 +69,5 @@ type ``supplying a tittle with width only`` () =
[<Fact>]
let ``validate HTML: sets height to width if not supplied`` () =
markdown |> convertsToHtml """
<p><img src="obs.png" width="30%" height="30%" alt="o" title="Title"/></p>
<p><img src="/obs.png" width="30%" height="30%" alt="o" title="Title"/></p>
"""

0 comments on commit 792cf01

Please sign in to comment.