Skip to content

Commit

Permalink
Add support for extensionless URLs (pretty URLs) (#274)
Browse files Browse the repository at this point in the history
* Add support for extensionless URLs

* format

* Refactor

* Remove commented line

* Fix tests

* Also strip index

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

* Fix test
  • Loading branch information
reakaleek authored Feb 10, 2025
1 parent cc07233 commit 513f300
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
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 @@ -75,7 +75,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 @@ -93,7 +93,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 @@ -110,7 +110,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 @@ -146,7 +146,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 @@ -165,7 +165,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 @@ -66,7 +66,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 @@ -202,7 +202,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 @@ -57,7 +57,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 @@ -80,7 +80,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 @@ -105,7 +105,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 @@ -131,6 +131,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 @@ -224,10 +225,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

0 comments on commit 513f300

Please sign in to comment.