Skip to content

Add support for extensionless URLs (pretty URLs) #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public string? NavigationTitle

public string FilePath { get; }
public string FileName { get; }
public string Url => $"{UrlPathPrefix}/{RelativePath.Replace(".md", ".html")}";
public string Url => Path.GetFileName(RelativePath) == "index.md"
? $"{UrlPathPrefix}/{RelativePath.Remove(RelativePath.LastIndexOf("index.md", StringComparison.Ordinal), "index.md".Length)}"
: $"{UrlPathPrefix}/{RelativePath.Remove(RelativePath.LastIndexOf(".md", StringComparison.Ordinal), 3)}";

public int NavigationIndex { get; internal set; } = -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
url = GetRootRelativePath(context, file);

if (url.EndsWith(".md"))
url = Path.ChangeExtension(url, ".html");
{
url = url.EndsWith("/index.md")
? url.Remove(url.LastIndexOf("index.md", StringComparison.Ordinal), "index.md".Length)
: url.Remove(url.LastIndexOf(".md", StringComparison.Ordinal), ".md".Length);
}

if (!string.IsNullOrWhiteSpace(url) && !string.IsNullOrWhiteSpace(urlPathPrefix))
url = $"{urlPathPrefix.TrimEnd('/')}{url}";
Expand Down
20 changes: 19 additions & 1 deletion src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,25 @@ public async Task WriteAsync(IFileInfo outputFile, MarkdownFile markdown, Cancel
outputFile.Directory.Create();

var rendered = await RenderLayout(markdown, ctx);
var path = Path.ChangeExtension(outputFile.FullName, ".html");
string path;
if (outputFile.Name == "index.md")
{
path = Path.ChangeExtension(outputFile.FullName, ".html");
}
else
{
var dir = outputFile.Directory is null
? null
: Path.Combine(outputFile.Directory.FullName, Path.GetFileNameWithoutExtension(outputFile.Name));

if (dir is not null && !_writeFileSystem.Directory.Exists(dir))
_writeFileSystem.Directory.CreateDirectory(dir);

path = dir is null
? Path.GetFileNameWithoutExtension(outputFile.Name) + ".html"
: Path.Combine(dir, "index.html");
}

await _writeFileSystem.File.WriteAllTextAsync(path, rendered, ctx);
}

Expand Down
15 changes: 12 additions & 3 deletions src/docs-builder/Http/DocumentationWebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,18 @@ private void SetUpRoutes()
private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorState holder, string slug, Cancel ctx)
{
var generator = holder.Generator;
slug = slug.Replace(".html", ".md");
if (!generator.DocumentationSet.FlatMappedFiles.TryGetValue(slug, out var documentationFile))
return Results.NotFound();

// For now, the logic is backwards compatible.
// Hence, both http://localhost:5000/migration/versioning.html and http://localhost:5000/migration/versioning works,
// so it's easier to copy links from issues created during the bug bounty.
// However, we can remove this logic in the future and only support links without the .html extension.
var s = Path.GetExtension(slug) == string.Empty ? Path.Combine(slug, "index.md") : slug.Replace(".html", ".md");
if (!generator.DocumentationSet.FlatMappedFiles.TryGetValue(s, out var documentationFile))
{
s = Path.GetExtension(slug) == string.Empty ? slug + ".md" : s.Replace("/index.md", ".md");
if (!generator.DocumentationSet.FlatMappedFiles.TryGetValue(s, out documentationFile))
return Results.NotFound();
}

switch (documentationFile)
{
Expand Down
10 changes: 5 additions & 5 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="/docs/testing/req.html#sub-requirements">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req#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="/docs/testing/req.html#new-reqs">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req#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="/docs/testing/req.html#sub-requirements">Special Requirements &gt; Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req#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="/docs/testing/req.html#sub-requirements2">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req#sub-requirements2">Sub Requirements</a></p>"""
);

[Fact]
Expand All @@ -166,7 +166,7 @@ [Heading inside dropdown](testing/req.md#heading-inside-dropdown)
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<a href="/docs/testing/req.html#heading-inside-dropdown">Heading inside dropdown</a>"""
"""<a href="/docs/testing/req#heading-inside-dropdown">Heading inside dropdown</a>"""
);
[Fact]
public void HasError() => Collector.Diagnostics.Should().HaveCount(0);
Expand Down
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="/docs/testing/req.html#hint_ref">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req#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="/docs/testing/req.html#custom-anchor">Sub Requirements</a></p>"""
"""<p><a href="/docs/testing/req#custom-anchor">Sub Requirements</a></p>"""
);

[Fact]
Expand Down
11 changes: 6 additions & 5 deletions tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class LinkToPageTests(ITestOutputHelper output) : LinkTestBase(output,
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<p><a href="/docs/testing/req.html">Requirements</a></p>"""
"""<p><a href="/docs/testing/req">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="/docs/testing/req.html">Special Requirements</a></p>"""
"""<p><a href="/docs/testing/req">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="/docs/testing/req.html">test</a></p>"""
"""<p><a href="/docs/testing/req">test</a></p>"""
);

[Fact]
Expand All @@ -132,6 +132,7 @@ public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
// TODO: The link is not rendered correctly yet, will be fixed in a follow-up

"""<p><a href="kibana://index.md">test</a></p>"""
);

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

Expand Down