Don't autolink URLs used as markdown link text#3319
Conversation
📝 WalkthroughWalkthroughThis PR prevents nested anchor markup by having HtmxLinkInlineRenderer detect when a LinkInline is inside another LinkInline and, when detected, render the link's children directly instead of emitting an outer . Regression tests were added: one for a URL used as the complete link text, one for a URL appearing inside surrounding link text, and one ensuring an image inside a link still renders the outer anchor and the image. All tests assert no diagnostics. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The parser-level check only caught the case where the autolink URL appeared immediately after `[`. Once any literal text intervened, the LinkDelimiterInline walk-back missed it. Detecting nested links in the renderer instead works for every parse shape. Co-authored-by: Cursor <cursoragent@cursor.com>
Replace fragile exact-string NotContain with NotMatchRegex to catch nested <a> regardless of attribute ordering. Add ImageInsideLinkTests to confirm the IsImage branch is unaffected by the IsNestedInsideLink guard. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/Elastic.Markdown.Tests/Inline/AutoLinkTests.cs (1)
255-279: ⚡ Quick winRename new test methods to
Method_Scenario_Expected.The newly added test method names don’t follow the required test naming convention. Please rename them to the
Method_Scenario_Expectedformat for consistency with the project standard.As per coding guidelines, "Test method naming convention: Method_Scenario_Expected."
Also applies to: 288-297
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/Elastic.Markdown.Tests/Inline/AutoLinkTests.cs` around lines 255 - 279, Rename the test methods to follow the Method_Scenario_Expected convention: change DoesNotCreateNestedAnchor to a name like RenderLink_WhenLinkContainsUrl_NoNestedAnchor (or similar Method_Scenario_Expected wording) and change DoesNotAutolinkUrlInsideLinkText to a name like RenderLink_WhenLinkTextContainsUrl_NoAutolink; also rename the HasNoErrors methods in both classes to something like Collector_HasNoDiagnostics_ZeroCount to match the same convention. Update the method identifiers (e.g., DoesNotCreateNestedAnchor, DoesNotAutolinkUrlInsideLinkText, and both HasNoErrors) to the new Method_Scenario_Expected names throughout the file and any references.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/Elastic.Markdown.Tests/Inline/AutoLinkTests.cs`:
- Around line 255-279: Rename the test methods to follow the
Method_Scenario_Expected convention: change DoesNotCreateNestedAnchor to a name
like RenderLink_WhenLinkContainsUrl_NoNestedAnchor (or similar
Method_Scenario_Expected wording) and change DoesNotAutolinkUrlInsideLinkText to
a name like RenderLink_WhenLinkTextContainsUrl_NoAutolink; also rename the
HasNoErrors methods in both classes to something like
Collector_HasNoDiagnostics_ZeroCount to match the same convention. Update the
method identifiers (e.g., DoesNotCreateNestedAnchor,
DoesNotAutolinkUrlInsideLinkText, and both HasNoErrors) to the new
Method_Scenario_Expected names throughout the file and any references.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 27f445c5-2bf3-4ab8-8f3f-f44285c37baa
📒 Files selected for processing (1)
tests/Elastic.Markdown.Tests/Inline/AutoLinkTests.cs
Why
When a URL is used as both the visible text and the target of a markdown link — e.g. https://gist.github.com — the page renders two external-link icons. The custom bare-URL autolinker runs before the standard link parser, so it matches the URL inside the [...] text and produces a nested . The CSS rule that paints the external icon then fires on both anchors. Closes #3317.
Chose to do this through a code fix rather than fixing instances because sometimes the URL link text is the most accurate + making it a bare link is busywork + the problem is likely to recur. the most expedient fix is in the code
What
HtmxLinkInlineRenderer now walks the parent chain of each LinkInline it's about to write; if any ancestor is also a LinkInline, it skips the wrapping and just renders the children. The outer link keeps its target="_blank" + external icon; the inner autolinked URL becomes plain text inside it. One anchor, one icon, valid HTML.
Adds two regression tests covering the exact issue case and a URL embedded mid-text inside link text.
Made with Cursor