-
Notifications
You must be signed in to change notification settings - Fork 44
Derive page titles from product metadata #4083
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using Elastic.Documentation; | ||
| using Elastic.Documentation.Configuration.Products; | ||
| using Elastic.Documentation.Configuration.Toc; | ||
|
|
||
| namespace Elastic.Markdown.Page; | ||
|
|
||
| internal readonly record struct PageTitleOptions(BuildType BuildType, BrandingConfiguration? Branding, string SiteName); | ||
|
|
||
| internal static class PageTitleResolver | ||
| { | ||
| public static string Resolve(string title, IReadOnlyCollection<Product> products, PageTitleOptions options) | ||
| { | ||
| if (products is { Count: 1 }) | ||
| { | ||
| var productName = products.First().DisplayName; | ||
| if (!title.Contains(productName, StringComparison.OrdinalIgnoreCase)) | ||
| title = $"{title} - {productName}"; | ||
| } | ||
|
|
||
| var suffix = options.BuildType == BuildType.Assembler && options.Branding is null ? "Elastic Docs" : options.SiteName; | ||
| return $"{title} | {suffix}"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.IO.Abstractions.TestingHelpers; | ||
| using AwesomeAssertions; | ||
| using Elastic.Documentation; | ||
| using Elastic.Documentation.Configuration; | ||
| using Elastic.Documentation.Diagnostics; | ||
| using Elastic.Markdown.IO; | ||
|
|
||
| namespace Elastic.Markdown.Tests; | ||
|
|
||
| public class PageTitleTests(ITestOutputHelper output) | ||
| { | ||
| [Fact] | ||
| public async Task GenerateAll_DocsetProductMissingFromH1_AppendsInferredProductName() | ||
| { | ||
| var html = await Generate(BuildType.Assembler, "# Query DSL", docsetProduct: "elasticsearch"); | ||
|
|
||
| html.Should().Contain("<title>Query DSL - Elasticsearch | Elastic Docs</title>"); | ||
| html.Should().Contain("<meta property=\"og:title\" content=\"Query DSL - Elasticsearch | Elastic Docs\""); | ||
| html.Should().Contain("<meta data-pagefind-meta=\"title[content]\" content=\"Query DSL - Elasticsearch | Elastic Docs\""); | ||
| html.Should().Contain("<h1>Query DSL</h1>"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GenerateAll_ProductAlreadyInH1_DoesNotAppendProductName() | ||
| { | ||
| var html = await Generate( | ||
| BuildType.Assembler, | ||
| """ | ||
| --- | ||
| products: | ||
| - id: elasticsearch | ||
| --- | ||
|
|
||
| # Elasticsearch query DSL | ||
| """ | ||
| ); | ||
|
|
||
| html.Should().Contain("<title>Elasticsearch query DSL | Elastic Docs</title>"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GenerateAll_MultipleProducts_DoesNotChooseAProductName() | ||
| { | ||
| var html = await Generate( | ||
| BuildType.Assembler, | ||
| """ | ||
| --- | ||
| products: | ||
| - id: elasticsearch | ||
| - id: kibana | ||
| --- | ||
|
|
||
| # Query languages | ||
| """ | ||
| ); | ||
|
|
||
| html.Should().Contain("<title>Query languages | Elastic Docs</title>"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GenerateAll_BrandedAssemblerBuild_KeepsExistingSuffix() | ||
| { | ||
| var html = await Generate( | ||
| BuildType.Assembler, | ||
| """ | ||
| --- | ||
| products: | ||
| - id: elasticsearch | ||
| --- | ||
|
|
||
| # Query DSL | ||
| """, | ||
| branded: true | ||
| ); | ||
|
|
||
| html.Should().Contain("<title>Query DSL - Elasticsearch | Query DSL</title>"); | ||
| html.Should().NotContain("| Elastic Docs</title>"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GenerateAll_IsolatedBuild_KeepsExistingSuffix() | ||
| { | ||
| var html = await Generate( | ||
| BuildType.Isolated, | ||
| """ | ||
| --- | ||
| products: | ||
| - id: elasticsearch | ||
| --- | ||
|
|
||
| # Query DSL | ||
| """ | ||
| ); | ||
|
|
||
| html.Should().Contain("<title>Query DSL - Elasticsearch | Query DSL</title>"); | ||
| html.Should().NotContain("| Elastic Docs</title>"); | ||
| } | ||
|
|
||
| private async Task<string> Generate(BuildType buildType, string markdown, bool branded = false, string? docsetProduct = null) | ||
| { | ||
| var branding = branded ? """ | ||
| branding: | ||
| icon: assets/logo.svg | ||
| """ : string.Empty; | ||
| var products = docsetProduct is null | ||
| ? string.Empty | ||
| : $""" | ||
| products: | ||
| - id: {docsetProduct} | ||
| """; | ||
| var fileSystem = new MockFileSystem( | ||
| new Dictionary<string, MockFileData> | ||
| { | ||
| ["docs/docset.yml"] = new( | ||
| $""" | ||
| project: test | ||
| {products} | ||
| toc: | ||
| - file: index.md | ||
| {branding} | ||
| """ | ||
| ), | ||
| ["docs/index.md"] = new(markdown), | ||
| ["docs/assets/logo.svg"] = new("<svg/>") | ||
| }, | ||
| new MockFileSystemOptions { CurrentDirectory = Paths.WorkingDirectoryRoot.FullName } | ||
| ); | ||
| await using var collector = new DiagnosticsCollector([]).StartAsync(TestContext.Current.CancellationToken); | ||
| var configurationContext = TestHelpers.CreateConfigurationContext(fileSystem); | ||
| var context = new BuildContext(collector, TestHelpers.CreateDocumentationFileSystem(fileSystem), configurationContext) | ||
| { | ||
| BuildType = buildType | ||
| }; | ||
| var set = new DocumentationSet(context, new TestLoggerFactory(output), new TestCrossLinkResolver()); | ||
| var generator = new DocumentationGenerator(set, new TestLoggerFactory(output)); | ||
|
|
||
| await generator.GenerateAll(TestContext.Current.CancellationToken); | ||
| await collector.StopAsync(TestContext.Current.CancellationToken); | ||
|
|
||
| return fileSystem.File.ReadAllText(Path.Join(set.OutputDirectory.FullName, "index.html")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.