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
2 changes: 2 additions & 0 deletions src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class EnhancedCodeBlock(BlockParser parser, ParserContext context)

public List<CallOut>? CallOuts { get; set; }

public IReadOnlyCollection<CallOut> UniqueCallOuts => CallOuts?.DistinctBy(c => c.Index).ToList() ?? [];

public bool InlineAnnotations { get; set; }

public string Language { get; set; } = "unknown";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static void RenderRazorSlice<T>(RazorSlice<T> slice, HtmlRenderer render
}
protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
{
var callOuts = block.CallOuts ?? [];
var callOuts = block.UniqueCallOuts;

var slice = Code.Create(new CodeViewModel
{
Expand All @@ -46,7 +46,7 @@ protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
var siblingBlock = block.Parent[index + 1];
if (siblingBlock is not ListBlock)
block.EmitError("Code block with annotations is not followed by a list");
if (siblingBlock is ListBlock l && l.Count != callOuts.Count)
if (siblingBlock is ListBlock l && l.Count < callOuts.Count)
{
block.EmitError(
$"Code block has {callOuts.Count} callouts but the following list only has {l.Count}");
Expand Down Expand Up @@ -84,7 +84,7 @@ protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
else if (block.InlineAnnotations)
{
renderer.WriteLine("<ol class=\"code-callouts\">");
foreach (var c in block.CallOuts ?? [])
foreach (var c in block.UniqueCallOuts)
{
renderer.WriteLine("<li>");
renderer.WriteLine(c.Text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,14 @@ public override bool Close(BlockProcessor processor, Block block)

callOutIndex++;
var callout = span.Slice(match.Index + startIndex, match.Length - startIndex);
var index = callOutIndex;
if (!inlineCodeAnnotation && int.TryParse(callout.Trim(['<', '>']), out index))
{

}
return new CallOut
{
Index = callOutIndex,
Index = index,
Text = callout.TrimStart('/').TrimStart('#').TrimStart().ToString(),
InlineCodeAnnotation = inlineCodeAnnotation,
SliceStart = startIndex,
Expand Down
29 changes: 29 additions & 0 deletions tests/Elastic.Markdown.Tests/CodeBlocks/CallOutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ public void RequiresContentToFollow() => Collector.Diagnostics.Should().HaveCoun
.And.OnlyContain(c => c.Message.StartsWith("Code block has 2 callouts but the following list only has 1"));
}

public class ClassicCallOutsReuseHighlights(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2; <2>
var z = y - 2; <2>
""",
"""
1. The first
2. The second appears twice
"""

)
{
[Fact]
public void SeesTwoUniqueCallouts() => Block!.UniqueCallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith("<"));

[Fact]
public void ParsesAllForLineInformation() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(3)
.And.OnlyContain(c => c.Text.StartsWith("<"));

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

public class ClassicCallOutWithTheRightListItems(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
Expand Down
Loading