-
Notifications
You must be signed in to change notification settings - Fork 32
Add llms.txt template and generator #1928
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 35 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
aadd904
Add boilerplate
theletterf f5abcde
Add H2 generator
theletterf 73de9d8
Add absolute URLs generation
theletterf e0885a8
Add line
theletterf 6621545
Remove line - sigh
theletterf a875ce4
No autogenerated summaries for now
theletterf 69b7055
Update src/Elastic.Markdown/Exporters/LlmMarkdownExporter.cs
theletterf 0b1b4c6
Update src/tooling/docs-assembler/Cli/RepositoryCommands.cs
theletterf 9abc8cd
Update src/tooling/docs-assembler/Navigation/LlmsNavigationEnhancer.cs
theletterf 7efadd4
Merge branch 'main' into add-llmstxt-template
theletterf 50b364a
Fix errors
theletterf b6946de
List implementations of INavigationItem and throw exception
theletterf bcc0c95
Refactor line 79
theletterf 150fcba
Reuse and extend existing MakeAbsoluteUrl method
theletterf b490533
Remove redundant mapping
theletterf 4b30e62
Add method to extract best title
theletterf a1c2937
Make links absolute in the boilerplate
theletterf a012b67
Fix file generation
theletterf ffe6641
Refactor method
theletterf 2851ef7
Restore file
theletterf 2ff5573
Restore file from source
theletterf 4656514
Add newline
theletterf e684ff4
Merge branch 'main' into add-llmstxt-template
theletterf 769acca
Make all absolute LLM links use md terminations
theletterf e79b8ad
Merge branch 'main' into add-llmstxt-template
theletterf dd4efcf
Merge branch 'main' into add-llmstxt-template
theletterf 4a6948e
Handle crosslinks
theletterf bc50a2e
Simplify logic
theletterf 73108e3
Make method more general for all file extensions
theletterf 8386e11
Update src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRender…
theletterf 3ea3d51
Remove md append logic
theletterf dab231e
Merge branch 'main' into add-llmstxt-template
theletterf 9026583
Remove md extension from boilerplate
theletterf 876e739
Merge branch 'add-llmstxt-template' of github.com:elastic/docs-builde…
theletterf e01ff5c
Merge branch 'main' into add-llmstxt-template
theletterf 57db0f1
Apply suggestion
theletterf 410960c
Update src/Elastic.Markdown/Exporters/LlmMarkdownExporter.cs
theletterf 24f7a13
Update src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmInlineRende…
theletterf 583a1db
Merge branch 'main' into add-llmstxt-template
theletterf e9ce69d
Apply fix for boilerplate
theletterf dc46603
Merge branch 'main' into add-llmstxt-template
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
111 changes: 111 additions & 0 deletions
111
src/services/Elastic.Documentation.Assembler/Navigation/LlmsNavigationEnhancer.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,111 @@ | ||
// 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.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using Elastic.Documentation.Assembler; | ||
using Elastic.Documentation.Assembler.Navigation; | ||
using Elastic.Documentation.Site.Navigation; | ||
using Elastic.Markdown.IO; | ||
using Elastic.Markdown.IO.Navigation; | ||
using Elastic.Markdown.Myst.Renderers.LlmMarkdown; | ||
|
||
namespace Elastic.Documentation.Assembler.Navigation; | ||
|
||
/// <summary> | ||
/// Generates enhanced navigation sections for the llms.txt file | ||
/// </summary> | ||
public class LlmsNavigationEnhancer | ||
{ | ||
public string GenerateNavigationSections(GlobalNavigation navigation, Uri canonicalBaseUrl) | ||
{ | ||
var content = new StringBuilder(); | ||
|
||
// Get top-level navigation items (excluding hidden ones) | ||
var topLevelItems = navigation.TopLevelItems.Where(item => !item.Hidden).ToArray(); | ||
|
||
foreach (var topLevelItem in topLevelItems) | ||
{ | ||
if (topLevelItem is not DocumentationGroup group) | ||
continue; | ||
|
||
// Create H2 section for the category - use H1 title if available, fallback to navigation title | ||
var categoryTitle = GetBestTitle(group); | ||
_ = content.AppendLine(CultureInfo.InvariantCulture, $"## {categoryTitle}"); | ||
_ = content.AppendLine(); | ||
|
||
// Get first-level children | ||
var firstLevelChildren = GetFirstLevelChildren(group); | ||
|
||
if (firstLevelChildren.Any()) | ||
{ | ||
foreach (var child in firstLevelChildren) | ||
{ | ||
var title = GetBestTitle(child); | ||
var url = LlmRenderingHelpers.MakeAbsoluteUrl(canonicalBaseUrl, child.Url); | ||
var description = GetDescription(child); | ||
|
||
_ = !string.IsNullOrEmpty(description) | ||
? content.AppendLine(CultureInfo.InvariantCulture, $"* [{title}]({url}): {description}") | ||
: content.AppendLine(CultureInfo.InvariantCulture, $"* [{title}]({url})"); | ||
} | ||
_ = content.AppendLine(); | ||
} | ||
} | ||
|
||
return content.ToString(); | ||
} | ||
|
||
|
||
private static IEnumerable<INavigationItem> GetFirstLevelChildren(DocumentationGroup group) => | ||
group.NavigationItems.Where(i => !i.Hidden); | ||
|
||
/// <summary> | ||
/// Gets the best title for a navigation item, preferring H1 content over navigation title | ||
/// </summary> | ||
private static string GetBestTitle(INavigationItem navigationItem) => navigationItem switch | ||
{ | ||
// For file navigation items, prefer the H1 title from the markdown content | ||
FileNavigationItem fileItem when !string.IsNullOrEmpty(fileItem.Model.Title) | ||
=> fileItem.Model.Title, | ||
FileNavigationItem fileItem | ||
=> fileItem.NavigationTitle, | ||
|
||
// For documentation groups, prefer the H1 title from the index file | ||
DocumentationGroup group when !string.IsNullOrEmpty(group.Index?.Title) | ||
=> group.Index.Title, | ||
DocumentationGroup group | ||
=> group.NavigationTitle, | ||
|
||
// For other navigation item types, use the navigation title | ||
_ => navigationItem.NavigationTitle | ||
}; | ||
|
||
private static string? GetDescription(INavigationItem navigationItem) => navigationItem switch | ||
{ | ||
// For file navigation items, extract from frontmatter | ||
FileNavigationItem fileItem when fileItem.Model is MarkdownFile markdownFile | ||
=> markdownFile.YamlFrontMatter?.Description, | ||
|
||
// For documentation groups, try to get from index file | ||
DocumentationGroup group when group.Index is MarkdownFile indexFile | ||
=> indexFile.YamlFrontMatter?.Description, | ||
|
||
// For table of contents trees (inherits from DocumentationGroup, but handled explicitly) | ||
TableOfContentsTree tocTree when tocTree.Index is MarkdownFile indexFile | ||
=> indexFile.YamlFrontMatter?.Description, | ||
|
||
// Cross-repository links don't have descriptions in frontmatter | ||
CrossLinkNavigationItem => null, | ||
|
||
// API-related navigation items (these don't have markdown frontmatter) | ||
// Check by namespace to avoid direct assembly references | ||
INavigationItem item when item.GetType().FullName?.StartsWith("Elastic.ApiExplorer.", StringComparison.Ordinal) == true => null, | ||
|
||
// Throw exception for any unhandled navigation item types | ||
_ => throw new InvalidOperationException($"Unhandled navigation item type: {navigationItem.GetType().FullName}") | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} |
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.