Skip to content

Commit 5489ab3

Browse files
committed
Fix callouts in console API payload
1 parent 90aba87 commit 5489ab3

File tree

6 files changed

+418
-2
lines changed

6 files changed

+418
-2
lines changed

docs/testing/index.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,31 @@ POST _reindex
6464
GET metricbeat-2016.05.30-1/_doc/1
6565
GET metricbeat-2016.05.31-1/_doc/1
6666
```
67+
68+
```console
69+
PUT my-index-000001
70+
{
71+
"mappings": {
72+
"enabled": false <1>
73+
}
74+
}
75+
76+
PUT my-index-000001/_doc/session_1
77+
{
78+
"user_id": "kimchy",
79+
"session_data": {
80+
"arbitrary_object": {
81+
"some_array": [ "foo", "bar", { "baz": 2 } ]
82+
}
83+
},
84+
"last_updated": "2015-12-06T18:20:22"
85+
}
86+
87+
GET my-index-000001/_doc/session_1 <2>
88+
89+
GET my-index-000001/_mapping <3>
90+
```
91+
92+
1. The entire mapping is disabled.
93+
2. The document can be retrieved.
94+
3. Checking the mapping reveals that no fields have been added.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
@foreach (var segment in Model.ApiSegments)
1515
{
1616
<code class="language-apiheader">@segment.Header@(Model.RenderConsoleCallouts(segment.LineNumber))</code>
17-
@if (segment.ContentLines.Count > 0)
17+
@if (segment.ContentLinesWithNumbers.Count > 0)
1818
{
19-
<code class="language-json">@string.Join("\n", segment.ContentLines)</code>
19+
<code class="language-json">@(Model.RenderContentLinesWithCallouts(segment.ContentLinesWithNumbers))</code>
2020
}
2121
}
2222
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,41 @@ public HtmlString RenderConsoleCallouts(int lineNumber)
4646
}
4747
return new HtmlString(html.ToString());
4848
}
49+
50+
public HtmlString RenderContentLinesWithCallouts(List<(string Content, int LineNumber)> contentLinesWithNumbers)
51+
{
52+
if (EnhancedCodeBlock?.CallOuts == null || contentLinesWithNumbers.Count == 0)
53+
return new HtmlString(string.Join("\n", contentLinesWithNumbers.Select(c => c.Content)));
54+
55+
var html = new System.Text.StringBuilder();
56+
for (var i = 0; i < contentLinesWithNumbers.Count; i++)
57+
{
58+
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+
}
73+
74+
if (i > 0)
75+
_ = html.Append('\n');
76+
_ = html.Append(line);
77+
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+
}
83+
}
84+
return new HtmlString(html.ToString());
85+
}
4986
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ApiSegment
1515
public string Header { get; set; } = "";
1616
public List<string> ContentLines { get; set; } = [];
1717
public int LineNumber { get; set; }
18+
public List<(string Content, int LineNumber)> ContentLinesWithNumbers { get; set; } = [];
1819
}
1920

2021
public class EnhancedCodeBlock(BlockParser parser, ParserContext context)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ private static void ProcessConsoleCodeBlock(
349349
else
350350
{
351351
if (!string.IsNullOrEmpty(lineText.Trim()))
352+
{
352353
currentSegment.ContentLines.Add(lineText);
354+
currentSegment.ContentLinesWithNumbers.Add((lineText, originatingLine));
355+
}
353356

354357
if (codeBlockArgs.UseCallouts && codeBlock.OpeningFencedCharCount <= 3)
355358
ProcessCalloutsForLine(span, codeBlock, ref callOutIndex, originatingLine);

0 commit comments

Comments
 (0)