Skip to content

Commit abeb409

Browse files
Copilottheletterf
andcommitted
Implement border option for images
Co-authored-by: theletterf <1773616+theletterf@users.noreply.github.com>
1 parent 6ebe255 commit abeb409

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

docs/syntax/images.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ Screenshots are images displayed with a box-shadow. Define a screenshot by addin
7777
:screenshot:
7878
:::
7979

80+
## Borders
81+
82+
Images can have a border to improve contrast with the page background. Add the `:border:` attribute to a block-level image directive to add a 1px border.
83+
84+
```markdown
85+
:::{image} /syntax/images/observability.png
86+
:alt: Elasticsearch with border
87+
:width: 400px
88+
:border:
89+
:::
90+
```
91+
92+
:::{image} /syntax/images/observability.png
93+
:alt: Elasticsearch with border
94+
:width: 400px
95+
:border:
96+
:::
97+
98+
The border is especially useful for screenshots with white or light backgrounds that might blend with the documentation page.
99+
100+
:::{note}
101+
The `:border:` option only applies to images used with the image directive. Inline images do not support borders.
102+
:::
103+
80104
## Inline images
81105

82106
```markdown

src/Elastic.Documentation.Site/Assets/markdown/images.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
.screenshot {
2222
@apply border-grey-20 bg-grey-10 border-1 p-4 shadow-md;
2323
}
24+
.border {
25+
@apply border-grey-20 border-1;
26+
}
2427
}
2528
}

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ private static void WriteImage(HtmlRenderer renderer, ImageBlock block)
126126
Target = block.Target,
127127
Width = block.Width,
128128
Screenshot = block.Screenshot,
129+
Border = block.Border,
129130
ImageUrl = imageUrl,
130131
});
131132
RenderRazorSlice(slice, renderer);
@@ -147,6 +148,7 @@ private static void WriteImageCarousel(HtmlRenderer renderer, ImageCarouselBlock
147148
Width = img.Width,
148149
Scale = img.Scale ?? string.Empty,
149150
Screenshot = img.Screenshot,
151+
Border = img.Border,
150152
Target = img.Target,
151153
ImageUrl = img.ImageUrl
152154
}).ToList(),
@@ -191,6 +193,7 @@ private static void WriteFigure(HtmlRenderer renderer, ImageBlock block)
191193
Target = block.Target,
192194
Width = block.Width,
193195
Screenshot = block.Screenshot,
196+
Border = block.Border,
194197
ImageUrl = imageUrl,
195198
});
196199
RenderRazorSlice(slice, renderer);

src/Elastic.Markdown/Myst/Directives/Image/ImageBlock.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class ImageBlock(DirectiveBlockParser parser, ParserContext context)
4545
/// </summary>
4646
public string? Screenshot { get; set; }
4747

48+
/// <summary>
49+
/// When set, adds a border to the image.
50+
/// </summary>
51+
public string? Border { get; set; }
52+
4853
/// <summary>
4954
/// The uniform scaling factor of the image. The default is “100 %”, i.e. no scaling.
5055
/// </summary>
@@ -88,6 +93,9 @@ public override void FinalizeAndValidate(ParserContext context)
8893
// Set Screenshot to "screenshot" if the :screenshot: option is present
8994
Screenshot = Prop("screenshot") != null ? "screenshot" : null;
9095

96+
// Set Border to "border" if the :border: option is present
97+
Border = Prop("border") != null ? "border" : null;
98+
9199
ExtractImageUrl(context);
92100
}
93101

src/Elastic.Markdown/Myst/Directives/Image/ImageView.cshtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
@inherits RazorSlice<Elastic.Markdown.Myst.Directives.Image.ImageViewModel>
2+
@{
3+
var cssClasses = string.Join(" ", new[] { Model.Screenshot, Model.Border }.Where(c => !string.IsNullOrEmpty(c)));
4+
}
25
<a class="reference internal image-reference" href="javascript:void(0)" onclick="document.getElementById('modal-@Model.UniqueImageId').style.display='flex'">
3-
<img loading="lazy" title="@Model.Title" alt="@(Model.Alt == string.Empty ? HtmlString.Empty : new HtmlString(Model.Alt))" src="@Model.ImageUrl" style="@Model.Style" class="@Model.Screenshot" />
6+
<img loading="lazy" title="@Model.Title" alt="@(Model.Alt == string.Empty ? HtmlString.Empty : new HtmlString(Model.Alt))" src="@Model.ImageUrl" style="@Model.Style" class="@cssClasses" />
47
@Model.RenderBlock()
58
</a>
69

src/Elastic.Markdown/Myst/Directives/Image/ImageViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class ImageViewModel : DirectiveViewModel
2626
? Guid.NewGuid().ToString("N")[..8] // fallback to a random ID if ImageUrl is null or empty
2727
: ShortId.Create(ImageUrl);
2828
public required string? Screenshot { get; init; }
29+
public required string? Border { get; init; }
2930

3031
public string Style
3132
{

tests/Elastic.Markdown.Tests/Directives/ImageTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,36 @@ public void WarnsOnExternalUri()
6666
.And.OnlyContain(d => d.Severity == Severity.Warning);
6767
}
6868
}
69+
70+
public class ImageBlockWithBorderTests(ITestOutputHelper output) : DirectiveTest<ImageBlock>(output,
71+
"""
72+
:::{image} img/observability.png
73+
:alt: Elasticsearch
74+
:width: 400px
75+
:border:
76+
:::
77+
"""
78+
)
79+
{
80+
protected override void AddToFileSystem(MockFileSystem fileSystem) =>
81+
fileSystem.AddFile(@"docs/img/observability.png", "");
82+
83+
[Fact]
84+
public void ParsesBlock() => Block.Should().NotBeNull();
85+
86+
[Fact]
87+
public void ParsesBorderProperty()
88+
{
89+
Block!.Alt.Should().Be("Elasticsearch");
90+
Block!.Width.Should().Be("400px");
91+
Block!.ImageUrl.Should().Be("/img/observability.png");
92+
Block!.Border.Should().Be("border");
93+
}
94+
95+
[Fact]
96+
public void ImageIsFoundSoNoErrorIsEmitted()
97+
{
98+
Block!.Found.Should().BeTrue();
99+
Collector.Diagnostics.Count.Should().Be(0);
100+
}
101+
}

0 commit comments

Comments
 (0)