Skip to content

Commit 79ff25c

Browse files
committed
Update docs and add alt fallback
1 parent 33ba431 commit 79ff25c

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

docs/syntax/images.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Or, use the `image` directive.
2929
:::
3030
```
3131

32+
Note: When using the image directive, if you specify an `:alt:` value but no `:title:` value, the alt text will automatically be used as the title.
33+
3234
:::{image} /syntax/images/observability.png
3335
:alt: Elasticsearch
3436
:width: 250px
@@ -60,38 +62,39 @@ Here is the same image used inline ![Elasticsearch](/syntax/images/observability
6062

6163
### Inline image titles
6264

63-
Titles are optional making this the minimal syntax required
65+
Titles are optional making this the minimal syntax required:
6466

6567
```markdown
6668
![Elasticsearch](/syntax/images/observability.png)
6769
```
6870

69-
Including a title can be done by supplying it as an optional argument.
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:
7074

7175
```markdown
72-
![Elasticsearch](/syntax/images/observability.png "elasticsearch")
76+
![Elasticsearch](/syntax/images/observability.png "Different title")
7377
```
7478

7579
### Inline image sizing
7680

77-
Inline images are supplied at the end through the title argument.
78-
79-
This is done to maintain maximum compatibility with markdown parsers
80-
and previewers.
81+
Image sizing is specified through the title argument. You can specify just the size without needing to provide a redundant title:
8182

8283
```markdown
83-
![alt](img.png "title =WxH")
84-
![alt](img.png "title =W")
84+
![alt](img.png "=WxH")
85+
![alt](img.png "=W")
8586
```
8687

88+
In this case, the alt text will automatically be used as the title, and the size parameters will be applied.
89+
8790
`W` and `H` can be either an absolute number in pixels or a number followed by `%` to indicate relative sizing.
8891

8992
If `H` is omitted `W` is used as the height as well.
9093

9194
```markdown
92-
![alt](img.png "title =250x330")
93-
![alt](img.png "title =50%x40%")
94-
![alt](img.png "title =50%")
95+
![alt](img.png "=250x330")
96+
![alt](img.png "=50%x40%")
97+
![alt](img.png "=50%")
9598
```
9699

97100

@@ -138,7 +141,7 @@ The image carousel directive builds upon the image directive.
138141

139142
:::{image} images/applies.png
140143
:alt: Second image description
141-
:title: Second image title
144+
### Title is optional - alt text will be used as title if not specified
142145
:::
143146

144147
::::

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public override void FinalizeAndValidate(ParserContext context)
7474
{
7575
Label = Prop("label", "name");
7676
Alt = Prop("alt")?.ReplaceSubstitutions(context) ?? string.Empty;
77-
Title = Prop("title")?.ReplaceSubstitutions(context);
77+
// Use Alt as Title if no explicit Title is provided
78+
var explicitTitle = Prop("title")?.ReplaceSubstitutions(context);
79+
Title = explicitTitle ?? (!string.IsNullOrEmpty(Alt) ? Alt : null);
7880

7981
Align = Prop("align");
8082
Height = Prop("height", "h");

tests/authoring/Blocks/ImageBlocks.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ``static path to image`` () =
1818
[<Fact>]
1919
let ``validate src is anchored`` () =
2020
markdown |> convertsToContainingHtml """
21-
<img loading="lazy" alt="Elasticsearch" src="/img/observability.png" style="width: 250px;" class="screenshot">
21+
<img loading="lazy" title="Elasticsearch" alt="Elasticsearch" src="/img/observability.png" style="width: 250px;" class="screenshot">
2222
"""
2323

2424
type ``supports --url-path-prefix`` () =
@@ -45,13 +45,13 @@ type ``supports --url-path-prefix`` () =
4545
[<Fact>]
4646
let ``validate image src contains prefix`` () =
4747
docs |> convertsToContainingHtml """
48-
<img loading="lazy" alt="Elasticsearch" src="/docs/img/observability.png" style="width: 250px;" class="screenshot">
48+
<img loading="lazy" title="Elasticsearch" alt="Elasticsearch" src="/docs/img/observability.png" style="width: 250px;" class="screenshot">
4949
"""
5050

5151
[<Fact>]
5252
let ``validate image src contains prefix when referenced relatively`` () =
5353
docs |> converts "folder/relative.md" |> containsHtml """
54-
<img loading="lazy" alt="Elasticsearch" src="/docs/img/observability.png" style="width: 250px;" class="screenshot">
54+
<img loading="lazy" title="Elasticsearch" alt="Elasticsearch" src="/docs/img/observability.png" style="width: 250px;" class="screenshot">
5555
"""
5656

5757
[<Fact>]
@@ -73,7 +73,7 @@ type ``image ref out of scope`` () =
7373
[<Fact>]
7474
let ``validate image src contains prefix and is anchored to documentation scope root`` () =
7575
docs |> convertsToContainingHtml """
76-
<img loading="lazy" alt="Elasticsearch" src="/docs/img/observability.png" style="width: 250px;" class="screenshot">
76+
<img loading="lazy" title="Elasticsearch" alt="Elasticsearch" src="/docs/img/observability.png" style="width: 250px;" class="screenshot">
7777
"""
7878

7979
[<Fact>]

0 commit comments

Comments
 (0)