Skip to content
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
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public record BuildContext
// This property is used to determine if the site should be indexed by search engines
public bool AllowIndexing { get; init; }

// This property is used for the canonical URL
public Uri? CanonicalBaseUrl { get; init; }

private readonly string? _urlPathPrefix;
public string? UrlPathPrefix
{
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument d
Applies = markdown.YamlFrontMatter?.AppliesTo,
GithubEditUrl = editUrl,
AllowIndexing = DocumentationSet.Build.AllowIndexing && !markdown.Hidden,
CanonicalBaseUrl = DocumentationSet.Build.CanonicalBaseUrl,
Features = DocumentationSet.Configuration.Features,
StaticFileContentHashProvider = StaticFileContentHashProvider
});
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Slices/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
UrlPathPrefix = Model.UrlPathPrefix,
GithubEditUrl = Model.GithubEditUrl,
AllowIndexing = Model.AllowIndexing,
CanonicalBaseUrl = Model.CanonicalBaseUrl,
Features = Model.Features,
StaticFileContentHashProvider = Model.StaticFileContentHashProvider
};
Expand Down
7 changes: 6 additions & 1 deletion src/Elastic.Markdown/Slices/Layout/_Head.cshtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@inherits RazorSlice<LayoutViewModel>
@using Elastic.Markdown.Helpers
<head>
<title>@Model.Title</title>
@foreach (var fontFile in await FontPreloader.GetFontUrisAsync(@Model.UrlPathPrefix))
{
<link rel="preload" href="@fontFile" as="font" type="font/woff2" crossorigin>
}
<link rel="stylesheet preload" as="style" type="text/css" href="@Model.Static("styles.css")" crossorigin/>
<title>@Model.Title</title>

@if (Model.CanonicalBaseUrl is not null)
{
<link rel="canonical" href="@(new Uri(Model.CanonicalBaseUrl, Model.CurrentDocument.Url).ToString())">
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@await RenderPartialAsync(_Favicon.Create())
Expand Down
2 changes: 2 additions & 0 deletions src/Elastic.Markdown/Slices/_ViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class IndexViewModel
public required string? GithubEditUrl { get; init; }
public required ApplicableTo? Applies { get; init; }
public required bool AllowIndexing { get; init; }
public required Uri? CanonicalBaseUrl { get; init; }
public required FeatureFlags Features { get; init; }
public required StaticFileContentHashProvider StaticFileContentHashProvider { get; init; }
}
Expand All @@ -48,6 +49,7 @@ public class LayoutViewModel
public required string? UrlPathPrefix { get; init; }
public required string? GithubEditUrl { get; init; }
public required bool AllowIndexing { get; init; }
public required Uri? CanonicalBaseUrl { get; init; }
public required FeatureFlags Features { get; init; }

public required DocumentationGroup[] TopLevelNavigationItems { get; init; }
Expand Down
15 changes: 13 additions & 2 deletions src/docs-builder/Cli/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public async Task ServeStatic(string? path = null, int port = 4000, Cancel ctx =
/// <param name="strict"> Treat warnings as errors and fail the build on warnings</param>
/// <param name="allowIndexing"> Allow indexing and following of html files</param>
/// <param name="metadataOnly"> Only emit documentation metadata to output</param>
/// <param name="canonicalBaseUrl"> The base URL for the canonical url tag</param>
/// <param name="ctx"></param>
[Command("generate")]
[ConsoleAppFilter<StopwatchFilter>]
Expand All @@ -89,6 +90,7 @@ public async Task<int> Generate(
bool? strict = null,
bool? allowIndexing = null,
bool? metadataOnly = null,
string? canonicalBaseUrl = null,
Cancel ctx = default
)
{
Expand All @@ -99,13 +101,20 @@ public async Task<int> Generate(

var runningOnCi = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"));
BuildContext context;

Uri? canonicalBaseUri = null;

if (canonicalBaseUrl != null && !Uri.TryCreate(canonicalBaseUrl, UriKind.Absolute, out canonicalBaseUri))
throw new ArgumentException($"The canonical base url '{canonicalBaseUrl}' is not a valid absolute uri");

try
{
context = new BuildContext(collector, fileSystem, fileSystem, path, output)
{
UrlPathPrefix = pathPrefix,
Force = force ?? false,
AllowIndexing = allowIndexing ?? false
AllowIndexing = allowIndexing ?? false,
CanonicalBaseUrl = canonicalBaseUri
};
}
// On CI, we are running on merge commit which may have changes against an older
Expand Down Expand Up @@ -156,6 +165,7 @@ public async Task<int> Generate(
/// <param name="strict"> Treat warnings as errors and fail the build on warnings</param>
/// <param name="allowIndexing"> Allow indexing and following of html files</param>
/// <param name="metadataOnly"> Only emit documentation metadata to output</param>
/// <param name="canonicalBaseUrl"> The base URL for the canonical url tag</param>
/// <param name="ctx"></param>
[Command("")]
[ConsoleAppFilter<StopwatchFilter>]
Expand All @@ -169,9 +179,10 @@ public async Task<int> GenerateDefault(
bool? strict = null,
bool? allowIndexing = null,
bool? metadataOnly = null,
string? canonicalBaseUrl = null,
Cancel ctx = default
) =>
await Generate(path, output, pathPrefix, force, strict, allowIndexing, metadataOnly, ctx);
await Generate(path, output, pathPrefix, force, strict, allowIndexing, metadataOnly, canonicalBaseUrl, ctx);


/// <summary>
Expand Down
Loading