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
36 changes: 36 additions & 0 deletions docs/syntax/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,42 @@ var client = new ElasticsearchClient("<CLOUD_ID>", apiKey);

::::

#### Align callouts

You can align callouts with spaces.

::::{tab-set}

:::{tab-item} Output

```yaml
foo: 1 <1>
barbar: 2 <2>
bazbazbaz: 3 <3>
```

1. Foo
2. Bar
3. Baz

:::

:::{tab-item} Markdown
````markdown
```yaml
foo: 1 <1>
barbar: 2 <2>
bazbazbaz: 3 <3>
```

1. Foo
2. Bar
3. Baz
````
:::

::::

#### Disable callouts

You can disable callouts by adding a code block argument `callouts=false`.
Expand Down
8 changes: 5 additions & 3 deletions src/Elastic.Markdown/Assets/markdown/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@apply text-grey-30 overflow-x-auto rounded-none border-0 p-6! text-sm;
background-color: #22272e;
mix-blend-mode: unset;
line-height: 1.5em;
}
code:first-child {
@apply rounded-t-sm;
Expand All @@ -23,8 +24,10 @@

pre code .code-callout {
@apply ml-1;
transform: translateY(-1px);
user-select: none;
&::after {
content: attr(data-index);
}
}

ol.code-callouts {
Expand All @@ -48,7 +51,7 @@

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

code {
Expand All @@ -57,7 +60,6 @@
line-height: 1.4em;
padding-left: 0.2em;
padding-right: 0.2em;
letter-spacing: 0.02em;
text-decoration: inherit;
font-weight: inherit;
mix-blend-mode: multiply;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,24 @@ private static void RenderCodeBlockLines(HtmlRenderer renderer, EnhancedCodeBloc

private static void RenderCodeBlockLine(HtmlRenderer renderer, EnhancedCodeBlock block, StringSlice slice, int lineNumber)
{
var originalLength = slice.Length;
_ = slice.TrimEnd();
var removedSpaces = originalLength - slice.Length;
_ = renderer.WriteEscape(slice);
RenderCallouts(renderer, block, lineNumber);
RenderCallouts(renderer, block, lineNumber, removedSpaces);
_ = renderer.WriteLine();
}

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

private static IEnumerable<CallOut> FindCallouts(
Expand Down
4 changes: 2 additions & 2 deletions tests/Elastic.Markdown.Tests/CodeBlocks/CallOutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public void RendersExpectedHtml() =>
Html.ReplaceLineEndings().Should().Contain("""
<div class="highlight-csharp notranslate">
<div class="highlight">
<pre><code class="language-csharp">var x = 1; <span class="code-callout" data-index="1">1</span>
<pre><code class="language-csharp">var x = 1;<span style="display: inline-block; width: 1ch"></span><span class="code-callout" data-index="1"></span>
var y = x - 2;
var z = y - 2; <span class="code-callout" data-index="2">2</span>
var z = y - 2;<span style="display: inline-block; width: 1ch"></span><span class="code-callout" data-index="2"></span>
</code></pre>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ 1. This is a callout
)
{
[Fact]
public void Render() => Html.Should().Contain("<span class=\"code-callout\" data-index=\"1\">1</span>");
public void Render() => Html.Should().Contain("<span class=\"code-callout\" data-index=\"1\"></span>");

[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
Expand Down
Loading