-
Notifications
You must be signed in to change notification settings - Fork 30
Introduce IUriEnvironmentResolver to resolve crosslinks based on deployment environment #728
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
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
35 changes: 35 additions & 0 deletions
35
src/Elastic.Markdown/CrossLinks/IUriEnvironmentResolver.cs
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,35 @@ | ||
// 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 | ||
|
||
namespace Elastic.Markdown.CrossLinks; | ||
|
||
public interface IUriEnvironmentResolver | ||
{ | ||
Uri Resolve(Uri crossLinkUri, string path); | ||
} | ||
|
||
public class PreviewEnvironmentUriResolver : IUriEnvironmentResolver | ||
{ | ||
private static Uri BaseUri { get; } = new("https://docs-v3-preview.elastic.dev"); | ||
|
||
public Uri Resolve(Uri crossLinkUri, string path) | ||
{ | ||
var branch = GetBranch(crossLinkUri); | ||
return new Uri(BaseUri, $"elastic/{crossLinkUri.Scheme}/tree/{branch}/{path}"); | ||
} | ||
|
||
/// Hardcoding these for now, we'll have an index.json pointing to all links.json files | ||
/// at some point from which we can query the branch soon. | ||
private static string GetBranch(Uri crossLinkUri) | ||
{ | ||
var branch = crossLinkUri.Scheme switch | ||
{ | ||
"docs-content" => "main", | ||
_ => "main" | ||
}; | ||
return branch; | ||
} | ||
|
||
|
||
} |
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
57 changes: 57 additions & 0 deletions
57
src/docs-assembler/Building/PublishEnvironmentUriResolver.cs
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,57 @@ | ||
// 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.Collections.Frozen; | ||
using Documentation.Assembler.Configuration; | ||
using Elastic.Markdown.CrossLinks; | ||
|
||
namespace Documentation.Assembler.Building; | ||
|
||
public class PublishEnvironmentUriResolver : IUriEnvironmentResolver | ||
{ | ||
private Uri BaseUri { get; } | ||
|
||
private PublishEnvironment PublishEnvironment { get; } | ||
|
||
private PreviewEnvironmentUriResolver PreviewResolver { get; } | ||
|
||
private FrozenDictionary<string, Repository> AllRepositories { get; } | ||
|
||
public PublishEnvironmentUriResolver(AssemblyConfiguration configuration, string environment) | ||
{ | ||
if (!configuration.Environments.TryGetValue(environment, out var e)) | ||
throw new Exception($"Could not find environment {environment}"); | ||
if (!Uri.TryCreate(e.Uri, UriKind.Absolute, out var uri)) | ||
throw new Exception($"Could not parse uri {e.Uri} in environment {environment}"); | ||
|
||
BaseUri = uri; | ||
PublishEnvironment = e; | ||
PreviewResolver = new PreviewEnvironmentUriResolver(); | ||
AllRepositories = configuration.ReferenceRepositories.Values.Concat<Repository>([configuration.Narrative]) | ||
.ToFrozenDictionary(e => e.Name, e => e); | ||
RepositoryLookup = AllRepositories.GetAlternateLookup<ReadOnlySpan<char>>(); | ||
} | ||
|
||
private FrozenDictionary<string, Repository>.AlternateLookup<ReadOnlySpan<char>> RepositoryLookup { get; } | ||
|
||
public Uri Resolve(Uri crossLinkUri, string path) | ||
{ | ||
if (PublishEnvironment.Name == "preview") | ||
return PreviewResolver.Resolve(crossLinkUri, path); | ||
|
||
var repositoryPath = crossLinkUri.Scheme; | ||
if (RepositoryLookup.TryGetValue(crossLinkUri.Scheme, out var repository)) | ||
repositoryPath = repository.PathPrefix; | ||
|
||
var fullPath = (PublishEnvironment.PathPrefix, repositoryPath) switch | ||
{ | ||
(null or "", null or "") => path, | ||
(null or "", var p) => $"{p}/{path}", | ||
(var p, null or "") => $"{p}/{path}", | ||
var (p, pp) => $"{p}/{pp}/{path}" | ||
}; | ||
|
||
return new Uri(BaseUri, fullPath); | ||
} | ||
} |
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
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.