Skip to content

Commit ffe6641

Browse files
committed
Refactor method
1 parent a012b67 commit ffe6641

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,26 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
4242
/// Converts relative URLs to absolute URLs using BuildContext.CanonicalBaseUrl for better LLM consumption
4343
/// </summary>
4444
public static string? MakeAbsoluteUrl(LlmMarkdownRenderer renderer, string? url)
45+
{
46+
if (renderer.BuildContext.CanonicalBaseUrl == null)
47+
return url;
48+
49+
return MakeAbsoluteUrl(renderer.BuildContext.CanonicalBaseUrl, url);
50+
}
51+
52+
/// <summary>
53+
/// Converts relative URLs to absolute URLs for LLM consumption
54+
/// </summary>
55+
public static string? MakeAbsoluteUrl(Uri? baseUri, string? url)
4556
{
4657
if (
4758
string.IsNullOrEmpty(url)
48-
|| renderer.BuildContext.CanonicalBaseUrl == null
59+
|| baseUri == null
4960
|| Uri.IsWellFormedUriString(url, UriKind.Absolute)
5061
|| !Uri.IsWellFormedUriString(url, UriKind.Relative))
5162
return url;
5263
try
5364
{
54-
var baseUri = renderer.BuildContext.CanonicalBaseUrl;
5565
var absoluteUri = new Uri(baseUri, url);
5666
return absoluteUri.ToString();
5767
}
@@ -62,24 +72,28 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
6272
}
6373

6474
/// <summary>
65-
/// Converts documentation URLs to absolute markdown URLs specifically for LLM consumption.
66-
/// Transforms HTML paths to .md file paths and ensures they're absolute URLs.
75+
/// Converts documentation URLs to absolute markdown URLs for LLM consumption
6776
/// </summary>
68-
/// <param name="url">The documentation URL to convert (e.g., "/docs/solutions/search/")</param>
69-
/// <param name="baseUrl">The base URL to use (defaults to "https://www.elastic.co")</param>
70-
/// <returns>Absolute markdown URL (e.g., "https://www.elastic.co/docs/solutions/search.md")</returns>
71-
public static string ConvertToAbsoluteMarkdownUrl(string url, string? baseUrl = null)
77+
public static string MakeAbsoluteMarkdownUrl(Uri baseUri, string? url)
7278
{
7379
if (string.IsNullOrEmpty(url))
74-
return url;
80+
return string.Empty;
81+
82+
// Convert to .md URL for LLM consumption
83+
var markdownUrl = ConvertToMarkdownUrl(url);
7584

76-
baseUrl ??= "https://www.elastic.co";
85+
// Use the existing absolute URL logic
86+
return MakeAbsoluteUrl(baseUri, markdownUrl) ?? markdownUrl;
87+
}
7788

78-
// Convert HTML URLs to .md URLs for LLM consumption
79-
// e.g., "/docs/solutions/search/" -> "https://www.elastic.co/docs/solutions/search.md"
89+
/// <summary>
90+
/// Converts documentation URL paths to .md file paths for LLM consumption
91+
/// </summary>
92+
private static string ConvertToMarkdownUrl(string url)
93+
{
8094
var cleanUrl = url.TrimStart('/');
8195

82-
// Remove "docs/" prefix if present for the markdown filename
96+
// Remove "docs/" prefix if present for the markdown filename, then add it back
8397
var markdownPath = cleanUrl;
8498
if (markdownPath.StartsWith("docs/", StringComparison.Ordinal))
8599
markdownPath = markdownPath.Substring(5);
@@ -90,9 +104,9 @@ public static string ConvertToAbsoluteMarkdownUrl(string url, string? baseUrl =
90104
else if (!markdownPath.EndsWith(".md", StringComparison.Ordinal))
91105
markdownPath += ".md";
92106

93-
// Make absolute URL using the canonical base URL
94-
return $"{baseUrl.TrimEnd('/')}/docs/{markdownPath}";
107+
return $"/docs/{markdownPath}";
95108
}
109+
96110
}
97111

98112
/// <summary>

src/services/Elastic.Documentation.Assembler/Building/AssemblerBuildService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ private static async Task EnhanceLlmsTxtFile(AssembleContext context, GlobalNavi
117117
return; // No llms.txt file to enhance
118118

119119
var existingContent = await readFs.File.ReadAllTextAsync(llmsTxtPath, ctx);
120-
var navigationSections = enhancer.GenerateNavigationSections(navigation);
120+
// Assembler always uses the production URL as canonical base URL
121+
var canonicalBaseUrl = new Uri("https://www.elastic.co");
122+
var navigationSections = enhancer.GenerateNavigationSections(navigation, canonicalBaseUrl);
121123

122124
// Append the navigation sections to the existing boilerplate
123125
var enhancedContent = existingContent + Environment.NewLine + navigationSections;

src/services/Elastic.Documentation.Assembler/Navigation/LlmsNavigationEnhancer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Elastic.Documentation.Assembler.Navigation;
2020
/// </summary>
2121
public class LlmsNavigationEnhancer
2222
{
23-
public string GenerateNavigationSections(GlobalNavigation navigation)
23+
public string GenerateNavigationSections(GlobalNavigation navigation, Uri canonicalBaseUrl)
2424
{
2525
var content = new StringBuilder();
2626

@@ -45,7 +45,7 @@ public string GenerateNavigationSections(GlobalNavigation navigation)
4545
foreach (var child in firstLevelChildren)
4646
{
4747
var title = GetBestTitle(child);
48-
var url = LlmRenderingHelpers.ConvertToAbsoluteMarkdownUrl(child.Url);
48+
var url = LlmRenderingHelpers.MakeAbsoluteMarkdownUrl(canonicalBaseUrl, child.Url);
4949
var description = GetDescription(child);
5050

5151
_ = !string.IsNullOrEmpty(description)

0 commit comments

Comments
 (0)