Skip to content

Commit 3b5c23f

Browse files
committed
Fix override
1 parent 79ff25c commit 3b5c23f

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

docs/syntax/images.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ Titles are optional making this the minimal syntax required:
6868
![Elasticsearch](/syntax/images/observability.png)
6969
```
7070

71-
When no title is explicitly provided, the alt text is automatically used as the title.
72-
73-
If you want a different title, you can supply it as an optional argument:
71+
For inline images, the alt text always overrides any title specified in the Markdown. This ensures consistent accessibility where both the `alt` and `title` attributes contain the same descriptive text.
7472

7573
```markdown
7674
![Elasticsearch](/syntax/images/observability.png "Different title")
7775
```
7876

77+
![Elasticsearch](/syntax/images/observability.png "Different title")
78+
79+
7980
### Inline image sizing
8081

8182
Image sizing is specified through the title argument. You can specify just the size without needing to provide a redundant title:
@@ -85,7 +86,7 @@ Image sizing is specified through the title argument. You can specify just the s
8586
![alt](img.png "=W")
8687
```
8788

88-
In this case, the alt text will automatically be used as the title, and the size parameters will be applied.
89+
In this case, the alt text will be used as both the `alt` and `title` attributes, and the size parameters will be applied.
8990

9091
`W` and `H` can be either an absolute number in pixels or a number followed by `%` to indicate relative sizing.
9192

src/Elastic.Markdown/Myst/Renderers/HtmxLinkInlineRenderer.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ protected override void Write(HtmlRenderer renderer, LinkInline link)
2020
{
2121
if (renderer.EnableHtmlForInline && !link.IsImage)
2222
{
23-
// ReSharper disable once UnusedVariable
2423
if (link.GetData(nameof(ParserContext.CurrentUrlPath)) is not string currentUrl)
2524
{
2625
base.Write(renderer, link);
@@ -70,9 +69,42 @@ protected override void Write(HtmlRenderer renderer, LinkInline link)
7069

7170
_ = renderer.Write("</a>");
7271
}
72+
else if (link.IsImage)
73+
{
74+
// Handle inline images with ALT override logic
75+
WriteImage(renderer, link);
76+
}
7377
else
7478
base.Write(renderer, link);
7579
}
80+
81+
private static void WriteImage(HtmlRenderer renderer, LinkInline link)
82+
{
83+
_ = renderer.Write("<img src=\"");
84+
_ = renderer.WriteEscapeUrl(link.GetDynamicUrl?.Invoke() ?? link.Url);
85+
_ = renderer.Write('"');
86+
87+
// Write alt text using WriteChildren to ensure substitutions are processed
88+
if (link.FirstChild != null)
89+
{
90+
_ = renderer.Write(" alt=\"");
91+
renderer.WriteChildren(link);
92+
_ = renderer.Write('"');
93+
}
94+
95+
// Write any additional attributes (like width/height from styling instructions)
96+
_ = renderer.WriteAttributes(link);
97+
98+
// Set title to alt text for inline images (after any substitutions are processed)
99+
if (link.FirstChild != null)
100+
{
101+
_ = renderer.Write(" title=\"");
102+
renderer.WriteChildren(link);
103+
_ = renderer.Write('"');
104+
}
105+
106+
_ = renderer.Write(" />");
107+
}
76108
}
77109

78110
public static class CustomLinkInlineRendererExtensions

tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public class ReplaceInImageTitle(ITestOutputHelper output) : InlineTest(output,
203203
[Fact]
204204
public void OnlySeesGlobalVariable() =>
205205
Html.Should().NotContain("title=\"{{hello-world}}\"")
206-
.And.Contain("title=\"Hello World\"");
206+
.And.Contain("title=\"Observability\"");
207207
}
208208

209209
public class MutationOperatorTest(ITestOutputHelper output) : InlineTest(output,

tests/authoring/Inline/InlineAppliesTo.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ``static path to image`` () =
1515
[<Fact>]
1616
let ``validate HTML: generates link and alt attr`` () =
1717
markdown |> convertsToHtml """
18-
<p><img src="/_static/img/observability.png" alt="Elasticsearch" /></p>
18+
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Elasticsearch" /></p>
1919
"""
2020

2121
type ``relative path to image`` () =
@@ -26,7 +26,7 @@ type ``relative path to image`` () =
2626
[<Fact>]
2727
let ``validate HTML: preserves relative path`` () =
2828
markdown |> convertsToHtml """
29-
<p><img src="/_static/img/observability.png" alt="Elasticsearch" /></p>
29+
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Elasticsearch" /></p>
3030
"""
3131

3232
type ``supplying a tittle`` () =
@@ -37,7 +37,7 @@ type ``supplying a tittle`` () =
3737
[<Fact>]
3838
let ``validate HTML: includes title`` () =
3939
markdown |> convertsToHtml """
40-
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Hello world" /></p>
40+
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Elasticsearch" /></p>
4141
"""
4242

4343
type ``supplying a tittle with width and height`` () =
@@ -48,7 +48,7 @@ type ``supplying a tittle with width and height`` () =
4848
[<Fact>]
4949
let ``validate HTML: does not include width and height in title`` () =
5050
markdown |> convertsToHtml """
51-
<p><img src="/obs.png" width="250px" height="400px" alt="o" title="Title"/></p>
51+
<p><img src="/obs.png" width="250px" height="400px" alt="o" title="o"/></p>
5252
"""
5353

5454
type ``supplying a tittle with width and height in percentage`` () =
@@ -59,7 +59,7 @@ type ``supplying a tittle with width and height in percentage`` () =
5959
[<Fact>]
6060
let ``validate HTML: does not include width and height in title`` () =
6161
markdown |> convertsToHtml """
62-
<p><img src="/obs.png" width="50%" height="40%" alt="o" title="Title"/></p>
62+
<p><img src="/obs.png" width="50%" height="40%" alt="o" title="o"/></p>
6363
"""
6464
type ``supplying a tittle with width only`` () =
6565
static let markdown = Setup.Markdown """
@@ -69,5 +69,5 @@ type ``supplying a tittle with width only`` () =
6969
[<Fact>]
7070
let ``validate HTML: sets height to width if not supplied`` () =
7171
markdown |> convertsToHtml """
72-
<p><img src="/obs.png" width="30%" height="30%" alt="o" title="Title"/></p>
72+
<p><img src="/obs.png" width="30%" height="30%" alt="o" title="o"/></p>
7373
"""

0 commit comments

Comments
 (0)