Skip to content

Commit 150fcba

Browse files
committed
Reuse and extend existing MakeAbsoluteUrl method
1 parent bcc0c95 commit 150fcba

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
6060
return url;
6161
}
6262
}
63+
64+
/// <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.
67+
/// </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)
72+
{
73+
if (string.IsNullOrEmpty(url))
74+
return url;
75+
76+
baseUrl ??= "https://www.elastic.co";
77+
78+
// Convert HTML URLs to .md URLs for LLM consumption
79+
// e.g., "/docs/solutions/search/" -> "https://www.elastic.co/docs/solutions/search.md"
80+
var cleanUrl = url.TrimStart('/');
81+
82+
// Remove "docs/" prefix if present for the markdown filename
83+
var markdownPath = cleanUrl;
84+
if (markdownPath.StartsWith("docs/", StringComparison.Ordinal))
85+
markdownPath = markdownPath.Substring(5);
86+
87+
// Convert directory URLs to .md files
88+
if (markdownPath.EndsWith('/'))
89+
markdownPath = markdownPath.TrimEnd('/') + ".md";
90+
else if (!markdownPath.EndsWith(".md", StringComparison.Ordinal))
91+
markdownPath += ".md";
92+
93+
// Make absolute URL using the canonical base URL
94+
return $"{baseUrl.TrimEnd('/')}/docs/{markdownPath}";
95+
}
6396
}
6497

6598
/// <summary>

src/tooling/docs-assembler/Navigation/LlmsNavigationEnhancer.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Elastic.Documentation.Site.Navigation;
1111
using Elastic.Markdown.IO;
1212
using Elastic.Markdown.IO.Navigation;
13+
using Elastic.Markdown.Myst.Renderers.LlmMarkdown;
1314

1415
namespace Documentation.Assembler.Navigation;
1516

@@ -43,7 +44,7 @@ public string GenerateNavigationSections(GlobalNavigation navigation)
4344
foreach (var child in firstLevelChildren)
4445
{
4546
var title = child.NavigationTitle;
46-
var url = ConvertToAbsoluteMarkdownUrl(child.Url);
47+
var url = LlmRenderingHelpers.ConvertToAbsoluteMarkdownUrl(child.Url);
4748
var description = GetDescription(child);
4849

4950
_ = !string.IsNullOrEmpty(description)
@@ -77,27 +78,6 @@ private static string GetCategoryDisplayName(string navigationTitle) =>
7778
private static IEnumerable<INavigationItem> GetFirstLevelChildren(DocumentationGroup group) =>
7879
group.NavigationItems.Where(i => !i.Hidden);
7980

80-
private static string ConvertToAbsoluteMarkdownUrl(string url)
81-
{
82-
// Convert HTML URLs to .md URLs for LLM consumption
83-
// e.g., "/docs/solutions/search/" -> "https://www.elastic.co/docs/solutions/search.md"
84-
var cleanUrl = url.TrimStart('/');
85-
86-
// Remove "docs/" prefix if present for the markdown filename
87-
var markdownPath = cleanUrl;
88-
if (markdownPath.StartsWith("docs/", StringComparison.Ordinal))
89-
markdownPath = markdownPath.Substring(5);
90-
91-
// Convert directory URLs to .md files
92-
if (markdownPath.EndsWith('/'))
93-
markdownPath = markdownPath.TrimEnd('/') + ".md";
94-
else if (!markdownPath.EndsWith(".md", StringComparison.Ordinal))
95-
markdownPath += ".md";
96-
97-
// Make absolute URL using the canonical base URL (always https://www.elastic.co for production)
98-
var baseUrl = "https://www.elastic.co";
99-
return $"{baseUrl}/docs/{markdownPath}";
100-
}
10181

10282
private static string? GetDescription(INavigationItem navigationItem) => navigationItem switch
10383
{

0 commit comments

Comments
 (0)