Skip to content

Commit 83c37d2

Browse files
committed
Fix relative links
1 parent 4c6aab9 commit 83c37d2

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

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

Lines changed: 11 additions & 6 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,13 +235,19 @@ 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;
241241
if (url.EndsWith(".md"))
242242
url = Path.ChangeExtension(url, ".html");
243243

244244
if (url.StartsWith("/") && !string.IsNullOrWhiteSpace(urlPathPrefix))
245245
url = $"{urlPathPrefix.TrimEnd('/')}{url}";
246+
else
247+
{
248+
var docsetDirectory = context.Configuration.SourceFile.Directory;
249+
url = file.FullName.Replace(docsetDirectory!.FullName, string.Empty);
250+
}
246251

247252
link.Url = !string.IsNullOrEmpty(anchor) ? $"{url}#{anchor}" : url;
248253
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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="/testing/req#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/InlineImageTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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="/_static/img/observability.png" alt="Elasticsearch" /></p>"""
4141
);
4242
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public class LinkToPageTests(ITestOutputHelper output) : LinkTestBase(output,
5858
public void GeneratesHtml() =>
5959
// language=html
6060
Html.Should().Contain(
61+
<<<<<<< Updated upstream
6162
"""<p><a href="testing/req.html">Requirements</a></p>"""
63+
=======
64+
"""<p><a href="/testing/req">Requirements</a></p>"""
65+
>>>>>>> Stashed changes
6266
);
6367

6468
[Fact]
@@ -81,7 +85,7 @@ public class InsertPageTitleTests(ITestOutputHelper output) : LinkTestBase(outpu
8185
public void GeneratesHtml() =>
8286
// language=html
8387
Html.Should().Contain(
84-
"""<p><a href="testing/req.html">Special Requirements</a></p>"""
88+
"""<p><a href="/testing/req">Special Requirements</a></p>"""
8589
);
8690

8791
[Fact]
@@ -106,7 +110,11 @@ public class LinkReferenceTest(ITestOutputHelper output) : LinkTestBase(output,
106110
public void GeneratesHtml() =>
107111
// language=html
108112
Html.Should().Contain(
113+
<<<<<<< Updated upstream
109114
"""<p><a href="testing/req.html">test</a></p>"""
115+
=======
116+
"""<p><a href="/testing/req">test</a></p>"""
117+
>>>>>>> Stashed changes
110118
);
111119

112120
[Fact]

0 commit comments

Comments
 (0)