Skip to content

Commit

Permalink
Support unique callouts for enhanced code blocks (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Jan 15, 2025
1 parent 55f5768 commit af9c7e0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
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

0 comments on commit af9c7e0

Please sign in to comment.