Skip to content

Commit a074566

Browse files
Copilottheletterf
andauthored
Allow button directives to use Markdown link references (#3443)
* Initial plan * fix: allow button directives to use link references --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Fabrizio Ferri-Benedetti <fabri.ferribenedetti@elastic.co>
1 parent 13b4e91 commit a074566

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/Elastic.Markdown/Myst/Directives/Button/ButtonBlock.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public partial class ButtonBlock(DirectiveBlockParser parser, ParserContext cont
5656
/// </summary>
5757
public bool IsInGroup { get; private set; }
5858

59-
// Regex to match a Markdown link: [text](url)
60-
[GeneratedRegex(@"^\s*\[[^\]]+\]\([^\)]+\)\s*$", RegexOptions.Singleline)]
59+
// Regex to match a single Markdown link, including reference-style links.
60+
[GeneratedRegex(@"^\s*\[[^\]]*\](?:\([^\)]+\)|\[[^\]]+\]|\[\])\s*$", RegexOptions.Singleline)]
6161
private static partial Regex LinkPattern();
6262

6363
public override void FinalizeAndValidate(ParserContext context)
@@ -94,14 +94,14 @@ private void ValidateContent()
9494

9595
if (string.IsNullOrWhiteSpace(content))
9696
{
97-
this.EmitError("Button directive requires a link. Use: :::{button}\n[text](url)\n:::");
97+
this.EmitError("Button directive requires a link. Use: :::{button}\n[text](url)\n:::\nOr: :::{button}\n[text][ref]\n:::");
9898
return;
9999
}
100100

101101
// Check if content matches the link pattern
102102
if (!LinkPattern().IsMatch(content))
103103
{
104-
this.EmitError("Button directive must contain only a single Markdown link. Use: :::{button}\n[text](url)\n:::");
104+
this.EmitError("Button directive must contain only a single Markdown link. Use: :::{button}\n[text](url)\n:::\nOr: :::{button}\n[text][ref]\n:::");
105105
}
106106
}
107107

tests/Elastic.Markdown.Tests/Directives/ButtonTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ public class ButtonExternalTests(ITestOutputHelper output) : DirectiveTest<Butto
8181
public void RendersNoopenerNoreferrer() => Html.Should().Contain("rel=\"noopener noreferrer\"");
8282
}
8383

84+
public class ButtonReferenceLinkTests(ITestOutputHelper output) : DirectiveTest<ButtonBlock>(output,
85+
"""
86+
:::{button}
87+
[Open][kibana-url]
88+
:::
89+
90+
[kibana-url]: <https://foo.example.com>
91+
"""
92+
)
93+
{
94+
[Fact]
95+
public void RendersReferencedLinkHref() => Html.Should().Contain("href=\"https://foo.example.com\"");
96+
97+
[Fact]
98+
public void RendersButtonText() => Html.Should().Contain(">Open<");
99+
100+
[Fact]
101+
public void EmitsNoErrors() => Collector.Diagnostics.Should().BeEmpty();
102+
}
103+
84104
public class ButtonInvalidTypeTests(ITestOutputHelper output) : DirectiveTest<ButtonBlock>(output,
85105
"""
86106
:::{button}

0 commit comments

Comments
 (0)