Skip to content

Commit a50013d

Browse files
use substitution values in image alt and title text
1 parent 0f77059 commit a50013d

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private static void WriteImage(HtmlRenderer renderer, ImageBlock block)
107107
Label = block.Label,
108108
Align = block.Align,
109109
Alt = block.Alt,
110+
Title = block.Title,
110111
Height = block.Height,
111112
Scale = block.Scale,
112113
Target = block.Target,
@@ -144,6 +145,7 @@ private static void WriteFigure(HtmlRenderer renderer, ImageBlock block)
144145
Label = block.Label,
145146
Align = block.Align,
146147
Alt = block.Alt,
148+
Title = block.Title,
147149
Height = block.Height,
148150
Scale = block.Scale,
149151
Target = block.Target,

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Markdown.Helpers;
56
using Elastic.Markdown.Diagnostics;
67
using Elastic.Markdown.IO;
78

@@ -20,6 +21,11 @@ public class ImageBlock(DirectiveBlockParser parser, ParserContext context)
2021
/// </summary>
2122
public string? Alt { get; set; }
2223

24+
/// <summary>
25+
/// Title text: a short description of the image
26+
/// </summary>
27+
public string? Title { get; set; }
28+
2329
/// <summary>
2430
/// The desired height of the image. Used to reserve space or scale the image vertically. When the “scale” option
2531
/// is also specified, they are combined. For example, a height of 200px and a scale of 50 is equivalent to
@@ -64,9 +70,10 @@ public class ImageBlock(DirectiveBlockParser parser, ParserContext context)
6470
public override void FinalizeAndValidate(ParserContext context)
6571
{
6672
Label = Prop("label", "name");
67-
Alt = Prop("alt");
68-
Align = Prop("align");
73+
Alt = (Prop("alt") ?? "{undefined}").ReplaceSubstitutions(context);
74+
Title = (Prop("title") ?? "{undefined}").ReplaceSubstitutions(context);
6975

76+
Align = Prop("align");
7077
Height = Prop("height", "h");
7178
Width = Prop("width", "w");
7279

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,21 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
6363

6464
ValidateAndProcessLink(link, processor, context);
6565

66-
ParseStylingInstructions(link);
66+
ParseStylingInstructions(link, context);
6767

6868
return match;
6969
}
7070

7171

72-
private static void ParseStylingInstructions(LinkInline link)
72+
private static void ParseStylingInstructions(LinkInline link, ParserContext context)
7373
{
7474
if (!link.IsImage)
7575
return;
7676

77+
var attributes = link.GetAttributes();
78+
7779
if (string.IsNullOrWhiteSpace(link.Title) || link.Title.IndexOf('=') < 0)
78-
return;
80+
link.Title = (link.Title ?? "{undefined}").ReplaceSubstitutions(context);
7981

8082
var matches = LinkRegexExtensions.MatchTitleStylingInstructions().Match(link.Title);
8183
if (!matches.Success)
@@ -89,10 +91,7 @@ private static void ParseStylingInstructions(LinkInline link)
8991
height = width;
9092
else if (!height.EndsWith('%'))
9193
height += "px";
92-
var title = link.Title[..matches.Index];
9394

94-
link.Title = title;
95-
var attributes = link.GetAttributes();
9695
attributes.AddProperty("width", width);
9796
attributes.AddProperty("height", height);
9897
}

src/Elastic.Markdown/Slices/Directives/_ViewModels.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ImageViewModel
5050
public required string? Label { get; init; }
5151
public required string? Align { get; init; }
5252
public required string? Alt { get; init; }
53+
public required string? Title { get; init; }
5354
public required string? Height { get; init; }
5455
public required string? Scale { get; init; }
5556
public required string? Target { get; init; }

0 commit comments

Comments
 (0)