Skip to content

Commit 9149434

Browse files
committed
Simplify
1 parent 5489ab3 commit 9149434

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/Elastic.Markdown/Myst/CodeBlocks/Code.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
@foreach (var segment in Model.ApiSegments)
1515
{
16-
<code class="language-apiheader">@segment.Header@(Model.RenderConsoleCallouts(segment.LineNumber))</code>
16+
<code class="language-apiheader">@(Model.RenderLineWithCallouts(segment.Header, segment.LineNumber))</code>
1717
@if (segment.ContentLinesWithNumbers.Count > 0)
1818
{
1919
<code class="language-json">@(Model.RenderContentLinesWithCallouts(segment.ContentLinesWithNumbers))</code>

src/Elastic.Markdown/Myst/CodeBlocks/CodeViewModel.cs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,51 @@ public HtmlString RenderBlock()
3030
return new HtmlString(result);
3131
}
3232

33-
public HtmlString RenderConsoleCallouts(int lineNumber)
33+
public HtmlString RenderLineWithCallouts(string content, int lineNumber)
3434
{
3535
if (EnhancedCodeBlock?.CallOuts == null)
36-
return HtmlString.Empty;
36+
return new HtmlString(content);
3737

3838
var callouts = EnhancedCodeBlock.CallOuts.Where(c => c.Line == lineNumber);
3939
if (!callouts.Any())
40-
return HtmlString.Empty;
40+
return new HtmlString(content);
4141

42+
var line = content;
4243
var html = new System.Text.StringBuilder();
44+
45+
// Remove callout markers from the line
46+
foreach (var callout in callouts)
47+
{
48+
var calloutPattern = $"<{callout.Index}>";
49+
line = line.Replace(calloutPattern, "");
50+
}
51+
line = line.TrimEnd();
52+
53+
_ = html.Append(line);
54+
55+
// Add callout HTML after the line
4356
foreach (var callout in callouts)
4457
{
4558
_ = html.Append($"<span class=\"code-callout\" data-index=\"{callout.Index}\"></span>");
4659
}
60+
4761
return new HtmlString(html.ToString());
4862
}
4963

5064
public HtmlString RenderContentLinesWithCallouts(List<(string Content, int LineNumber)> contentLinesWithNumbers)
5165
{
52-
if (EnhancedCodeBlock?.CallOuts == null || contentLinesWithNumbers.Count == 0)
53-
return new HtmlString(string.Join("\n", contentLinesWithNumbers.Select(c => c.Content)));
66+
if (contentLinesWithNumbers.Count == 0)
67+
return HtmlString.Empty;
5468

5569
var html = new System.Text.StringBuilder();
5670
for (var i = 0; i < contentLinesWithNumbers.Count; i++)
5771
{
5872
var (content, lineNumber) = contentLinesWithNumbers[i];
59-
var line = content;
60-
61-
// Find callouts for this line
62-
var callouts = EnhancedCodeBlock.CallOuts.Where(c => c.Line == lineNumber);
63-
if (callouts.Any())
64-
{
65-
// Remove callout markers from the line
66-
foreach (var callout in callouts)
67-
{
68-
var calloutPattern = $"<{callout.Index}>";
69-
line = line.Replace(calloutPattern, "");
70-
}
71-
line = line.TrimEnd();
72-
}
7373

7474
if (i > 0)
7575
_ = html.Append('\n');
76-
_ = html.Append(line);
7776

78-
// Add callout HTML after the line
79-
foreach (var callout in callouts)
80-
{
81-
_ = html.Append($"<span class=\"code-callout\" data-index=\"{callout.Index}\"></span>");
82-
}
77+
_ = html.Append(RenderLineWithCallouts(content, lineNumber));
8378
}
8479
return new HtmlString(html.ToString());
8580
}

tests/Elastic.Markdown.Tests/CodeBlocks/ConsoleCodeBlockTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public void RendersCalloutHtmlInConsoleCodeBlocks()
272272
EnhancedCodeBlock = Block
273273
};
274274

275-
var calloutHtml = viewModel.RenderConsoleCallouts(Block.ApiSegments[0].LineNumber);
275+
var calloutHtml = viewModel.RenderLineWithCallouts(Block.ApiSegments[0].Header, Block.ApiSegments[0].LineNumber);
276276
calloutHtml.Value.Should().Contain("code-callout");
277277
calloutHtml.Value.Should().Contain("data-index=\"1\"");
278278
}

0 commit comments

Comments
 (0)