Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -59,7 +59,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; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
}

if (url.EndsWith(".md"))
link.Url = Path.ChangeExtension(url, ".html");
link.Url = url.Remove(url.LastIndexOf(".md", StringComparison.Ordinal), 3);
// rooted links might need the configured path prefix to properly link
var prefix = processor.GetBuildContext().UrlPathPrefix;
if (url.StartsWith("/") && !string.IsNullOrWhiteSpace(prefix))
Expand Down
21 changes: 20 additions & 1 deletion src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,26 @@ 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");
// 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 @@ -91,9 +91,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
8 changes: 4 additions & 4 deletions tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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="testing/req#sub-requirements">Sub Requirements</a></p>"""
);

[Fact]
Expand All @@ -89,7 +89,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="testing/req#new-reqs">Sub Requirements</a></p>"""
);

[Fact]
Expand All @@ -106,7 +106,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="testing/req#sub-requirements">Special Requirements &gt; Sub Requirements</a></p>"""
);

[Fact]
Expand Down Expand Up @@ -142,7 +142,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="testing/req#sub-requirements2">Sub Requirements</a></p>"""
);

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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="testing/req#hint_ref">Sub Requirements</a></p>"""
);

[Fact]
Expand Down
14 changes: 7 additions & 7 deletions tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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="testing/req">Requirements</a></p>"""
);

[Fact]
Expand All @@ -83,7 +83,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="testing/req">Special Requirements</a></p>"""
);

[Fact]
Expand All @@ -108,7 +108,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="testing/req">test</a></p>"""
);

[Fact]
Expand All @@ -134,7 +134,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.html">test</a></p>"""
"""<p><a href="kibana://index">test</a></p>"""
);

[Fact]
Expand All @@ -160,7 +160,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>Go to <a href="kibana://index.html">test</a></p>"""
"""<p>Go to <a href="kibana://index">test</a></p>"""
);

[Fact]
Expand Down Expand Up @@ -227,10 +227,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="/testing/req">Special Requirements</a></li>
</ul>
<ul>
<li> <a href="/testing/req.html">Special Requirements</a></li>
<li> <a href="/testing/req">Special Requirements</a></li>
</ul>
""");

Expand Down
Loading