Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/Elastic.Markdown/Myst/Directives/Button/ButtonBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public partial class ButtonBlock(DirectiveBlockParser parser, ParserContext cont
/// </summary>
public bool IsInGroup { get; private set; }

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

public override void FinalizeAndValidate(ParserContext context)
Expand Down Expand Up @@ -94,14 +94,14 @@ private void ValidateContent()

if (string.IsNullOrWhiteSpace(content))
{
this.EmitError("Button directive requires a link. Use: :::{button}\n[text](url)\n:::");
this.EmitError("Button directive requires a link. Use: :::{button}\n[text](url)\n:::\nOr: :::{button}\n[text][ref]\n:::");
return;
}

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

Expand Down
20 changes: 20 additions & 0 deletions tests/Elastic.Markdown.Tests/Directives/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ public class ButtonExternalTests(ITestOutputHelper output) : DirectiveTest<Butto
public void RendersNoopenerNoreferrer() => Html.Should().Contain("rel=\"noopener noreferrer\"");
}

public class ButtonReferenceLinkTests(ITestOutputHelper output) : DirectiveTest<ButtonBlock>(output,
"""
:::{button}
[Open][kibana-url]
:::

[kibana-url]: <https://foo.example.com>
"""
)
{
[Fact]
public void RendersReferencedLinkHref() => Html.Should().Contain("href=\"https://foo.example.com\"");

[Fact]
public void RendersButtonText() => Html.Should().Contain(">Open<");

[Fact]
public void EmitsNoErrors() => Collector.Diagnostics.Should().BeEmpty();
}

public class ButtonInvalidTypeTests(ITestOutputHelper output) : DirectiveTest<ButtonBlock>(output,
"""
:::{button}
Expand Down
Loading