Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 Elastic.Markdown.IO;
using Elastic.Markdown.Myst.InlineParsers.Substitution;
using Elastic.Markdown.Myst.Roles;
using Elastic.Markdown.Myst.Roles.Kbd;
Expand Down Expand Up @@ -32,8 +33,23 @@ protected override void Write(LlmMarkdownRenderer renderer, LinkInline obj)
renderer.WriteChildren(obj);
renderer.Writer.Write("](");
var url = obj.GetDynamicUrl?.Invoke() ?? obj.Url;
var absoluteUrl = LlmRenderingHelpers.MakeAbsoluteUrl(renderer, url);
renderer.Writer.Write(absoluteUrl ?? string.Empty);

// Check if this is an internal link to a markdown page
var isCrossLink = (obj.GetData("isCrossLink") as bool?) == true;
var hasTargetNavigationRoot = obj.GetData($"Target{nameof(MarkdownFile.NavigationRoot)}") != null;
var isInternalMarkdownLink = !isCrossLink && hasTargetNavigationRoot;

if (isInternalMarkdownLink)
{
// For internal markdown links, preserve the .md extension
renderer.Writer.Write(EnsureMarkdownExtension(url) ?? string.Empty);
}
else
{
// For external links and cross-links, make absolute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

cross-links are absolute, but should also get a .md extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 8b0f20c. Cross-links to markdown files now preserve the .md extension and render as relative paths (e.g., solutions/observability/apps/apm-server-binary.md) instead of absolute URLs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. It still needs to become a full URL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This doesn't look right. It still needs to become a full URL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 75e3a94. Cross-links to markdown files now become full URLs with .md extensions (e.g., https://docs-v3-preview.elastic.dev/elastic/docs-content/tree/main/solutions/observability/apps/apm-server-binary.md) instead of relative paths.

var absoluteUrl = LlmRenderingHelpers.MakeAbsoluteUrl(renderer, url);
renderer.Writer.Write(absoluteUrl ?? string.Empty);
}
}
if (!string.IsNullOrEmpty(obj.Title))
{
Expand All @@ -43,6 +59,25 @@ protected override void Write(LlmMarkdownRenderer renderer, LinkInline obj)
}
renderer.Writer.Write(")");
}

/// <summary>
/// Ensures the URL ends with .md extension for markdown links
/// </summary>
private static string? EnsureMarkdownExtension(string? url)
{
if (string.IsNullOrEmpty(url))
return url;

// If it already has .md extension, return as-is
if (url.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
return url;

// Convert absolute paths to relative paths for markdown links
var processedUrl = url.StartsWith('/') ? url.TrimStart('/') : url;

// Add .md extension to internal markdown links
return processedUrl + ".md";
}
}

public class LlmEmphasisInlineRenderer : MarkdownObjectRenderer<LlmMarkdownRenderer, EmphasisInline>
Expand Down
24 changes: 24 additions & 0 deletions tests/authoring/LlmMarkdown/LlmMarkdownOutput.fs
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,27 @@ sub:
markdown |> convertsToNewLLM """
## Hello, World!
"""

type ``links`` () =
static let generator = Setup.Generate [
Index """
This is a [link to another page](another-page.md).

This is an [external link](https://example.com).

This is a [cross-link](https://docs.elastic.co/some-page).
"""
Markdown "another-page.md" """
# Another Page

This is another page for testing internal links.
"""
]

[<Fact>]
let ``internal markdown links preserve .md extension while external links become absolute`` () =
generator |> convertsToNewLLM """
This is a [link to another page](another-page.md).
This is an [external link](https://example.com).
This is a [cross-link](https://docs.elastic.co/some-page).
"""