Skip to content

Commit f5833d4

Browse files
authored
Fix copy-paste of code blocks with code callouts (#946)
* Fix copy-paste of code blocks with code callouts * Revert changes to copybutton.ts * Fix tests * Naming
1 parent 97ede38 commit f5833d4

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

docs/syntax/code.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,42 @@ var client = new ElasticsearchClient("<CLOUD_ID>", apiKey);
195195

196196
::::
197197

198+
#### Align callouts
199+
200+
You can align callouts with spaces.
201+
202+
::::{tab-set}
203+
204+
:::{tab-item} Output
205+
206+
```yaml
207+
foo: 1 <1>
208+
barbar: 2 <2>
209+
bazbazbaz: 3 <3>
210+
```
211+
212+
1. Foo
213+
2. Bar
214+
3. Baz
215+
216+
:::
217+
218+
:::{tab-item} Markdown
219+
````markdown
220+
```yaml
221+
foo: 1 <1>
222+
barbar: 2 <2>
223+
bazbazbaz: 3 <3>
224+
```
225+
226+
1. Foo
227+
2. Bar
228+
3. Baz
229+
````
230+
:::
231+
232+
::::
233+
198234
#### Disable callouts
199235
200236
You can disable callouts by adding a code block argument `callouts=false`.

src/Elastic.Markdown/Assets/markdown/code.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@apply text-grey-30 overflow-x-auto rounded-none border-0 p-6! text-sm;
1010
background-color: #22272e;
1111
mix-blend-mode: unset;
12+
line-height: 1.5em;
1213
}
1314
code:first-child {
1415
@apply rounded-t-sm;
@@ -23,8 +24,10 @@
2324

2425
pre code .code-callout {
2526
@apply ml-1;
26-
transform: translateY(-1px);
2727
user-select: none;
28+
&::after {
29+
content: attr(data-index);
30+
}
2831
}
2932

3033
ol.code-callouts {
@@ -48,7 +51,7 @@
4851

4952
pre code .code-callout,
5053
ol.code-callouts li::before {
51-
@apply bg-blue-elastic inline-flex size-5 items-center justify-center rounded-full font-mono text-xs! text-white!;
54+
@apply bg-blue-elastic inline-flex size-4.5 items-center justify-center rounded-full font-mono text-xs! text-white!;
5255
}
5356

5457
code {
@@ -57,7 +60,6 @@
5760
line-height: 1.4em;
5861
padding-left: 0.2em;
5962
padding-right: 0.2em;
60-
letter-spacing: 0.02em;
6163
text-decoration: inherit;
6264
font-weight: inherit;
6365
mix-blend-mode: multiply;

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,24 @@ private static void RenderCodeBlockLines(HtmlRenderer renderer, EnhancedCodeBloc
6161

6262
private static void RenderCodeBlockLine(HtmlRenderer renderer, EnhancedCodeBlock block, StringSlice slice, int lineNumber)
6363
{
64+
var originalLength = slice.Length;
65+
_ = slice.TrimEnd();
66+
var removedSpaces = originalLength - slice.Length;
6467
_ = renderer.WriteEscape(slice);
65-
RenderCallouts(renderer, block, lineNumber);
68+
RenderCallouts(renderer, block, lineNumber, removedSpaces);
6669
_ = renderer.WriteLine();
6770
}
6871

69-
private static void RenderCallouts(HtmlRenderer renderer, EnhancedCodeBlock block, int lineNumber)
72+
private static void RenderCallouts(HtmlRenderer renderer, EnhancedCodeBlock block, int lineNumber, int indent)
7073
{
7174
var callOuts = FindCallouts(block.CallOuts, lineNumber + 1);
7275
foreach (var callOut in callOuts)
73-
_ = renderer.Write($"<span class=\"code-callout\" data-index=\"{callOut.Index}\">{callOut.Index}</span>");
76+
{
77+
// This adds a span with the same width as the removed spaces
78+
// to ensure the callout number is aligned with the code
79+
_ = renderer.Write($"<span style=\"display: inline-block; width: {indent}ch\"></span>");
80+
_ = renderer.Write($"<span class=\"code-callout\" data-index=\"{callOut.Index}\"></span>");
81+
}
7482
}
7583

7684
private static IEnumerable<CallOut> FindCallouts(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public void RendersExpectedHtml() =>
121121
Html.ReplaceLineEndings().Should().Contain("""
122122
<div class="highlight-csharp notranslate">
123123
<div class="highlight">
124-
<pre><code class="language-csharp">var x = 1; <span class="code-callout" data-index="1">1</span>
124+
<pre><code class="language-csharp">var x = 1;<span style="display: inline-block; width: 1ch"></span><span class="code-callout" data-index="1"></span>
125125
var y = x - 2;
126-
var z = y - 2; <span class="code-callout" data-index="2">2</span>
126+
var z = y - 2;<span style="display: inline-block; width: 1ch"></span><span class="code-callout" data-index="2"></span>
127127
</code></pre>
128128
</div>
129129
</div>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ 1. This is a callout
9595
)
9696
{
9797
[Fact]
98-
public void Render() => Html.Should().Contain("<span class=\"code-callout\" data-index=\"1\">1</span>");
98+
public void Render() => Html.Should().Contain("<span class=\"code-callout\" data-index=\"1\"></span>");
9999

100100
[Fact]
101101
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);

0 commit comments

Comments
 (0)