-
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
18
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.
+311
−11
Open
Changes from all commits
Commits
Show all changes
18 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 db1db5e
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf 2567bf4
Move routine
theletterf cb1d64a
Merge branch 'main' into crosslinks-in-toc-take-three
theletterf a19d49b
Fix resolution
theletterf 4f4ebbe
Merge branch 'crosslinks-in-toc-take-three' of github.com:elastic/doc…
theletterf 30ed149
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
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
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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
// 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, | ||
Cancel ctx = default) | ||
{ | ||
// Ensure cross-links are fetched before validation | ||
_ = await crossLinkResolver.FetchLinks(ctx); | ||
// 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); | ||
} | ||
} | ||
// 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.