Skip to content

Commit 792cf01

Browse files
authored
Fix relative links (#442)
* Fix relative links * fix * test * fixes * Set url path prefix for tests * ok
1 parent 689c4e5 commit 792cf01

File tree

8 files changed

+41
-31
lines changed

8 files changed

+41
-31
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ private static void ProcessInternalLink(InlineProcessor processor, LinkInline li
168168
{
169169
var (url, anchor) = SplitUrlAndAnchor(link.Url ?? string.Empty);
170170
var includeFrom = GetIncludeFromPath(url, context);
171-
171+
var file = ResolveFile(context, url);
172172
ValidateInternalUrl(processor, url, includeFrom, line, column, length, context);
173-
ProcessLinkText(processor, link, context, url, anchor, line, column, length);
174-
UpdateLinkUrl(link, url, anchor, context.Build.UrlPathPrefix ?? string.Empty);
173+
ProcessLinkText(processor, link, context, url, anchor, line, column, length, file);
174+
UpdateLinkUrl(link, url, context, anchor, file);
175175
}
176176

177177
private static (string url, string? anchor) SplitUrlAndAnchor(string fullUrl)
@@ -195,12 +195,11 @@ private static void ValidateInternalUrl(InlineProcessor processor, string url, s
195195
processor.EmitError(line, column, length, $"`{url}` does not exist. resolved to `{pathOnDisk}");
196196
}
197197

198-
private static void ProcessLinkText(InlineProcessor processor, LinkInline link, ParserContext context, string url, string? anchor, int line, int column, int length)
198+
private static void ProcessLinkText(InlineProcessor processor, LinkInline link, ParserContext context, string url, string? anchor, int line, int column, int length, IFileInfo file)
199199
{
200200
if (link.FirstChild != null && string.IsNullOrEmpty(anchor))
201201
return;
202202

203-
var file = ResolveFile(context, url);
204203
var markdown = context.GetDocumentationFile?.Invoke(file) as MarkdownFile;
205204

206205
if (markdown == null)
@@ -236,15 +235,26 @@ private static void ValidateAnchor(InlineProcessor processor, MarkdownFile markd
236235
processor.EmitError(line, column, length, $"`{anchor}` does not exist in {markdown.FileName}.");
237236
}
238237

239-
private static void UpdateLinkUrl(LinkInline link, string url, string? anchor, string urlPathPrefix)
238+
private static void UpdateLinkUrl(LinkInline link, string url, ParserContext context, string? anchor, IFileInfo file)
240239
{
240+
var urlPathPrefix = context.Build.UrlPathPrefix ?? string.Empty;
241+
242+
if (!url.StartsWith('/') && !string.IsNullOrEmpty(url))
243+
url = GetRootRelativePath(context, file);
244+
241245
if (url.EndsWith(".md"))
242246
url = Path.ChangeExtension(url, ".html");
243247

244-
if (url.StartsWith("/") && !string.IsNullOrWhiteSpace(urlPathPrefix))
248+
if (!string.IsNullOrWhiteSpace(url) && !string.IsNullOrWhiteSpace(urlPathPrefix))
245249
url = $"{urlPathPrefix.TrimEnd('/')}{url}";
246250

247-
link.Url = !string.IsNullOrEmpty(anchor) ? $"{url}#{anchor}" : url;
251+
link.Url = string.IsNullOrEmpty(anchor) ? url : $"{url}#{anchor}";
252+
}
253+
254+
private static string GetRootRelativePath(ParserContext context, IFileInfo file)
255+
{
256+
var docsetDirectory = context.Configuration.SourceFile.Directory;
257+
return file.FullName.Replace(docsetDirectory!.FullName, string.Empty);
248258
}
249259

250260
private static bool IsCrossLink(Uri? uri) =>

tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ [Sub Requirements](testing/req.md#sub-requirements)
7676
public void GeneratesHtml() =>
7777
// language=html
7878
Html.Should().Contain(
79-
"""<p><a href="testing/req.html#sub-requirements">Sub Requirements</a></p>"""
79+
"""<p><a href="/docs/testing/req.html#sub-requirements">Sub Requirements</a></p>"""
8080
);
8181

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

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

117117
[Fact]
@@ -147,7 +147,7 @@ [Sub Requirements](testing/req.md#sub-requirements2)
147147
public void GeneratesHtml() =>
148148
// language=html
149149
Html.Should().Contain(
150-
"""<p><a href="testing/req.html#sub-requirements2">Sub Requirements</a></p>"""
150+
"""<p><a href="/docs/testing/req.html#sub-requirements2">Sub Requirements</a></p>"""
151151
);
152152

153153
[Fact]
@@ -166,9 +166,8 @@ [Heading inside dropdown](testing/req.md#heading-inside-dropdown)
166166
public void GeneratesHtml() =>
167167
// language=html
168168
Html.Should().Contain(
169-
"""<a href="testing/req.html#heading-inside-dropdown">Heading inside dropdown</a>"""
169+
"""<a href="/docs/testing/req.html#heading-inside-dropdown">Heading inside dropdown</a>"""
170170
);
171-
172171
[Fact]
173172
public void HasError() => Collector.Diagnostics.Should().HaveCount(0);
174173
}

tests/Elastic.Markdown.Tests/Inline/DirectiveBlockLinkTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ [Sub Requirements](testing/req.md#hint_ref)
6767
public void GeneratesHtml() =>
6868
// language=html
6969
Html.Should().Contain(
70-
"""<p><a href="testing/req.html#hint_ref">Sub Requirements</a></p>"""
70+
"""<p><a href="/docs/testing/req.html#hint_ref">Sub Requirements</a></p>"""
7171
);
7272

7373
[Fact]

tests/Elastic.Markdown.Tests/Inline/InlineAnchorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ [Sub Requirements](testing/req.md#custom-anchor)
203203
public void GeneratesHtml() =>
204204
// language=html
205205
Html.Should().Contain(
206-
"""<p><a href="testing/req.html#custom-anchor">Sub Requirements</a></p>"""
206+
"""<p><a href="/docs/testing/req.html#custom-anchor">Sub Requirements</a></p>"""
207207
);
208208

209209
[Fact]

tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class InlineImageTest(ITestOutputHelper output) : InlineTest<LinkInline>(
2020
public void GeneratesAttributesInHtml() =>
2121
// language=html
2222
Html.Should().Contain(
23-
"""<p><img src="/_static/img/observability.png" alt="Elasticsearch" /></p>"""
23+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
2424
);
2525
}
2626

@@ -37,6 +37,6 @@ public class RelativeInlineImageTest(ITestOutputHelper output) : InlineTest<Link
3737
public void GeneratesAttributesInHtml() =>
3838
// language=html
3939
Html.Should().Contain(
40-
"""<p><img src="_static/img/observability.png" alt="Elasticsearch" /></p>"""
40+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
4141
);
4242
}

tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class InlineLinkTests(ITestOutputHelper output) : LinkTestBase(output,
4141
public void GeneratesHtml() =>
4242
// language=html
4343
Html.Should().Contain(
44-
"""<p><a href="/_static/img/observability.png">Elasticsearch</a></p>"""
44+
"""<p><a href="/docs/_static/img/observability.png">Elasticsearch</a></p>"""
4545
);
4646

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

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

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

112112
[Fact]
@@ -225,10 +225,10 @@ public void GeneratesHtml() =>
225225
Html.TrimEnd().Should().Be("""
226226
<p>Links:</p>
227227
<ul>
228-
<li><a href="/testing/req.html">Special Requirements</a></li>
228+
<li><a href="/docs/testing/req.html">Special Requirements</a></li>
229229
</ul>
230230
<ul>
231-
<li><a href="/testing/req.html">Special Requirements</a></li>
231+
<li><a href="/docs/testing/req.html">Special Requirements</a></li>
232232
</ul>
233233
""");
234234

tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected InlineTest(
100100
{ "docs/index.md", new MockFileData(documentContents) }
101101
}, new MockFileSystemOptions
102102
{
103-
CurrentDirectory = Paths.Root.FullName
103+
CurrentDirectory = Paths.Root.FullName,
104104
});
105105
// ReSharper disable once VirtualMemberCallInConstructor
106106
// nasty but sub implementations won't use class state.
@@ -112,7 +112,8 @@ protected InlineTest(
112112
Collector = new TestDiagnosticsCollector(output);
113113
var context = new BuildContext(FileSystem)
114114
{
115-
Collector = Collector
115+
Collector = Collector,
116+
UrlPathPrefix = "/docs"
116117
};
117118
Set = new DocumentationSet(context);
118119
File = Set.GetMarkdownFile(FileSystem.FileInfo.New("docs/index.md")) ?? throw new NullReferenceException();

tests/authoring/Inline/InlineImages.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ``relative path to image`` () =
2626
[<Fact>]
2727
let ``validate HTML: preserves relative path`` () =
2828
markdown |> convertsToHtml """
29-
<p><img src="_static/img/observability.png" alt="Elasticsearch" /></p>
29+
<p><img src="/_static/img/observability.png" alt="Elasticsearch" /></p>
3030
"""
3131

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

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

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

0 commit comments

Comments
 (0)