Skip to content

Commit 3ea3d51

Browse files
committed
Remove md append logic
1 parent 8386e11 commit 3ea3d51

File tree

3 files changed

+12
-105
lines changed

3 files changed

+12
-105
lines changed

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

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,25 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
4040

4141
/// <summary>
4242
/// Converts relative URLs to absolute URLs using BuildContext.CanonicalBaseUrl for better LLM consumption.
43-
/// For documentation URLs, converts them to .md format for consistency with LLM-generated files.
43+
/// Also converts localhost URLs to canonical URLs.
4444
/// </summary>
4545
public static string? MakeAbsoluteUrl(LlmMarkdownRenderer renderer, string? url)
4646
{
4747
if (renderer.BuildContext.CanonicalBaseUrl == null)
4848
return url;
4949

50-
// For documentation URLs (starting with /docs/), convert to .md format for LLM consistency
51-
// BUT exclude files with extensions (images, PDFs, etc.) which should keep their original extensions
52-
if (!string.IsNullOrEmpty(url) && url.StartsWith("/docs/", StringComparison.Ordinal) && !HasFileExtension(url))
50+
// Convert localhost URLs to canonical URLs for LLM consumption
51+
if (!string.IsNullOrEmpty(url) && url.StartsWith("http://localhost", StringComparison.OrdinalIgnoreCase))
5352
{
54-
var markdownUrl = ConvertToMarkdownUrl(url);
55-
return MakeAbsoluteUrl(renderer.BuildContext.CanonicalBaseUrl, markdownUrl);
53+
if (Uri.TryCreate(url, UriKind.Absolute, out var localhostUri) &&
54+
localhostUri.AbsolutePath.StartsWith("/docs/", StringComparison.Ordinal))
55+
{
56+
// Replace localhost with canonical base URL
57+
var canonicalUrl = new Uri(renderer.BuildContext.CanonicalBaseUrl, localhostUri.AbsolutePath);
58+
return canonicalUrl.ToString();
59+
}
5660
}
5761

58-
// For other URLs (images, external links), use regular absolute URL conversion
5962
return MakeAbsoluteUrl(renderer.BuildContext.CanonicalBaseUrl, url);
6063
}
6164

@@ -81,68 +84,7 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
8184
}
8285
}
8386

84-
/// <summary>
85-
/// Converts documentation URLs to absolute markdown URLs for LLM consumption
86-
/// </summary>
87-
public static string MakeAbsoluteMarkdownUrl(Uri baseUri, string? url)
88-
{
89-
if (string.IsNullOrEmpty(url))
90-
return string.Empty;
91-
92-
// Convert to .md URL for LLM consumption
93-
var markdownUrl = ConvertToMarkdownUrl(url);
94-
95-
// Use the existing absolute URL logic
96-
return MakeAbsoluteUrl(baseUri, markdownUrl) ?? markdownUrl;
97-
}
9887

99-
/// <summary>
100-
/// Converts documentation URL paths to .md file paths for LLM consumption
101-
/// </summary>
102-
private static string ConvertToMarkdownUrl(string url)
103-
{
104-
var cleanUrl = url.TrimStart('/');
105-
106-
// Remove "docs/" prefix if present for the markdown filename, then add it back
107-
var markdownPath = cleanUrl;
108-
if (markdownPath.StartsWith("docs/", StringComparison.Ordinal))
109-
markdownPath = markdownPath.Substring(5);
110-
111-
// Convert directory URLs to .md files
112-
if (markdownPath.EndsWith('/'))
113-
markdownPath = markdownPath.TrimEnd('/') + ".md";
114-
else if (!markdownPath.EndsWith(".md", StringComparison.Ordinal))
115-
markdownPath += ".md";
116-
117-
return $"/docs/{markdownPath}";
118-
}
119-
120-
/// <summary>
121-
/// Checks if a URL path points to a file with an extension (not a documentation page)
122-
/// </summary>
123-
private static bool HasFileExtension(string path)
124-
{
125-
try
126-
{
127-
// Try to parse as URI to handle query parameters and fragments
128-
if (Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out var uri))
129-
{
130-
// Get the path component without query/fragment
131-
var pathOnly = uri.GetLeftPart(UriPartial.Path);
132-
var ext = Path.GetExtension(pathOnly);
133-
return !string.IsNullOrEmpty(ext);
134-
}
135-
136-
// Fallback to direct path parsing
137-
var extension = Path.GetExtension(path);
138-
return !string.IsNullOrEmpty(extension);
139-
}
140-
catch
141-
{
142-
// If parsing fails, assume it's a documentation page
143-
return false;
144-
}
145-
}
14688

14789
}
14890

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void Write(LlmMarkdownRenderer renderer, LinkInline obj)
3232
renderer.WriteChildren(obj);
3333
renderer.Writer.Write("](");
3434
var url = obj.GetDynamicUrl?.Invoke() ?? obj.Url;
35-
var absoluteUrl = ProcessLinkUrl(renderer, obj, url);
35+
var absoluteUrl = LlmRenderingHelpers.MakeAbsoluteUrl(renderer, url);
3636
renderer.Writer.Write(absoluteUrl ?? string.Empty);
3737
}
3838
if (!string.IsNullOrEmpty(obj.Title))
@@ -44,41 +44,6 @@ protected override void Write(LlmMarkdownRenderer renderer, LinkInline obj)
4444
renderer.Writer.Write(")");
4545
}
4646

47-
/// <summary>
48-
/// Processes link URLs with special handling for crosslinks to ensure they get .md treatment
49-
/// </summary>
50-
private static string? ProcessLinkUrl(LlmMarkdownRenderer renderer, LinkInline obj, string? url)
51-
{
52-
// Check if this is a crosslink (similar to HtmxLinkInlineRenderer)
53-
var isCrossLink = (obj.GetData("isCrossLink") as bool?) == true;
54-
55-
// Also check if this is a resolved crosslink to Elastic docs by URL pattern
56-
// This handles cases where crosslinks have been resolved but lost their metadata
57-
var isElasticDocsUrl = !string.IsNullOrEmpty(url) &&
58-
url.StartsWith("https://www.elastic.co/docs/", StringComparison.Ordinal);
59-
60-
if ((isCrossLink || isElasticDocsUrl) && !string.IsNullOrEmpty(url))
61-
{
62-
// For crosslinks that resolve to documentation URLs, use MakeAbsoluteMarkdownUrl
63-
// to ensure they get the .md treatment
64-
if (url.Contains("/docs/", StringComparison.Ordinal))
65-
{
66-
// Extract the docs path from the resolved URL
67-
var uri = Uri.TryCreate(url, UriKind.Absolute, out var parsedUri) ? parsedUri : null;
68-
if (uri != null && renderer.BuildContext.CanonicalBaseUrl != null)
69-
{
70-
var path = uri.AbsolutePath;
71-
if (path.StartsWith("/docs/", StringComparison.Ordinal))
72-
{
73-
return LlmRenderingHelpers.MakeAbsoluteMarkdownUrl(renderer.BuildContext.CanonicalBaseUrl, path);
74-
}
75-
}
76-
}
77-
}
78-
79-
// For regular links, use the standard absolute URL conversion
80-
return LlmRenderingHelpers.MakeAbsoluteUrl(renderer, url);
81-
}
8247

8348
}
8449

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public string GenerateNavigationSections(GlobalNavigation navigation, Uri canoni
4545
foreach (var child in firstLevelChildren)
4646
{
4747
var title = GetBestTitle(child);
48-
var url = LlmRenderingHelpers.MakeAbsoluteMarkdownUrl(canonicalBaseUrl, child.Url);
48+
var url = LlmRenderingHelpers.MakeAbsoluteUrl(canonicalBaseUrl, child.Url);
4949
var description = GetDescription(child);
5050

5151
_ = !string.IsNullOrEmpty(description)

0 commit comments

Comments
 (0)