Skip to content
Closed
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
4 changes: 2 additions & 2 deletions docs/syntax/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Titles are optional making this the minimal syntax required:
![Elasticsearch](/syntax/images/observability.png)
```

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.
For inline images, the Markdown title is used as the `title` attribute, and the alt text is used as the `alt` attribute. If you don't specify a title, the alt text is used for both attributes.

```markdown
![Elasticsearch](/syntax/images/observability.png "Different title")
Expand All @@ -110,7 +110,7 @@ Image sizing is specified through the title argument. You can specify just the s
![alt](img.png "=W")
```

In this case, the alt text will be used as both the `alt` and `title` attributes, and the size parameters will be applied.
In this case, the alt text is used as both the `alt` and `title` attributes, and the size parameters are applied.

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

Expand Down
12 changes: 9 additions & 3 deletions src/Elastic.Markdown/Myst/Renderers/HtmxLinkInlineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected override void Write(HtmlRenderer renderer, LinkInline link)
}
else if (link.IsImage)
{
// Handle inline images with ALT override logic
// Handle inline images
WriteImage(renderer, link);
}
else
Expand All @@ -95,8 +95,14 @@ private static void WriteImage(HtmlRenderer renderer, LinkInline link)
// Write any additional attributes (like width/height from styling instructions)
_ = renderer.WriteAttributes(link);

// Always use alt text as title for accessibility consistency
if (link.FirstChild != null)
// Use the Markdown title when provided, otherwise fall back to the alt text
if (!string.IsNullOrEmpty(link.Title))
{
_ = renderer.Write(" title=\"");
_ = renderer.WriteEscape(link.Title);
_ = renderer.Write('"');
}
else if (link.FirstChild != null)
{
_ = renderer.Write(" title=\"");
renderer.WriteChildren(link);
Expand Down
8 changes: 4 additions & 4 deletions tests/Elastic.Authoring.Tests/Inline/InlineImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class SupplyingATitle : MarkdownTest
public async Task ValidateHtml() =>
await Docs.ConvertsToHtml(
"""
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Elasticsearch" /></p>
<p><img src="/_static/img/observability.png" alt="Elasticsearch" title="Hello world" /></p>
"""
);
}
Expand All @@ -58,7 +58,7 @@ public class SupplyingATitleWithWidthAndHeight : MarkdownTest
[Test, DisplayName("validate HTML: does not include width and height in title")]
public async Task ValidateHtml() =>
await Docs.ConvertsToHtml("""
<p><img src="/obs.png" width="250px" height="400px" alt="o" title="o"/></p>
<p><img src="/obs.png" width="250px" height="400px" alt="o" title="Title"/></p>
""");
}

Expand All @@ -71,7 +71,7 @@ public class SupplyingATitleWithWidthAndHeightInPercentage : MarkdownTest
[Test, DisplayName("validate HTML: does not include width and height in title")]
public async Task ValidateHtml() =>
await Docs.ConvertsToHtml("""
<p><img src="/obs.png" width="50%" height="40%" alt="o" title="o"/></p>
<p><img src="/obs.png" width="50%" height="40%" alt="o" title="Title"/></p>
""");
}

Expand All @@ -84,6 +84,6 @@ public class SupplyingATitleWithWidthOnly : MarkdownTest
[Test, DisplayName("validate HTML: sets height to width if not supplied")]
public async Task ValidateHtml() =>
await Docs.ConvertsToHtml("""
<p><img src="/obs.png" width="30%" height="30%" alt="o" title="o"/></p>
<p><img src="/obs.png" width="30%" height="30%" alt="o" title="Title"/></p>
""");
}
4 changes: 2 additions & 2 deletions tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void GeneratesAttributesInHtml() =>
);
}

// Test image sizing with title and sizing — explicit title in markdown is ignored; alt text is always used as title
// Test image sizing with title and sizing — explicit title in markdown is used as the title; alt text stays as alt
[InheritsTests]
public class InlineImageWithTitleAndSizingTest() : InlineTest<LinkInline>(
"""
Expand All @@ -104,7 +104,7 @@ public class InlineImageWithTitleAndSizingTest() : InlineTest<LinkInline>(
public void GeneratesAttributesInHtml() =>
// language=html
Html.ShouldContainHtml(
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" title="Elasticsearch" /></p>"""
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" title="My Title" /></p>"""
);
}

Expand Down
Loading