-
Notifications
You must be signed in to change notification settings - Fork 26
Add crosslinks to toc: in docset.yml #1615
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
theletterf
wants to merge
12
commits into
main
Choose a base branch
from
crosslinks-in-toc-take-three
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bf5747a
Add crosslinks to toc
theletterf c1bb57d
Fix errors
theletterf c92fb38
Update docs
theletterf d4bd6ca
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf b78c2c3
Add title validation
theletterf 04920ab
Add ctx for Cancel
theletterf effe888
FileNavigationItem can be ignored
theletterf 03ec234
Remove redundant code
theletterf d3a8574
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf 134b86d
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf 081d4c4
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf ca8bc40
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,25 @@ public DocumentationSet( | |
.ToDictionary(kv => kv.Item1, kv => kv.Item2) | ||
.ToFrozenDictionary(); | ||
|
||
// Validate cross-repo links in navigation | ||
|
||
try | ||
{ | ||
// First ensure links are fetched - this is essential for resolving links properly | ||
_ = LinkResolver.FetchLinks(new Cancel()).GetAwaiter().GetResult(); | ||
|
||
NavigationCrossLinkValidator.ValidateNavigationCrossLinksAsync( | ||
Tree, | ||
LinkResolver, | ||
(msg) => Context.EmitError(Context.ConfigurationPath, msg) | ||
).GetAwaiter().GetResult(); | ||
} | ||
catch (Exception e) | ||
{ | ||
// Log the error but don't fail the build | ||
Context.EmitError(Context.ConfigurationPath, $"Error validating cross-links in navigation: {e.Message}"); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this routine to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which Resolve()? Tree.Resolve? |
||
ValidateRedirectsExists(); | ||
} | ||
|
||
|
@@ -222,6 +241,10 @@ private void UpdateNavigationIndex(IReadOnlyCollection<INavigationItem> navigati | |
var fileIndex = Interlocked.Increment(ref navigationIndex); | ||
fileNavigationItem.NavigationIndex = fileIndex; | ||
break; | ||
case CrossLinkNavigationItem crossLinkNavigationItem: | ||
var crossLinkIndex = Interlocked.Increment(ref navigationIndex); | ||
crossLinkNavigationItem.NavigationIndex = crossLinkIndex; | ||
break; | ||
case DocumentationGroup documentationGroup: | ||
var groupIndex = Interlocked.Increment(ref navigationIndex); | ||
documentationGroup.NavigationIndex = groupIndex; | ||
|
@@ -241,6 +264,9 @@ private static IReadOnlyCollection<INavigationItem> CreateNavigationLookup(INavi | |
if (item is ILeafNavigationItem<INavigationModel> leaf) | ||
return [leaf]; | ||
|
||
if (item is CrossLinkNavigationItem crossLink) | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return [crossLink]; | ||
|
||
if (item is INodeNavigationItem<INavigationModel, INavigationItem> node) | ||
{ | ||
var items = node.NavigationItems.SelectMany(CreateNavigationLookup); | ||
|
@@ -254,6 +280,8 @@ public static (string, INavigationItem)[] Pairs(INavigationItem item) | |
{ | ||
if (item is FileNavigationItem f) | ||
return [(f.Model.CrossLink, item)]; | ||
if (item is CrossLinkNavigationItem cl) | ||
return [(cl.Url, item)]; // Use the URL as the key for cross-links | ||
if (item is DocumentationGroup g) | ||
{ | ||
var index = new List<(string, INavigationItem)> | ||
|
67 changes: 67 additions & 0 deletions
67
src/Elastic.Markdown/IO/Navigation/CrossLinkNavigationItem.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,67 @@ | ||
// 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.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Elastic.Documentation.Site.Navigation; | ||
|
||
namespace Elastic.Markdown.IO.Navigation; | ||
|
||
[DebuggerDisplay("CrossLink: {Url}")] | ||
public record CrossLinkNavigationItem : ILeafNavigationItem<INavigationModel> | ||
{ | ||
// Override Url accessor to use ResolvedUrl if available | ||
string INavigationItem.Url => ResolvedUrl ?? Url; | ||
public CrossLinkNavigationItem(string url, string? title, DocumentationGroup group, bool hidden = false) | ||
{ | ||
_url = url; | ||
NavigationTitle = title ?? GetNavigationTitleFromUrl(url); | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Parent = group; | ||
NavigationRoot = group.NavigationRoot; | ||
Hidden = hidden; | ||
} | ||
|
||
private string GetNavigationTitleFromUrl(string url) | ||
{ | ||
// Extract a decent title from the URL | ||
try | ||
{ | ||
if (Uri.TryCreate(url, UriKind.Absolute, out var uri)) | ||
{ | ||
// Get the last segment of the path and remove extension | ||
var lastSegment = uri.AbsolutePath.Split('/').Last(); | ||
lastSegment = Path.GetFileNameWithoutExtension(lastSegment); | ||
|
||
// Convert to title case (simple version) | ||
if (!string.IsNullOrEmpty(lastSegment)) | ||
{ | ||
var words = lastSegment.Replace('-', ' ').Replace('_', ' ').Split(' '); | ||
var titleCase = string.Join(" ", words.Select(w => | ||
string.IsNullOrEmpty(w) ? "" : char.ToUpper(w[0]) + w[1..].ToLowerInvariant())); | ||
return titleCase; | ||
} | ||
} | ||
} | ||
catch | ||
{ | ||
// Fall back to URL if parsing fails | ||
} | ||
|
||
return url; | ||
} | ||
|
||
public INodeNavigationItem<INavigationModel, INavigationItem>? Parent { get; set; } | ||
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot { get; } | ||
// Original URL from the cross-link | ||
private readonly string _url; | ||
|
||
// Store resolved URL for rendering | ||
public string? ResolvedUrl { get; set; } | ||
|
||
// Implement the INavigationItem.Url property to use ResolvedUrl if available | ||
public string Url => ResolvedUrl ?? _url; public string NavigationTitle { get; } | ||
public int NavigationIndex { get; set; } | ||
public bool Hidden { get; } | ||
public INavigationModel Model => null!; // Cross-link has no local model | ||
} |
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
94 changes: 94 additions & 0 deletions
94
src/Elastic.Markdown/IO/Navigation/NavigationCrossLinkValidator.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,94 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Elastic.Documentation.Site.Navigation; | ||
using Elastic.Markdown.Links.CrossLinks; | ||
|
||
namespace Elastic.Markdown.IO.Navigation; | ||
|
||
public static class NavigationCrossLinkValidator | ||
{ | ||
public static async Task ValidateNavigationCrossLinksAsync( | ||
INavigationItem root, | ||
ICrossLinkResolver crossLinkResolver, | ||
Action<string> errorEmitter) | ||
{ | ||
// Ensure cross-links are fetched before validation | ||
_ = await crossLinkResolver.FetchLinks(new Cancel()); | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Collect all navigation items that contain cross-repo links | ||
var itemsWithCrossLinks = FindNavigationItemsWithCrossLinks(root); | ||
|
||
foreach (var item in itemsWithCrossLinks) | ||
{ | ||
if (item is CrossLinkNavigationItem crossLinkItem) | ||
{ | ||
var url = crossLinkItem.Url; | ||
if (url != null && Uri.TryCreate(url, UriKind.Absolute, out var crossUri) && | ||
crossUri.Scheme != "http" && crossUri.Scheme != "https") | ||
{ | ||
// Try to resolve the cross-link URL | ||
if (crossLinkResolver.TryResolve(errorEmitter, crossUri, out var resolvedUri)) | ||
{ | ||
// If resolved successfully, set the resolved URL | ||
crossLinkItem.ResolvedUrl = resolvedUri.ToString(); | ||
} | ||
else | ||
{ | ||
// Error already emitted by CrossLinkResolver | ||
// But we won't fail the build - just display the original URL | ||
} | ||
} | ||
} | ||
else if (item is FileNavigationItem fileItem && | ||
fileItem.Url != null && | ||
Uri.TryCreate(fileItem.Url, UriKind.Absolute, out var fileUri) && | ||
fileUri.Scheme != "http" && | ||
fileUri.Scheme != "https") | ||
{ | ||
// Cross-link URL detected in a FileNavigationItem, but we're not validating it yet | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
private static List<INavigationItem> FindNavigationItemsWithCrossLinks(INavigationItem item) | ||
{ | ||
var results = new List<INavigationItem>(); | ||
|
||
// Check if this item has a cross-link | ||
if (item is CrossLinkNavigationItem crossLinkItem) | ||
{ | ||
var url = crossLinkItem.Url; | ||
if (url != null && | ||
Uri.TryCreate(url, UriKind.Absolute, out var uri) && | ||
uri.Scheme != "http" && | ||
uri.Scheme != "https") | ||
{ | ||
results.Add(item); | ||
} | ||
} | ||
else if (item is FileNavigationItem fileItem && | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fileItem.Url != null && | ||
Uri.TryCreate(fileItem.Url, UriKind.Absolute, out var fileUri) && | ||
fileUri.Scheme != "http" && | ||
fileUri.Scheme != "https") | ||
{ | ||
results.Add(item); | ||
} // Recursively check children if this is a container | ||
if (item is INodeNavigationItem<INavigationModel, INavigationItem> containerItem) | ||
{ | ||
foreach (var child in containerItem.NavigationItems) | ||
{ | ||
results.AddRange(FindNavigationItemsWithCrossLinks(child)); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
} |
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
Oops, something went wrong.
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.