Skip to content

Commit 4a6948e

Browse files
committed
Handle crosslinks
1 parent dd4efcf commit 4a6948e

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,24 @@ private static string ConvertToMarkdownUrl(string url)
107107
if (markdownPath.StartsWith("docs/", StringComparison.Ordinal))
108108
markdownPath = markdownPath.Substring(5);
109109

110-
// Convert directory URLs to .md files
110+
// Convert directory URLs to .md files, but don't convert image files
111111
if (markdownPath.EndsWith('/'))
112112
markdownPath = markdownPath.TrimEnd('/') + ".md";
113-
else if (!markdownPath.EndsWith(".md", StringComparison.Ordinal))
113+
else if (!markdownPath.EndsWith(".md", StringComparison.Ordinal) && !IsImageFile(markdownPath))
114114
markdownPath += ".md";
115115

116116
return $"/docs/{markdownPath}";
117117
}
118118

119+
/// <summary>
120+
/// Checks if a URL path points to an image file
121+
/// </summary>
122+
private static bool IsImageFile(string path)
123+
{
124+
var extension = Path.GetExtension(path).ToLowerInvariant();
125+
return extension is ".png" or ".jpg" or ".jpeg" or ".gif" or ".svg" or ".webp" or ".bmp" or ".ico";
126+
}
127+
119128
}
120129

121130
/// <summary>

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

Lines changed: 38 additions & 1 deletion
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 = LlmRenderingHelpers.MakeAbsoluteUrl(renderer, url);
35+
var absoluteUrl = ProcessLinkUrl(renderer, obj, url);
3636
renderer.Writer.Write(absoluteUrl ?? string.Empty);
3737
}
3838
if (!string.IsNullOrEmpty(obj.Title))
@@ -43,6 +43,43 @@ protected override void Write(LlmMarkdownRenderer renderer, LinkInline obj)
4343
}
4444
renderer.Writer.Write(")");
4545
}
46+
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+
}
82+
4683
}
4784

4885
public class LlmEmphasisInlineRenderer : MarkdownObjectRenderer<LlmMarkdownRenderer, EmphasisInline>

0 commit comments

Comments
 (0)